Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class named MyRectangle to represent rectangles. The required data fiel

ID: 3691097 • Letter: C

Question

Create a class named MyRectangle to represent rectangles. The required data fields are width, height, and color. Use double data type for width and height, and a String for color. Suppose that all rectangles are the same color. Use a static variable for color. You will need to provide the accessor methods for the properties and a method findArea() for computing the area of the rectangle.

Compile and run your program until it works and the output looks nice. Add the necessary documentation as described in Course Documents, and then attach your .java file to this assignment. Do not attach the .class file, attach only the .java source code.

The outline of the class is given as follows:

public class MyRectangle{

private double width = 1.0;
private double height = 1.0;
private static String color = "white";

public MyRectangle(){ }

public MyRectangle(double widthParam, double heightParam, String colorParam){ }

public double getWidth(){ }

public void setWidth(double widthParam){ }

public double getHeight(){ }

public void setHeight(double heightParam){ }

public String getColor(){ }  

public static void setColor(String colorParam){ }

public double findArea(){ }

}

Write a program to test your class MyRectangle. In the client program, create two MyRectangle objects. Assign a width and height to each of the two objects. Assign the first object the color red, and the second, yellow. Display all properties of both objects including their area.

INCORRECT OUTPUT: So my output for the first rectangle keeps outputting yellow instead of red and I don't know how to fix it. My source code is below.

public class MyRectangle
{

private double width = 1.0;
private double height = 1.0;
private static String color = "white";


public MyRectangle()
{

}

public MyRectangle(double width, double height, String color)
{
this.width = width;
this.height = height;
this.color = color;
}

public static String getColor() {
return color;
}

public static void setColor(String color) {
MyRectangle.color = color;
}

public double getHeight() {
return height;
}

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

public double getWidth() {
return width;
}

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

//Find the area to calculate area of the rectangle

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

public static void main(String args[])
{

//Create first rectangle with width, height and color
MyRectangle rect1 = new MyRectangle();
rect1.setWidth(3);
rect1.setHeight(12);
rect1.setColor("red");

//Create second rectangle with width, height and color

MyRectangle rect2 = new MyRectangle();
rect2.setWidth(16);
rect2.setHeight(8);
rect2.setColor("yellow");

//Display details

System.out.println("First Rectangle Details ");
System.out.println("===========================");
System.out.println("Height : "+rect1.getHeight()+" Width : "+rect1.getWidth()+" Color : "+rect1.getColor());
System.out.println("Area of First Rectangle : "+rect1.findArea());

System.out.println(" Second Rectangle Details ");
System.out.println("===========================");
System.out.println("Height : "+rect2.getHeight()+" Width : "+rect2.getWidth()+" Color : "+rect2.getColor());
System.out.println("Area of Second Rectangle : "+rect2.findArea());

}

}

Explanation / Answer

public class Rectangle
{
private double width;
private double height;
private String color = "white";
public Rectangle ()
{
width = 1.0;
height = 1.0;
}

private Rectangle (double w, double h) //init width to w, init height to h
{
width = w;
height = h;
}

public double getWidth() // called a getter
{
return width;
}
public void setWidth(double w) // this is a mutator method. Also called a setter
{
width = w;
}
public double getHeight() // called a getter
{
return height;
}
public void setHeight(double w) // this is a mutator method. Also called a setter
{
height = w;
}
public double getArea()
{
return width*height;
}
public double getPerimeter()
{
return height* 2 + width* 2; // was "return 0.0;"
}
}
public class RectangleTester
{
public static void main (String[] args)
{
Rectangle rect1 = new Rectangle(4.0, 40.0);
Rectangle rect2 = new Rectangle(3.5, 35.9);
double result = rect1.getWidth();
System.out.println (result);
result = rect2.getWidth();
System.out.println (result);
}
}

-------------------------------------------------------------------------------------------------

public class RectangleDemo {

private class Rectangle {

private double height;

private double width;

private String color;

public Rectangle(double wid, double high){

height = high;

width = wid;

}

public Rectangle(){

height = 1;

width = 1;

color = "White";

}

public void setHeight(double high){

height = high;

}

public void setWidth(double wid){

width = wid;

}

public void setColor(String col){

color = col;

}

public double getArea(){

return height*width;

}

public double getPerimeter(){

return 2*(height + width);

}

public void getColor(){

System.out.println("Color is: " + color +" ");

return;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote