This is the seventh times i have been asking this question, but i got code every
ID: 3647498 • Letter: T
Question
This is the seventh times i have been asking this question, but i got code everytime that does not run.I want someting i can compile and run, not only the code.Also, i come up with something but, it gives me same yellow color both time, i need Red in first and yello in second.Please do reply this if your code can compile and run.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(){
}
}
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.
Explanation / Answer
please rate - thanks
it's because color is static, which I believe was the way it was defined in the original post
the outline given in class was incorrect
the static says that the variable color is associated with the class, not a specific instance of that class, so all the color will be the last value, which is yellow
class MyRectangle
{
private double width = 1.0;
private double height = 1.0;
private String color="white";
public MyRectangle()
{
width = 1.0;
height = 1.0;
}
public MyRectangle(double widthParam,double heightParam, String colorParam)
{
setWidth(widthParam);
setHeight(heightParam);
setColor(colorParam);
}
public double getWidth()
{
return width;
}
public void setWidth(double widthParam)
{
width = widthParam;
}
public double getHeight()
{
return height;
}
public void setHeight(double heightParam)
{
height = heightParam;
}
public String getColor()
{
return color;
}
public void setColor(String colorParam)
{
color = colorParam;
}
public double findArea()
{
return getWidth()*getHeight();
}
public static void main(String [] args){
MyRectangle r1 = new MyRectangle(1.5,2,"Red");
MyRectangle r2 = new MyRectangle(3,5.5,"yellow");
print(r1,1);
print (r2,2);
}
public static void print(MyRectangle r, int n)
{System.out.println("Rectangle "+n);
System.out.println("height: "+r.getHeight());
System.out.println("width: "+r.getWidth());
System.out.println("color: "+r.getColor());
System.out.println("area: "+r.findArea()+" ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.