Question

Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The...

Programming Language: Java

Write a class named ColoredRectangle that extends the Rectangle class (given below). The ColoredRectangle class will have an additional private String field named Color.

The ColoredRectangle class should have four constructors: a no-arg constructor; a three-arg constructor; a two-arg constructor that accepts a Rectangle object and a color; and a copy constructor.

The ColoredRectangle class should have an Equals and toString methods.

The ColoredRectangle class should have mutators and accessors for all three fields.

public class Rectangle

public class Rectangle
{
private double length;
private double width;
public Rectangle(double len, double w)
{
length = len;
width = w;
}
public void setLength(double len)
{
length = len;
}
public void setWidth(double w)
{
width = w;
}
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
public double getArea()
{
return length * width;
}
public String toString ()
{
return “length = “ + length + “, width = “ + width ;
}
public Boolean (Rectangle other)
{
return length == other.length && width = other.width ;
}
}

Additional Instructions: In no-arg constructor, all this constructor to do is to set length and width to zero.

Write and run the following code in the demo class.

    public static void main(String[] args)
   {
       Rectangle rect1 = new Rectangle(2.0, 1.0);
       ColoredRectangle CRect1 = new ColoredRectangle();            
       ColoredRectangle CRect2 = new ColoredRectangle(4.0, 2.0, "green");
       ColoredRectangle CRect3 = new ColoredRectangle(rect1, "green");   
       ColoredRectangle CRect4 = new ColoredRectangle(CRect3);    
       System.out.println("The plain rectangle #1: " + rect1);
       System.out.println("The colored rectangle #1: " + CRect1);
       System.out.println("The colored rectangle #2: " + CRect2);
       System.out.println("The colored rectangle #3: " + CRect3);
       System.out.println("The colored rectangle #4: " + CRect4);

        if(CRect4.equals(CRect3))
           System.out.println("Colored rectangle #4 is equal to colored rectangle #3");
       else
           System.out.println("Colored rectangle #4 is NOT equals to colored rectangle #3");

        if(CRect3.equals(CRect2))
           System.out.println("Colored rectangle #3 is equal to colored rectangle #2");
       else
           System.out.println("Colored rectangle #3 is NOT equal to colored rectangle #2");
   }

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class Rectangle {
    private double length;
    private double width;

    public Rectangle(double len, double w) {
        length = len;
        width = w;
    }

    public void setLength(double len) {
        length = len;
    }

    public void setWidth(double w) {
        width = w;
    }

    public double getLength() {
        return length;
    }

    public double getWidth() {
        return width;
    }

    public double getArea() {
        return length * width;
    }

    public String toString() {
        return "length = " + length + ",width = " + width;
    }

    public Boolean equals(Rectangle other) {
        return length == other.length && width == other.width;
    }
}

class ColoredRectangle extends Rectangle {

    private String color;

    public ColoredRectangle() {
        this(1, 1);
    }

    public ColoredRectangle(double len, double w) {
        this(len, w, "white");
    }

    public ColoredRectangle(double len, double w, String color) {
        super(len, w);
        this.color = color;
    }

    public ColoredRectangle(Rectangle rectangle, String color) {
        super(rectangle.getLength(), rectangle.getWidth());
        this.color = color;
    }

    public ColoredRectangle(ColoredRectangle rectangle) {
        super(rectangle.getLength(), rectangle.getWidth());
        this.color = rectangle.color;
    }

    public String getColor() {
        return color;
    }

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

    public String toString() {
        return "length = " + getLength() + ",width = " + getWidth() + ",color = " + color;
    }

    public Boolean equals(ColoredRectangle other) {
        return super.equals(other) && color.equals(other.color);
    }
}

class RectangleTest {

