Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Class Hierarchy Practice Exercise The purpose of this exercise is to practice cr

ID: 3782176 • Letter: C

Question

Class Hierarchy Practice Exercise

The purpose of this exercise is to practice creating a set of classes that are related on a class hierarchy. The classes should represent a set of relationships you might see in real-world objects. An example is given below:

Part 1 Class Design

You should not use this group of classes! Your job is to discover a group of is-a relationships for your own class hierarchy. You will need to fill in the names of the classes in the following hierarcy:

Some ideas you might use include:

types of sports (team sport, individual sport)

programmers (web programmers, software programmers)

insects (beetles, ants)

fish (fresh water, salt water)

movies (horror, comedy)

pick something you like!

You must choose an object for each class in the above diagram. Each parent to child relationship should be an is-a relationship that makes sense with the real world counterparts of your objects.

Part 2 Writing Your Classes

Fields:

For each of your classes, you need to add at least one field of data.

This data should be appropriate for the class that contains the data (ie. don’t store a “beachBallSize” field in a “ladder” class).

Constructors:

Each of your classes should have one parameterized constructor to pass in any data for that object and its parent class.

No default constructors are allowed! You will be severly penalized if you ignore this rule!

Methods:

Add getters and setters for each of your fields.

Add a toString() method to each of your classes.

Your toString() methods should not be redundant! Call super.toString() to print out any data that you have inherited.

Driver Class

You are responsible for creating a driver class called “ObjectTest.”

This class should allow a user to create an object for any of the classes in your hierarchy that don’t have a child class.

In my example above, this would be the Deer, PolarBear and Lion classes.

You should then request each piece of data stored in an object based on that class from the Java console. This includes any data stored in the parent classes they inherit from.

Use the values provided by your user and create an instance of the appropriate class.

Print out the toString() value of your object to the Java console.

Explanation / Answer

Heirarchy used:

Shape
TwoDimentionalShape ThreeDimentionalShape
Circle Rectangle Cylinder

public class Shape {

   private String color;

private String shapeType;

   public Shape(String color, String shapeType) {

       super();

       this.color = color;

       this.shapeType = shapeType;

   }

   public String getColor() {

       return color;

   }

   public void setColor(String color) {

       this.color = color;

   }

   public String getShapeType() {

       return shapeType;

   }

   public void setShapeType(String shapeType) {

       this.shapeType = shapeType;

   }

  

   @Override

   public String toString() {

       return "Shape [type: "+this.shapeType+", color: "+this.color+"]";

   }

  

}

public class TwoDimentionalShape extends Shape{

  

   private String shapeName;

  

   private boolean circular;

  

   public TwoDimentionalShape(String color, String shapeName, boolean isCircular) {

       super(color, "Two Dimentional Shape");

       this.shapeName = shapeName;

       this.circular = isCircular;

   }

  

   public String getShapeName() {

       return shapeName;

   }

   public void setShapeName(String shapeName) {

       this.shapeName = shapeName;

   }

   public boolean isCircular() {

       return circular;

   }

   public void setCircular(boolean circular) {

       this.circular = circular;

   }

  

  

}

public class ThreeDimentionalShape extends Shape {

   private String shapeName;

  

   private boolean spherical;

  

   public ThreeDimentionalShape(String color, String shapeName, boolean isSpherical) {

       super(color, "Three Dimentional Shape");

       this.shapeName = shapeName;

       this.spherical = isSpherical;

   }

  

   public String getShapeName() {

       return shapeName;

   }

   public void setShapeName(String shapeName) {

       this.shapeName = shapeName;

   }

   public boolean isSpherical() {

       return spherical;

   }

   public void setSpherical(boolean spherical) {

       this.spherical = spherical;

   }   

}

public class Circle extends TwoDimentionalShape {

  

   private int radius;

   public Circle(String color, int radius) {

       super(color, "Circle", true);

       this.radius = radius;

   }

   public int getRadius() {

       return radius;

   }

   public void setRadius(int radius) {

       this.radius = radius;

   }

  

   @Override

   public String toString() {

       return "[ shapeName: "+this.getShapeName()+", color: "+this.getColor()+","

               + " shapeType: "+this.getShapeType()+", radius: "+this.radius+", isCircular: "+this.isCircular()+"]";

   }   

}

public class Rectangle extends TwoDimentionalShape {

  

   private int length;

   private int breadth;

  

   public Rectangle(String color, int length, int breadth) {

       super(color, "Rectangle", false);

       this.length = length;

       this.breadth = breadth;

   }

   public int getLength() {

       return length;

   }

   public void setLength(int length) {

       this.length = length;

   }

   public int getBreadth() {

       return breadth;

   }

   public void setBreadth(int breadth) {

       this.breadth = breadth;

   }

  

   @Override

   public String toString() {

       return "[ shapeName: "+this.getShapeName()+", color: "+this.getColor()+","

               + " shapeType: "+this.getShapeType()+", length: "+this.length+","

                       + "breadth: "+this.breadth+", isCircular: "+this.isCircular()+"]";

   }

}

public class Cylinder extends ThreeDimentionalShape {

  

   private int radius;

   private int height;

  

   public Cylinder(String color, int radius, int height) {

       super(color, "Circle", true);

       this.radius = radius;

       this.height = height;

   }

   public int getRadius() {

       return radius;

   }

   public void setRadius(int radius) {

       this.radius = radius;

   }

  

  

   public int getHeight() {

       return height;

   }

   public void setHeight(int height) {

       this.height = height;

   }

   @Override

   public String toString() {

       return "[ shapeName: "+this.getShapeName()+", color: "+this.getColor()+","

               + " shapeType: "+this.getShapeType()+", radius: "+this.radius+","

                       + " height: "+this.height+", isSpherical: "+this.isSpherical()+"]";

   }

  

}

package org.learning.hierarchy;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class ObjectTest {

   public static void main(String[] args) throws NumberFormatException, IOException {

       BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

       System.out.println("Please Select the following to be Created");

       System.out.println("Shape");

       System.out.println("Two Dimentional Shape");

       System.out.println("1. Rectangle (choice num 1)");

       System.out.println("2. Circle (choice num 2)");

       System.out.println("Three Dimentional Shape");

       System.out.println("1. Cylinder (choice num 3)");

       int choice = Integer.parseInt(bufferedReader.readLine());

       switch (choice) {

       case 1:

           System.out.println("Enter Length: ");

           int length = Integer.parseInt(bufferedReader.readLine());

           System.out.println("Enter Breadth: ");

           int breadth = Integer.parseInt(bufferedReader.readLine());

           System.out.println("Enter Color: ");

           String rectColor = bufferedReader.readLine();

           Rectangle rectangle = new Rectangle(rectColor, length, breadth);

           System.out.println(rectangle);

           break;

       case 2:

           System.out.println("Enter Radius: ");

           int circleRadius = Integer.parseInt(bufferedReader.readLine());

           System.out.println("Enter Color: ");

           String circleColor = bufferedReader.readLine();

           Circle circle = new Circle(circleColor, circleRadius);

           System.out.println(circle);

           break;

       case 3:

           System.out.print("Enter Radius: ");

           int cylinderRadius = Integer.parseInt(bufferedReader.readLine());

           System.out.print("Enter Height: ");

           int height = Integer.parseInt(bufferedReader.readLine());

           System.out.print("Enter Color: ");

           String color = bufferedReader.readLine();

           Cylinder cylinder = new Cylinder(color, cylinderRadius, height);

           System.out.println(cylinder);

           break;

       default:

           System.out.println("Enter a valid value");

           break;

       }

   }

}