Create a class named MyRectangle to represent rectangles. The required data fiel
ID: 3653237 • 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(){ } }Explanation / Answer
Below is the code, if there is any problem with the codes, PM me before rating me.
import java.util.*;
public class MyRectangle{
private double width = 0.0;
private double height = 0.0;
private static String color = "";
public MyRectangle(){ }
public MyRectangle(double widthParam, double heightParam, String colorParam){
this.width = widthParam;
this.height = heightParam;
this.colour = colourParam;
}
public double getWidth(){
return width;
}
public void setWidth(double widthParam){
this.width = widthParam;
}
public double getHeight(){
return height;
}
public void setHeight(double heightParam){
this.height = heightParam;
}
public String getColor(){
return color;
}
public static void setColor(String colorParam){
this.colour = colorParam;
}
public double findArea(){ }
double area = height * width;
return area;
}
class Main {
public static void main(String[] args) {
double width = 0.0;
double height = 0.0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter width of first rectangle : ");
width = sc.nextDouble();
System.out.print("Enter heightof first rectangle : ");
height = sc.nextDouble();
MyRectangle rect1 = new Rectangle(width,height,red);
System.out.print("Enter width of second rectangle : ");
width = sc.nextDouble();
System.out.print("Enter heightof second rectangle : ");
height = sc.nextDouble();
MyRectangle rect2 = new Rectangle(width,height,yellow);
System.out.println();
System.out.printf("For the first rectangle, colour = %s, width = %.2f, height = %.2f, area = %.2f ",rect1.getColor(),rect1.getWidth(),rect1.getHeight(),rect1.findArea());
System.out.printf("For the second rectangle, colour = %s, width = %.2f, height = %.2f, area = %.2f ",rect2.getColor(),rect2.getWidth(),rect2.getHeight(),rect2.findArea());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.