My purpose was to create a class called MyRectangle to represent the rectangles.
Mandatory data fields: width, height and color. Use a double data type for width and height and a string for color. Then write a program to test the MyRectangle class. In the client program, create two MyRectangle objects. Assign the width and height to each of the two objects. Set the first object to red and the second to yellow. Display all properties of both objects, including their area.
I wrote everything and I get no errors, but my output remains the same no matter what values I insert in the rectangles.
package MyRectangle;
public class MyRectangle{
private double width = 1.0;
private double height = 1.0;
private static String color = "black";
public MyRectangle(double par, double par1){
width ++;
height ++;
}
public MyRectangle(double widthParam, double heightParam, String colorParam){
width = widthParam;
height = heightParam;
color = colorParam;
width ++;
height ++;
}
public double getWidth(){
return width;
}
public void setWidth(double widthParam){
width = (widthParam >= 0) ? widthParam: 0;
}
public double getHeight(){
return height;
}
public void setHeight(double heightParam){
height = (heightParam >= 0) ? heightParam: 0;
}
public static String getColor(){
return color;
}
public static void setColor(String colorParam){
color = colorParam;
}
public double findArea(){
return width * height;
}
}
class MyRectangleTest {
@SuppressWarnings("static-access")
public static void main(String args[]) {
MyRectangle r1 = new MyRectangle(5.0, 25.0);
r1.setColor("Red");
System.out.println(r1);
System.out.println("The area of rectangle one is: " + r1.findArea());
MyRectangle r2 = new MyRectangle(3.0, 9.0);
r2.setColor("Yellow");
System.out.println(r2);
System.out.println("The area of rectangle one is: " + r2.findArea());
}
}
source
share