Tải bản đầy đủ (.pdf) (40 trang)

bài tập assignments môn học et4430 lập trình nâng cao

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (703.67 KB, 40 trang )

<span class="text_page_counter">Trang 1</span><div class="page_container" data-page="1">

System.out.println("Hello World"); }

}

2.

// TODO Auto-generated method stub double x1,y1,x2,y2,d,d2;

Scanner scanner = new Scanner (System.in); System.out.println ("Nhap toa do diem thu nhat"); x1= scanner.nextDouble();

System.out.print("Khoang cach giua 2 diem la: " + Math.ceil(d*100.0)/100.0);

}

</div><span class="text_page_counter">Trang 2</span><div class="page_container" data-page="2">

3.

package b3;

// TODO Auto-generated method stub double x1,y1,x2,y2,r1,r2,d,d2;

Scanner scanner = new Scanner (System.in);

System.out.println ("Nhap toa do tam duong tron 1:"); x1= scanner.nextDouble();

<b>Assignment Tu n 2+3 </b>ầ

<b>Bài làm </b>

</div><span class="text_page_counter">Trang 3</span><div class="page_container" data-page="3">

publicclass Rectangle { double width; double height; public Rectangle() { }

public Rectangle(double width, double height) { this.width = width;

this.height = height; }

public double getWidth() { return width; }

public void setWidth(double width) { this.width = width;

}

public double getHeight() { return height; }

public void setHeight(double height) { this.height = height;

}

public double getArea() { return getHeight()*getWidth(); }

public double getPerimeter() { return getHeight() + getWidth(); }

}

int channel; int volumeLevel; boolean on;

</div><span class="text_page_counter">Trang 4</span><div class="page_container" data-page="4">

public void turnOn() { on = true;

System.out.println("Turn on this TV"); }

public void turnOff() { on = false;

System.out.println("Turn off this TV"); }

public void setChannel (int newChanel) { this.channel = newChanel;

System.out.println("Set "+ this.channel +" for this TV"); }

public void setVolume (int newVolumeLevel) { this.volumeLevel = newVolumeLevel;

System.out.println("Set "+ this.volumeLevel+" for this TV"); }

public void channelUp() { this.channel += ; 1

System.out.println("Increases the channel number by 1");

}

public void channelDown() { this.channel -= ; 1

System.out.println("Decreases the channel by 1"); }

</div><span class="text_page_counter">Trang 5</span><div class="page_container" data-page="5">

public static void main(String[] args) { // TODO Auto-generated method stub

}

}

4.

// TODO Auto-generated method stub

System.out.println("---Ket qua bai 4:---"); Random random = new Random(1000);

int count = ; 0

for int( i = ; i < 0 50; i++) {

/* After 10 numbers go to the next line */ if(count == 10) {

System.out.print("\n"); count = ; 0

}

System.out.print(random.nextInt(100) + " "); count++;

}

System.out.println(" "); }

5.

</div><span class="text_page_counter">Trang 6</span><div class="page_container" data-page="6">

public Date getStartTime() { return startTime; }

public void setStartTime(Date startTime) { this.startTime = startTime;

}

public Date getEndTime() { return endTime; }

public void setEndTime(Date endTime) { this.endTime = endTime;

}

public stopWatch() { startTime = new Date(); }

public void start() { startTime = new Date(); }

public void stop() { endTime = new Date(); }

public double getElapsedTime() {

return endTime.getTime() - startTime.getTime(); }

// TODO Auto-generated method stub /*---1. Ket qua bai 1---*/

System.out.println("---Ket qua bai --");

Rectangle rectangle = new Rectangle(); rectangle.setHeight( ); 2

System.out.println("Height use setHeight function: " + rectangle.getHeight());

</div><span class="text_page_counter">Trang 7</span><div class="page_container" data-page="7">

System.out.println("Width after use setWidth function: " + rectangle.getWidth());

rectangle = new Rectangle(4 5, );

System.out.println("Height use contrusctor have argument function: "+ rectangle.getHeight() );

System.out.println("Width use contrusctor have argument function: "+ rectangle.getWidth() );

System.out.println("The area of rectangle use contrusctor have argument function: " + rectangle.getArea());

System.out.println("The Perimeter of rectangle use contrusctor have argument function: " + rectangle.getPerimeter());

/*---2.Ket qua bai 2---*/

System.out.println("---Ket qua bai --");

TV tv = new TV(); tv.turnOn();

tv.turnOff(); tv.setChannel( ); 1 tv.setVolume( ); 2 tv.channelUp(); tv.channelDown(); tv.volumeUp(); tv.volumeDown();

/*---3.Ket qua bai 3---*/

System.out.println("---Ket qua bai -");

Date date = new Date((long) pow(10 2, ));

date = new Date((long) pow(10 4,));

System.out.println("date after 10000ms: " + date.toString());

date = new Date((long) pow(10 5,));

System.out.println("date after 100000ms: " + date.toString());

date = new Date((long) pow(10 6,));

System.out.println("date after 1000000ms: " + date.toString());

date = new Date((long) pow(10 7,));

System.out.println("date after 10000000ms: " + date.toString());

date = new Date((long) pow(10 8,));

System.out.println("date after 100000000ms: " + date.toString());

</div><span class="text_page_counter">Trang 8</span><div class="page_container" data-page="8">

System.out.println("date after 1000000000ms: " + date.toString());

date = new Date((long) pow(10 10, ));

System.out.println("date after 10000000000ms: " + date.toString()); /*---3.Ket qua bai 4---*/

System.out.println("---Ket qua bai -");

Random random = new Random(1000); int count = ; 0

for int( i = ; i < 0 50; i++) {

/* After 10 numbers go to the next line */ if(count == 10) {

System.out.print("\n"); count = ; 0

}

System.out.print(random.nextInt(100) + " "); count++;

}

System.out.println(" ");

/*---5. Ket qua bai 5---*/

System.out.println("---Ket qua bai -");

stopWatch stopwatch = new stopWatch();

System.out.println("Start time after constructor: " + stopwatch.getStartTime());

stopwatch.setStartTime(new Date(1000));

System.out.println("Start time after use setStartTime function: "+ stopwatch.getStartTime());

stopwatch.setEndTime(new Date(100000));

System.out.println("End time after use setEndTime function: "+ stopwatch.getEndTime());

System.out.println("Time elapsed: " + stopwatch.getElapsedTime()); }

</div><span class="text_page_counter">Trang 9</span><div class="page_container" data-page="9">

public int SLOW = ; 1 public int MEDIUM = ; 2 public int FAST = ; 3

String color; Fan(){

speed = SLOW; on = false; radius = ; 5 color = "blue"; }

return speed; }

this.speed = speed; }

return on; }

this.on = on;

}public double getRadius() { return radius;

}

this.radius = radius; }public String getColor() {

</div><span class="text_page_counter">Trang 10</span><div class="page_container" data-page="10">

public void setColor(String color) { this.color = color;

}

return ( "Fan is "+ this.color + " and the radius of the fan is " + this.radius);

} }

2.

public MyInteger(int value1) { this.value= value1; }

return value; }

return ((value % ) == ); 2 0 }

return ((value % ) == ); 2 1 }

if (value == || value == ) { 1 2 return true;

} else {

for (int i = 2; i < value; i++) { if (value % i == ) 0 return false; }

}

return true; }

return myInt.isEven(); }

</div><span class="text_page_counter">Trang 11</span><div class="page_container" data-page="11">

return myInt.isOdd(); }

return myInt.isPrime(); }

if (this.value == value2) return true; return false;

}

if (this.value == value3.value) return true; return false;

}

String newString = String.valueOf(arrayChar); int newValue = Integer.valueOf(newString); return newValue;

}

int newValue = Integer.valueOf(newString); return newValue;

} }

3.

private int id;

privatedouble balance; double annualInterestRate; private Date dateCreated;

public Account() { id = ; 0 balance = ; 0

annualInterestRate = ; 0

</div><span class="text_page_counter">Trang 12</span><div class="page_container" data-page="12">

public Account(int id, double balance) { this.id = id;

this.balance = balance; dateCreated = new Date(); }

public int getId() { return id; }

public void setId(int id) { this.id = id;

}

public double getBalance() { return balance; }

public void setBalance(double balance) { this.balance = balance;

}

public double getAnnualInterestRate() { return annualInterestRate; }

public void setAnnualInterestRate(double annualInterestRate) { this.annualInterestRate = annualInterestRate;

}

public Date getDateCreated() { return dateCreated; }

public double getMonthlyInterestRate() { return annualInterestRate / 12; }

public double getMonthlyInterest() { return balance * getAnnualInterestRate(); }

</div><span class="text_page_counter">Trang 13</span><div class="page_container" data-page="13">

Scanner scanner = new Scanner(System.in); double withdrawMoney = scanner.nextDouble(); balance -= withdrawMoney;

}

public void deposit() {

System.out.print("Enter an amount to deposit: "); Scanner scanner = new Scanner(System.in); double depositMoney = scanner.nextDouble(); balance += depositMoney;

} }

4.

System.out.println("---Ket qua bai 4---"); Account[] accountList = new Account[10]; for int( i = ; i < accountList.length; i++) { 0 accountList[i] = new Account(i, 1000000); }

while true( ) {

System.out.print("Nhap id: "); int id = scanner.nextInt(); if(id < || id > ) { 0 9

System.out.println("The ID is not correct. Please enter ID again!");

</div><span class="text_page_counter">Trang 14</span><div class="page_container" data-page="14">

accountList[id].getBalance()); break; case :

accountList[id].withdraw(); break;

case :

accountList[id].deposit(); break;

case :

check = false; break; default:

System.out.println("The choice is not in menu. Please enter the choice again!");

break; }

} } } }

// TODO Auto-generated method stub /*---Bai1---*/

System.out.println("---Ket qua bai 1---"); Fan fan1 = new Fan();

Fan fan2 = new Fan(); fan1.setSpeed( ); 3 fan1.setRadius(10); fan1.setColor("yellow"); fan1.setOn(true); fan2.setSpeed( ); 2 fan2.setColor("blue"); fan2.setRadius( ); 5 fan2.setOn(false);