    public static void main(String[] args) {
        Rectangle rect1 = new Rectangle(2.0, 1.0);
        ColoredRectangle CRect1 = new ColoredRectangle();
        ColoredRectangle CRect2 = new ColoredRectangle(4.0, 2.0, "green");
        ColoredRectangle CRect3 = new ColoredRectangle(rect1, "green");
        ColoredRectangle CRect4 = new ColoredRectangle(CRect3);
        System.out.println("The plain rectangle #1: " + rect1);
        System.out.println("The colored rectangle #1: " + CRect1);
        System.out.println("The colored rectangle #2: " + CRect2);
        System.out.println("The colored rectangle #3: " + CRect3);
        System.out.println("The colored rectangle #4: " + CRect4);

        if (CRect4.equals(CRect3))
            System.out.println("Colored rectangle #4 is equal to colored rectangle #3");
        else
            System.out.println("Colored rectangle #4 is NOT equals to colored rectangle #3");

        if (CRect3.equals(CRect2))
            System.out.println("Colored rectangle #3 is equal to colored rectangle #2");
        else
            System.out.println("Colored rectangle #3 is NOT equal to colored rectangle #2");
    }
}
Add a comment
Know the answer?
Add Answer to:
Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. design...

  • Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override...

    Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override the toString method for Rectangle. Override the equals method for Rectangle. Implement the comparable Interface for Rectangle (Compare by area) Rectangle class: public class Rectangle {       private double length;    private double width;       public Rectangle(double l, double w)    {        length = l;        width = w;    }       public double getLength()    {   ...

  • Programming: Java: Fixing errors in code help: The errors are in square.java and rectangle.java and they...

    Programming: Java: Fixing errors in code help: The errors are in square.java and rectangle.java and they both say: constructor Shape in class Shape cannot be applied to given types; required: no arguments found: String reason: actual and formal argument lists differ in length The directions say: The files Shape.java and TestArea.java have been finished already, YOU DONT ADD ANYTHING TO THESE TWO. According to the requirement, you need to modify in Square.java and Rectangle.java, respectively a. Implementing constructor with no...

  • Why is my program returning 0 for the area of a triangle? public class GeometricObjects {...

    Why is my program returning 0 for the area of a triangle? public class GeometricObjects {    private String color = " white ";     private boolean filled;     private java.util.Date dateCreated;     public GeometricObjects() {         dateCreated = new java.util.Date();     }     public GeometricObjects(String color, boolean filled) {         dateCreated = new java.util.Date();         this.color = color;         this.filled = filled;     }     public String getColor() {         return color;     }     public void setColor(String color)...

  • Consider the Rectangle2 java class definition below. Write the definition of an equals) method that checks...

    Consider the Rectangle2 java class definition below. Write the definition of an equals) method that checks if two Rectangle objects have the same dimensions Write a Java program to test the equals method public class Rectangle2 K private int width, length; public Rectangle2(int w, int 1) { setWidth(w); setLength(1); System.out.println("Inside parameterized!!!"); } public void setWidth(int w) { width = w;} public void setLength(int i) { length = 1;} int getWidth() { return width; } int getLength() { return length; }...

  • Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface....

    Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface. Couldn't figure it out. the compare to method should print 0, 1, or -1 import java.util.*; public class RectangleMain {    public static void main(String [] args) throws CloneNotSupportedException    {        /*ComparableRectangleAlsoCloneable obj1 = new ComparableRectangleAlsoCloneable(4, 5);        ComparableRectangleAlsoCloneable obj2 = (ComparableRectangleAlsoCloneable)obj1.clone();               System.out.println(obj1.toString());        System.out.println(obj1 == obj2); //false        System.out.println(obj2.toString());*/               Scanner...

  • JAVA PROGRAM USING ECLIPSE. THE FIRST IMAGE IS THE INSTRUCTIONS, THE SECOND IS THE OUTPUT TEST...

    JAVA PROGRAM USING ECLIPSE. THE FIRST IMAGE IS THE INSTRUCTIONS, THE SECOND IS THE OUTPUT TEST CASES(WHAT IM TRYING TO PRODUCE) BELOW THOSE IMAGES ARE THE .JAVA FILES THAT I HAVE CREATED. THESE ARE GeometircObject.Java,Point.java, and Tester.Java. I just need help making the Rectangle.java and Rectangle2D.java classes. GeometricObject.Java: public abstract class GeometricObject { private String color = "white"; // shape color private boolean filled; // fill status protected GeometricObject() { // POST: default shape is unfilled blue this.color = "blue";...

  • This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets...

    This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets and sets methods for the variables. Create a No-Arg constructor and a constructor that accepts all three values. At the end of the class add a main method that accepts variables from the user as input to represent the length and width of a room in feet and the price of carpeting per square foot in dollars...

  • Write a class named Octagon (Octagon.java) that extends the following abstract GeometricObject class and implements the...

    Write a class named Octagon (Octagon.java) that extends the following abstract GeometricObject class and implements the Comparable and Cloneable interfaces. //GeometricObject.java: The abstract GeometricObject class public abstract class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated; /** Construct a default geometric object */ protected GeometricObject() { dateCreated = new java.util.Date(); } /** Construct a geometric object with color and filled value */ protected GeometricObject(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color;...

  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT