Question

Write a code in Java where: Create two subclasses for the geometric objects: a circle, and...

Write a code in Java where:

Create two subclasses for the geometric objects: a circle, and a rectangle.

They must invoke the displayGeometricObject method to display the objects.

The displayGeometricObject displays the area and diameter if the object is a circle, and displays area if the object is a rectangle.

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

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();

   }

   /** Return color */

   public String getColor() {

       return color;

   }

   /** Set a new color */

   public void setColor(String color) {

       this.color = color;

   }

   /** Return filled. Since filled is boolean,

so, the get method name is isFilled */

   public boolean isFilled() {

       return filled;

   }

   /** Set a new filled */

   public void setFilled(boolean filled) {

       this.filled = filled;

   }

   /** Get dateCreated */

   public java.util.Date getDateCreated() {

       return dateCreated;

   }

   /** Return a string representation of this object */

   public String toString() {

       return "created on " + dateCreated + "\ncolor: " + color +

               " and filled: " + filled;

   }

   /** Abstract method getArea */

   public abstract double getArea();

   /** Abstract method getPerimeter */

   public abstract double getPerimeter();

}

############

public class Circle extends GeometricObject {

   private double radius;

   public Circle() {

   }

   public Circle(double radius) {

       this.radius = radius;

   }

   /** Return radius */

   public double getRadius() {

       return radius;

   }

   /** Set a new radius */

   public void setRadius(double radius) {

       this.radius = radius;

   }

   /** Return area */

   public double getArea() {

       return radius * radius * Math.PI;

   }

   /** Return diameter */

   public double getDiameter() {

       return 2 * radius;

   }

   /** Return perimeter */

   public double getPerimeter() {

       return 2 * radius * Math.PI;

   }

  

   /* Print the circle info */

   public void printCircle() {

       System.out.println("The circle is created " + getDateCreated() +

               " and the radius is " + radius);

   }

}

###########

public class Rectangle extends GeometricObject {

   private double length, width;

   public Rectangle() {

   }

   public Rectangle(double length, double width) {

       this.length = length;

       this.width = width;

   }

  

   public double getLength() {

       return length;

   }

   public double getWidth() {

       return width;

   }

   public void setLength(double length) {

       this.length = length;

   }

   public void setWidth(double width) {

       this.width = width;

   }

   /** Return area */

   public double getArea() {

       return length*width;

      

   }

  

   /** Return perimeter */

   public double getPerimeter() {

       return 2 * (length+width);

   }

   /* Print the circle info */

   public void printRectangle() {

       System.out.println("The rectangle is created " + getDateCreated() +

               " and the length is " + length+", widht: "+width);

   }

}

############

public class TestGeometricObjects {

  

   static void displayGeometricObject(GeometricObject[] objs) {

      

       for(GeometricObject obj : objs) {

          

           if(obj instanceof Circle) {

               System.out.println("Diameter: "+((Circle)obj).getDiameter());

               System.out.println("Area: "+obj.getArea());

           }else{

               System.out.println("Area: "+obj.getArea());

           }

       }

   }

  

   public static void main(String[] args) {

      

       GeometricObject[] arr = {

           new Circle(4.5), new Rectangle(4, 5), new Circle(6)  

       };

      

       displayGeometricObject(arr);

      

   }

}

/*

Sample run:;

Diameter: 9.0

Area: 63.61725123519331

Area: 20.0

Diameter: 12.0

Area: 113.09733552923255

*/

Add a comment
Know the answer?
Add Answer to:
Write a code in Java where: Create two subclasses for the geometric objects: a circle, and...
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 1. Write a class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects...

    JAVA 1. Write a class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects should have the following fields and methods: int width, int height Where width and height are the dimensions of the rectangle. Write accessor methods (i.e. getWidth(), getHeight()) that retrieve the values stored in the fields above. And mutator methods (i.e. setWidthl), setHeight() ) that change the field's values. Finally create a method called getAreal) that returns the area of the rectangle. Like we did...

  • Java Task 2: Write a program where you create an ArrayList object that can store letters....

    Java Task 2: Write a program where you create an ArrayList object that can store letters. Show how can you add a letter to the ArrayList, remove a letter from theArrayList, replace a letter in the ArrayList, insert a letter in a specific location in the ArrayList, and display all the letters in the ArrayList. Task 3: Write a program where you create an ArrayList object that can store Circle object (you created circle class in your previous PAs). Create...

  • This is in Java, thanks! d Define two subclasses as below: 1) “Advanced Java” with an...

    This is in Java, thanks! d Define two subclasses as below: 1) “Advanced Java” with an additional field called “grades_in_class” 2) “Web Technology” with an additional field called “grades_Quizzes”. Define the required constructors and getter methods in both classes. e Override the computeGrade() in both classes as below: 1. In Advanced Java, Fgrade= 40%* avg_exams+ 40%* avg_Assignments + 20* grades_in_class 2. In Web Technology, Fgrade= 30%* avg_exams+ 50%* avg_Assignments + 20*grades_Quizzes Note: Fgrade is a local variable in the computeGrade()...

  • Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance...

    Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance variables x and y that represented for the coordinates for a point. Provide constructor for initialising two instance variables. Provide set and get methods for each instance variable, Provide toString method to return formatted string for a point coordinates. 2. Create class Circle, its inheritance from Point. Provide a integer private radius instance variable. Provide constructor to initialise the center coordinates and radius for...

  • this is for java programming Please Use Comments I am not 100% sure if my code...

    this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...

  • Write a java class definition for a circle object. The object should be capable of setting...

    Write a java class definition for a circle object. The object should be capable of setting radius, and computing its area and circumference. Use this to create two Circle objects with radius 10 and 40.5, respectively. Print their areas and circumference. Here is the Java class file (Circle.java). Compile it. public class Circle{ //Instance Variables private double PI = 3.1459; private double radius; //Methods public Circle ( ) { }    //get method (Accessor Methods ) public double getRadius (...

  • Type up the GeometricObject class (code given on the back of the paper). Name this file Geometric...

    Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...

  • (NEED FULL CODE PLEASE)write a java program class to calculate and display the areas of 10 rectan...

    (NEED FULL CODE PLEASE)write a java program class to calculate and display the areas of 10 rectangles using arrays. * input validation sizes must be between 0-50 inches output to be displayed on the screen and send to an output file. *the output must appear in the following format. rectangle length width area 1    2.0    3.0    6.0 2 - - - - 10 display the rectangle with largest area. rectangle1 area= display the rectangle with smallest area. rectangle2 area=

  • Write a Java program that prompts user to select one of the five shapes(Rectangle, Square, Triangle,...

    Write a Java program that prompts user to select one of the five shapes(Rectangle, Square, Triangle, Circle, and Parallelogram), then calculate area of the shape,must have input validation and uses more of java library like math class, wrapper class, string methods, use formatted output with the printf method. Finally, create a pseudo-code statement and flowchart

  • Create a class Circle with one instance variable of type double called radius. Then define an...

    Create a class Circle with one instance variable of type double called radius. Then define an appropriate constructor that takes an initial value for the radius, get and set methods for the radius, and methods getArea and getPerimeter. Create a class RightTriangle with three instance variables of type double called base, height, and hypotenuse. Then define an appropriate constructor that takes initial values for the base and height and calculates the hypotenuse, a single set method which takes new values...

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