In addition to the Main class, the above project contains the class Shape, which is needed for this project the Shape class contains: 1 protected instance variable to store the shape's id number. 1 private static variable to store the id number for the next object to be created (used to help each object have a unique id number) 1 protected default constructor (with no arguments) that sets the shape's id number to be unique, using the static variable to help with that. 1 public method called getId which returns the object's id 2 public methods: area and perimeter, which are really just meant to be overridden.  Later we'll learn how this can be done better with abstract methods.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

In addition to the Main class, the above project contains the class Shape, which is needed for this project the Shape class contains:

  • 1 protected instance variable to store the shape's id number.
  • 1 private static variable to store the id number for the next object to be created (used to help each object have a unique id number)
  • 1 protected default constructor (with no arguments) that sets the shape's id number to be unique, using the static variable to help with that.
  • 1 public method called getId which returns the object's id
  • 2 public methods: area and perimeter, which are really just meant to be overridden.  Later we'll learn how this can be done better with abstract methods.

Here are the 3 classes you should write followed by their details:

  1. Circle
    • Circle should be a sub-class of Shape, so it inherits all of the above plus...
    • 1 protected instance variable to store the circle's radius.
    • 1 public constructor with 1 argument to initialize the radius.
    • 2 public methods which override Shape methods: area and perimeter. (Use the @Override annotation.) They should return the area and perimeter of the circle. You can use Math.PI to get an accurate value of π.
  2. Rectangle
    • Rectangle should be a sub-class of Shape, so it inherits all of Shape's members plus...
    • 2 protected instance variables to store the rectangle's length and height.
    • 1 public constructor with 2 arguments to initialize the length and height.
    • 2 public methods: area and perimeter. (Again, use the @Override annotation.) They should return the area and perimeter of the rectangle.
  3. Square
    • Square should be a sub-class of Rectangle, so it inherits all of Rectangle's members plus...
    • 1 public constructor with 1 argument to initialize the length and height to be that value.
    • You don't need to write area or perimeter, because the inherited code from Rectangle works just fine for a square.
Test program for Assignment 10 ShapeHierarchy.
Uses Shape, Circle, Rectangle, and Square.

YOUR JOB IS TO WRITE THE Circle, Rectangle,
and Square CLASSES SO THIS PROGRAM
PRODUCES THE OUTPUT SHOWN AT BOTTOM.

DO NOT CHANGE THIS FILE
*/

class Main
{
publicstaticvoid main(String[] args)
{
// Array to store all types of Shape objects:
Shape[] shapes = new Shape[4];
// Parallel array to store text descriptions of each shape.
String[] descriptions = new String[4];

shapes[0] = new Circle(10);
descriptions[0] = "10 unit radius Circle";
shapes[1] = new Rectangle(15, 25);
descriptions[1] = "15x25 Rectangle";
shapes[2] = new Square(20);
descriptions[2] = "20 unit Square";
shapes[3] = new Circle(2);
descriptions[3] = "2 unit radius Circle";

System.out.println("This program examines the ratio of area to perimeter");
System.out.println("for a few different shapes.");
System.out.println("(The larger the ratio, the greater the efficiency of space contained)");
for(int i=0; i<shapes.length && i<descriptions.length; i++)
{
System.out.println("Shape #" + shapes[i].getId()
+ " is a " + descriptions[i] + " with ratio "
+ shapes[i].area() / shapes[i].perimeter());
}
}
}

/* Output:

This program examines the ratio of area to perimeter
for a few different shapes.
(The larger the ratio, the greater the efficiency of space contained)
Shape #1 is a 10 unit radius Circle with ratio 5.0
Shape #2 is a 15x25 Rectangle with ratio 4.6875
Shape #3 is a 20 unit Square with ratio 5.0
Shape #4 is a 2 unit radius Circle with ratio 1.0

*/
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 6 images

Blurred answer
Knowledge Booster
Unreferenced Objects
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education