1. (The Rectangle class) Design a class named Rectangle to represent a rectangle. The class contains:
Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height.
A string data field named color that specifies the color of a rectangle. Hypothetically, assume that all rectangles have the same color. The default color is white.
A no-arg constructor that creates a default rectangle.
A constructor that creates a rectangle with the specified width and height.
The accessor and mutator methods for all three data fields.
A method named getArea() that returns the area of this rectangle.
A method named getPerimeter() that returns the perimeter.
---------------------------------------------
half way
public class lab17sept2016 {
public static void main(String[] args) {
// cara bina objek
Rectangle segi4 = new Rectangle();
System.out.println(segi4.color);
System.out.println(segi4.height);
System.out.println(segi4.width);
Rectangle segiempat = new Rectangle(10,15);
System.out.println(segiempat.color);
System.out.println(segiempat.height);
System.out.println(segiempat.width);
}
}
class Rectangle{
double width=1;
double height=1;
String color="white";
//A no-arg constructor that creates a default rectangle.
Rectangle(){
}
//A constructor that creates a rectangle with the specified width and height
Rectangle(double newWidth, double newheight){
width = newWidth;
height = newheight;
}
//The accessor (get) and mutator(set) methods for all three data fields.
double getwidth(){
return width;
}
double getheight(){
return height;
}
String getcolor(){
return color;
}
void setwidth(double newWidth){
width =newWidth;
}
void setheight(double newheight){
height =newheight;
}
void setcolor(String newColor){
color = newColor;
}
double getArea(){
return (width * height) ;
}
double getPerimeter(){
return (2*(width + height)) ;
}
}
2. (The Account class) Design a class named Account that contains:
An int data field named id for the account (default 0).
A double data field named balance for the account (default 0).
A double data field named annualInterestRate that stores the current interest rate (default 0).
A Date data field named dateCreated that stores the date when the account was created.
A no-arg constructor that creates a default account.
The accessor and mutator methods for id, balance, and annualInterestRate.
The accessor method for dateCreated.
A method named getMonthlyInterestRate() that returns the monthly interest rate.
A method named withDraw that withdraws a specified amount from the account.
A method named deposit that deposits a specified amount to the account.
Draw the UML diagram for the class. Implement the class. Write a test program that creates an Account object with an account ID of 1122, a balance of 20000, and an annual interest rate of 4.5%. Use the withdraw method to withdraw $2500, use the deposit method to deposit $3000, and print the balance, the monthly interest, and the date when this account was created.
--------------------------------------------------------------------------------------------------------------------
public class Test {
public static void main (String[] args) {
Account account = new Account(1122, 20000);
Account.setAnnualInterestRate(4.5);
account.withdraw(2500);
account.deposit(3000);
System.out.println("Balance is " + account.getBalance());
System.out.println("Monthly interest is " +
account.getMonthlyInterest());
System.out.println("This account was created at " +
account.getDateCreated());
}
}
Class Account {
// Implement the class here
}
No comments:
Post a Comment