Question

write a completed program (included the main) for the Q: add an equals method to each of the Rectangle circle and triangle classes introduced in this chapter. two shapes are considered equal if their fields have equivalent values. based on

public class Circle implements Shape f private double radius; // Constructs a new circle with the given radius. public Circle

public class Rectangle implements Shape f private double width; private double height; // Constructs a new rectangle with the

// A general interface for shape classes public interface Shape f public double getArea(); public double getPerimeter

public class Triangle implements Shape f private double a; private double b; private double c; // Constructs a new Triangle g

0 0
Add a comment Improve this question Transcribed image text
Answer #0

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// Shape.java

public interface Shape {
   public double getArea();

   public double getPerimeter();

}

_________________

// Circle.java

public class Circle implements Shape {
   private double radius;

   public Circle(double radius) {
       this.radius = radius;
   }

   @Override
   public double getArea() {
       return Math.PI * radius * radius;
   }

   @Override
   public double getPerimeter() {
       return 2 * Math.PI * radius;
   }

   @Override
   public int hashCode() {
       final int prime = 31;
       int result = 1;
       long temp;
       temp = Double.doubleToLongBits(radius);
       result = prime * result + (int) (temp ^ (temp >>> 32));
       return result;
   }


   public boolean equals(Circle other) {
       if (Double.doubleToLongBits(radius) != Double
               .doubleToLongBits(other.radius))
           return false;
       return true;
   }
  
  

}

_________________________

// Rectangle.java

public class Rectangle implements Shape {
   private double width;
   private double height;

   public Rectangle(double width, double height) {
       this.width = width;
       this.height = height;
   }

   @Override
   public double getArea() {

       return width * height;
   }

   @Override
   public double getPerimeter() {
       return 2 * (width + height);
   }

   public boolean equals(Rectangle other) {
       if (Double.doubleToLongBits(height) != Double
               .doubleToLongBits(other.height))
           return false;
       if (Double.doubleToLongBits(width) != Double
               .doubleToLongBits(other.width))
           return false;
       return true;
   }

}
__________________________

// Triangle.java

public class Triangle implements Shape {
   private double a;
   private double b;
   private double c;

   public Triangle(double a, double b, double c) {
       this.a = a;
       this.b = b;
       this.c = c;
   }

   @Override
   public double getArea() {
       double p;
       double area;
       p = (a + b + c) / 2;
       area = Math.sqrt(p * (p - a) * (p - b) * (p - c));
       return area;
   }

   @Override
   public double getPerimeter() {

       return a + b + c;
   }

   @Override
   public int hashCode() {
       final int prime = 31;
       int result = 1;
       long temp;
       temp = Double.doubleToLongBits(a);
       result = prime * result + (int) (temp ^ (temp >>> 32));
       temp = Double.doubleToLongBits(b);
       result = prime * result + (int) (temp ^ (temp >>> 32));
       temp = Double.doubleToLongBits(c);
       result = prime * result + (int) (temp ^ (temp >>> 32));
       return result;
   }

   public boolean equals(Triangle other) {
       if (Double.doubleToLongBits(a) != Double.doubleToLongBits(other.a))
           return false;
       if (Double.doubleToLongBits(b) != Double.doubleToLongBits(other.b))
           return false;
       if (Double.doubleToLongBits(c) != Double.doubleToLongBits(other.c))
           return false;
       return true;
   }
  

}
____________________________

// Test.java

import java.util.ArrayList;

public class Test {

