Create a class named MyRectangle to represent rectangles. The required data fiel
ID: 3623777 • 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;
private double height = 1;
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(){
}
}
Explanation / Answer
publicclass MyRectangle
{
privatedouble width = 1.0;
privatedouble height = 1.0;
privatestatic String color ="white";
publicMyRectangle()
{
width = 1.0;
height = 1.0;
}
publicMyRectangle(double widthParam,double heightParam, String colorParam)
{
setWidth(widthParam);
setHeight(heightParam);
setColor(colorParam);
}
publicdouble getWidth()
{
return width;
}
publicvoid setWidth(double widthParam)
{
width = widthParam;
}
publicdouble getHeight()
{
return height;
}
publicvoid setHeight(double heightParam)
{
height = heightParam;
}
publicString getColor()
{
return color;
}
publicstatic void setColor(StringcolorParam)
{
color = colorParam;
}
publicdouble findArea()
{
return getWidth()*getHeight();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.