Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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,

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());

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Relational Theory Normal Forms And All That Jazz

Authors: Chris Date

1st Edition

1449328016, 978-1449328016

More Books

Students also viewed these Databases questions

Question

What is computer?

Answered: 1 week ago

Question

What is the most important part of any HCM Project Map and why?

Answered: 1 week ago