</div><span class="text_page_counter">Trang 15</span><div class="page_container" data-page="15">

System.out.println("Gia tri 1 = "+ value1.getValue()); System.out.println("Gia tri 2 = "+ value2.getValue()); System.out.println("Gia tri 3 = "+ value3.getValue());

System.out.println("Gia tri 1 la so chan: " + value1.isEven()); System.out.println("Gia tri 1 la so le: " + value1.isOdd()); System.out.println("Gia tri 1 la so nguyen to: " + value1.isPrime());

System.out.println("Gia tri 2 la so chan: " + value2.isEven()); System.out.println("Gia tri 2 la so le: " + value2.isOdd()); System.out.println("Gia tri 2 la so nguyen to: " + value2.isPrime());

System.out.println("Gia tri 3 la so chan: " + value3.isEven()); System.out.println("Gia tri 3 la so le: " + value3.isOdd()); System.out.println("Gia tri 3 la so nguyen to: " + value3.isPrime());

MyInteger newMyInteger = new MyInteger( ); 9 System.out.println("newMyInteger chan: " + newMyInteger.isEven(newMyInteger));

System.out.println("newMyIntege le: " + newMyInteger.isOdd(newMyInteger));

System.out.println("newMyInteger la so nguyen to: " + newMyInteger.isPrime(newMyInteger));

System.out.print("Nhap so can kiem tra: "); Scanner scanner = new Scanner(System.in); int newNumber = scanner.nextInt();

System.out.println(newNumber + "= gia tri 1: " + value1.equals(newNumber));

System.out.println("Gia tri newMyInteger: " + newMyInteger.getValue() + " bang gia tri value1: " + value1.equals(newMyInteger));

char[] charArray = {'2', '0', '2' '2', }; int convertNum = value.parseInt(charArray);

System.out.println("Mang chuyen thanh so: " + convertNum);

</div><span class="text_page_counter">Trang 16</span><div class="page_container" data-page="16">

account.withdraw();

account.setAnnualInterestRate(4.5/100);

NumberFormat currentLocale = NumberFormat.getInstance(); System.out.println("Balance: " +

currentLocale.format(account.getBalance())); System.out.println("Monthly interest: " + currentLocale.format(account.getMonthlyInterest()));

System.out.println("Date created: " + account.getDateCreated());

/*---Bai4---*/

System.out.println("---Ket qua bai 4---"); Account[] accountList = new Account[10]; for int( i = ; i < accountList.length; i++) { 0 accountList[i] = new Account(i, 1000000); }

while true( ) {

System.out.print("Nhap id: "); int id = scanner.nextInt(); if(id < || id > ) { 0 9

System.out.println("The ID is not correct. Please enter ID again!");

case :

System.out.println("The balance is " + accountList[id].getBalance());

break;

</div><span class="text_page_counter">Trang 17</span><div class="page_container" data-page="17">

break; case :

accountList[id].deposit(); break;

case :

check = false; break; default:

System.out.println("The choice is not in menu. Please enter the choice again!");

break; }

} } } }

}

this.color = color; this.filled = filled;

this.dateCreated = new Date(); }

return color; }

this.color = color; }

</div><span class="text_page_counter">Trang 18</span><div class="page_container" data-page="18">

}

this.filled = filled; }

return dateCreated; }

return ("Color: "+ this.color + " IsFilled: "+ this.filled + " Date:" + this.dateCreated );

} }

double radius; public Circle2() { }

public Circle2(double radius) { this.radius = radius; }

public Circle2(String color, boolean filled, double radius) { super();

this.radius = radius; }

public double getRadius() { return radius; }

public void setRadius(double radius) { this.radius = radius;

}

public double getArea(){

return Math.PI * Math.pow(this.radius,2); }

public double getPerimeter(){ return * Math.PI * this.radius;

</div><span class="text_page_counter">Trang 19</span><div class="page_container" data-page="19">

public double getDiameter(){ return * this.radius; }

public String printCircle(){

return super.toString() + " Radius:"+this.radius; }

}

2.

}

emailAddress) {

this.name = name; this.address = address; this.phoneNumber = phoneNumber; this.emailAddress = emailAddress; }

this.name = name; }

return name; }

this.address = address; }

</div><span class="text_page_counter">Trang 20</span><div class="page_container" data-page="20">

return sum; }

public String toString () {

return ("Color: "+ this.color + " ,IsFilled: "+ this.filled + " ,Date:" + this.dateCreated );

} }

public Rectangle(double width, double height) { this.width = width;

this.height = height; }

public Rectangle(double width, double height, String color, boolean

filled) { super(); this.width = width; this.height = height; }

public double getWidth() { return width; }

public void setWidth(doublewidth) { this.width = width;

}

public double getHeight() { return height; }

public void setHeight(doubleheight) { this.height = height;

} @Override

public double getArea() { return width*height; }

public double getPerimeter() { return 2*(width height + ); }

public void printRectangle() {

System.out.println(super.toString() + " - Width: " + width + " - Height: " + height + " - Area: " + getArea() +

" - Perimeter: " + getPerimeter()); }

public String toString() {

</div>

×