The question is
- Write a class named Rectangle to represent rectangle objects. The UML diagram for the class is shown below. Then, write a program to test the class Rectangle. In this program, create three Rectangle objects. The first object has width 4 and height 40 while the second object has width 25 and height 20.5. Assign any colors that you like. The third object has 1 for both width and height but the color is red. For this object, use the first constructor and then change the color to red using the appropriate method. Display the properties of these objects including their areas, perimeters and diagonal lengths.
dThe here is I declare Rectangle Class.
here I create another class which contain main method. Main method combine everything and which method should run./* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Exercise2; /** * * @author Achmad Zulkarnain */ public class Rectangle { double width; double height; String color; public Rectangle() { //Default Constructor width = 1; height = 1; color = "green"; } public Rectangle(double w, double h, String s) { //constructor width = w; height = h; color = s; } public double getWidth() { return width; } public void setWidth(double w) { width = w; } public double getheight() { return height; } public void setHeight(double h) { height = h; } public String getColor() { return color; } public void setColor(String c) { color = c; } public double getArea() { return width*height; } public double getPerimeter() { return 2 * (width + height); } public double getDiagonal() { return Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2)); } }
package Exercise2; public class javamain { /** * @param args the command line arguments */ public static void main(String[] args) { Rectangle obj1 = new Rectangle(4, 40, "Purple"); Rectangle obj2 = new Rectangle(25, 20.5, "Blue"); Rectangle obj3 = new Rectangle(); //Object 1 System.out.println("The first object has width: "+obj1.getWidth()); System.out.println("The first object has height: "+obj1.getheight()); System.out.println("The first object has color: "+obj1.getColor()); System.out.println("The area of the first object is "+obj1.getArea()); System.out.println("The parimeter of this object is "+obj1.getPerimeter()); System.out.println("The diagonal object is "+obj1.getDiagonal()); System.out.println("#######################################"); //Object 2 System.out.println("The second object has width: "+obj2.getWidth()); System.out.println("The second object has height: "+obj2.getheight()); System.out.println("The second object has color: "+obj2.getColor()); System.out.println("The area of the second object is "+obj2.getArea()); System.out.println("The parimeter of this object is "+obj2.getPerimeter()); System.out.println("The diagonal object is "+obj2.getDiagonal()); System.out.println("#######################################"); System.out.println("The third object has width: "+obj3.getWidth()); System.out.println("The third object has height: "+obj3.getheight()); obj3.setColor("Red"); System.out.println("The third object has color: "+obj3.getColor()); System.out.println("The area of the third object is "+obj3.getArea()); System.out.println("The parimeter of this object is "+obj3.getPerimeter()); System.out.println("The diagonal object is "+obj3.getDiagonal()); } }
0 comments:
Post a Comment