   public static void main(String[] args) {
       ArrayList<Shape> arl = new ArrayList<Shape>();
       arl.add(new Circle(5.5));
       arl.add(new Rectangle(4, 7));
       arl.add(new Triangle(3, 4, 5));
       for (int i = 0; i < arl.size(); i++) {
           if (arl.get(i) instanceof Circle) {
               System.out.println("Circle :");
               System.out.println("Area :" + arl.get(i).getArea());
               System.out.println("Perimeter :" + arl.get(i).getPerimeter());
           }
           if (arl.get(i) instanceof Rectangle) {
               System.out.println("Rectangle :");
               System.out.println("Area :" + arl.get(i).getArea());
               System.out.println("Perimeter :" + arl.get(i).getPerimeter());
           }
           if (arl.get(i) instanceof Triangle) {
               System.out.println("Triangle :");
               System.out.println("Area :" + arl.get(i).getArea());
               System.out.println("Perimeter :" + arl.get(i).getPerimeter());
           }
       }

   }

}
___________________________

Output:

Circle :
Area :95.03317777109123
Perimeter :34.55751918948772
Rectangle :
Area :28.0
Perimeter :22.0
Triangle :
Area :6.0
Perimeter :12.0


_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
write a completed program (included the main) for the Q: add an equals method to each...
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 language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • 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...

  • 1. Consider the class Circle below. Add an equals method to this class. The method should...

    1. Consider the class Circle below. Add an equals method to this class. The method should accept a Circle object as an argument. It should return true if the argument object contains the same data as the calling object, and false otherwise. public class Circle {     private double radius;     public Circle(double r)     {         radius = r;     }     public double getArea()     {         return Math.PI * radius * radius;     }     public double...

  • Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g...

    Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g GeometricObject.java GeometricObject.java:92: error: class, interface, or enum expected import java.util.Comparator; ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. 20.21 Please code using Java IDE. Please DO NOT use Toolkit. You can use a class for GeometricObject. MY IDE does not have access to import ToolKit.Circle;import. ToolKit.GeometricObject;.import ToolKit.Rectangle. Can you code this without using the ToolKit? Please show an...

  • Within NetBeans, write a Java program for EACH of the following problems. (The Rectangle class) Following...

    Within NetBeans, write a Java program for EACH of the following problems. (The Rectangle class) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains: · 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...

  • m The interface Measurable is defined as the following: /** An interface for methods that return...

    m The interface Measurable is defined as the following: /** An interface for methods that return the perimeter and area of an object. */ public interface Measurable { public double getPerimeter(); public double getArea(); } The class Rectangle implements the interface. The incomplete program is written as follows: 1 public class Rectangle 2 { private double width; private double height; public Rectangle(double w, double h) { width = w; height h; } { 3 4 5 6 7 8 9...

  • I am trying to write a Geometry.java program but Dr.Java is giving me errors and I...

    I am trying to write a Geometry.java program but Dr.Java is giving me errors and I dont know what I am doing wrong. import java.util.Scanner; /** This program demonstrates static methods */ public class Geometry { public static void main(String[] args) { int choice; // The user's choice double value = 0; // The method's return value char letter; // The user's Y or N decision double radius; // The radius of the circle double length; // The length of...

  • Define an interface named Shape with a single method named area that calculates the area of...

    Define an interface named Shape with a single method named area that calculates the area of the geometric shape:        public double area(); Next, define a class named Circle that implements Shape. The Circle class should have an instance variable for the radius, a constructor that sets the radius, an accessor and a mutator method for the radius, and an implementation of the area method. Define another class named Rectangle that implements Shape. The Rectangle class should have instance variables...

  • Design a general class GeometricObject can be used to model all geometric objects. This class contains...

    Design a general class GeometricObject can be used to model all geometric objects. This class contains the properties color and filled and their appropriate get and set methods. Assume that this class also contains toString() methods. The toString() method returns a string representation of the object. Define the Triangle, Circle, and Rectangle classes that extend the GeometricObject class. The Triangle class inherits all accessible data fields and methods from the GeometricObject class.In addition, it has three double data fields named...

  • Following the example of the Circle class in Section 8.2, design a class named Rectangle to...

    Following the example of the Circle class in Section 8.2, design a class named Rectangle to represent a rectangle. The class contains: ■ 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...

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