Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Due: 7 May @ 11:59 pm* [CSE 007] Lab 11: Classes, Inheritance & Polymorphism Date Assigned: 29 April 2022 Objective: The purpose of this
Due: 7 May @ 11:59 pm* [CSE 007] Lab 11: Classes, Inheritance & Polymorphism Date Assigned: 29 April 2022 Objective: The purpose of this activity is to explore a Shape object that serves as the parent for at least 3 other shapes (i.e. classes that extend Shape or inherit visible members) in order to intentionally incorporate inheritance and polymorphism into a program. Submission: Shape.java, Rectangle.java, Triangle.java, Circle.java, ShapeTest.java *no additional extensions will be granted; This is after the last day of classes. The third pillar of object-oriented programming is polymorphism. This is the idea that given derived classes (created through the use of inheritance), we can use a reference variable of a superType to hold a reference to a subType object. This program will utilize an abstract super class. Abstract classes are like the typical concrete object classes that we have been utilizing, with a few main differences: 1. The class declaration includes the abstract modifier; Ex: a. public abstract class Shape {} 2. You cannot create an instance of an abstract class. As such, we change the constructors of this class to be protected so that only derived classes can access them through constructor chaining. a. protected Shape (String color, String name) {} 3. Although you cannot create an instance of an abstract class, you can use an abstract class as a data type to hold references of derived classes (using Polymorphism). a. Shape [] shapes = new Shape [2]; b. shapes [0] = new Circle(); c. shapes [1] = new Rectangle(); 4. Methods that will be overridden in derived classes should be declared as abstract in the parent class. a. public abstract double computeArea (); b. Note that there is no method body or curly braces, just a semicolon to end the declaration. 5. Any class with (access to) abstract methods is abstract, but a class does not have to have any abstract methods to be defined as abstract. a. This means that if a child class extends an abstract parent class, they must override those abstract methods that were previously declared in the parent class. For this assignment, you will create at least 5 classes: Shape.java, Circle.java, Triangle.java, Rectangle, and ShapeTest.java. Shape will serve as the SuperClass, from which Circle, Triangle and Rectangle will be derived. ShapeTest.java will hold the main method used to create instances of these objects and test the code that you create. public abstract class Shape {}: This class represents an abstract superclass, and includes only a few members (described below). Private Data Field(s): o private String color; o private String name; O private static int numShapes=0; //to hold the number of shape objects created Methods o Protected Constructor(s): overload the constructor method at least 2 times to initialize any combination of data fields. Note: protected means that all child classes (here, circle, rectangle and triangle) have access to the constructors but that they are not visible outside of those classes. o Getter/Setter methods: create methods to access and modify private data fields O Create computeArea () and compute Perimeter () declared as abstract methods. These methods will be overridden in the subclasses that you will define o Overridden methods: override toString (from the object class) to return the shape name and color. public class Circle{}: This is a direct child/subclass to Shape, and is described here. Private Data Field(s): o private double radius; o private static int numCircles=0; //to hold the number of circle objects created Methods o Constructor(s): overload the constructor method at least 3 times to initialize any combination of data fields from Circle and within the super class. O Getter/Setter methods: create methods to access and modify radius O Override computeArea () to calculate the area of the circle and return the value as a double As a reminder, the formula to calculate the area of a circle is: A=T*(radius) o Override compute Perimeter () to calculate the perimeter of the circle and return the value as a double As a reminder, the formula to calculate the perimeter of a circle is: P=2*T*radius public class Triangle{}: This is a direct child/subclass to Shape, and is described here. Override toString (from the Shape ass) to return super.toString() concatenated with the radius and area of the shape. Private Data Field(s): o private double base; o private double height; o private static int numTris=0; //to hold the number of triangle objects created O Methods O Constructor(s): overload the constructor method at least 3 times to initialize any combination of data fields from Triangle and within the super class. Getter/Setter methods: create methods to access and modify the private data fields O Override computeArea () to calculate the area of the triangle and return the value as a double As a reminder, the formula to calculate the area of a triangle is: A=12*base *height O Override compute Perimeter () to calculate the perimeter of the triangle and return the value as a double As a reminder, the formula to calculate the perimeter of a triangle is: P=x+y+base, where x and y are the length of the other two sides for the triangle Be sure to validate these values as positive doubles You can ask the user for input here for the length of the other two sides (since you already have the base), or you can incorporate those values as private data fields to be initialized within the constructor(s). O Override toString (from the Shape class) to return super.toString() concatenated with the base, height and area of the shape. public class Rectangle{}: This is a direct child/subclass to Shape, and is described here. Private Data Field(s): O double length; O double width; O static int numRects=0; //to hold the number of rectangle objects created Methods O Constructor(s): overload the constructor method at least 3 times to initialize any combination of data fields from Rectangle and within the super class. o Getter/Setter methods: create methods to access and modify the private data fields O Override computeArea () to calculate the area of the triangle and return the value as a double As a reminder, the formula to calculate the area of a rectangle is: A-length*width Override compute Perimeter () to calculate the perimeter of the triangle and return the value as a double As a reminder, the formula to calculate the perimeter of a triangle is: P=(2*length) + (2*width) O Override toString (from the Shape class) to return super.toString() concatenated with the base, height and area of the shape. O public class ShapeTest{}: This class will include a main method to test the Shape, Circle and Triangle classes that you previously created. The main method you create should be simple enough to test your code, but also highlight the power of polymorphism, afforded through the use of object-oriented programming and inheritance. Your program should include a modularized main class (using methods) to do the following: An array of shapes that includes references to all types of concrete shape objects Allows the user to manually create a variety in the array O Be sure to validate any input, allowing the user to indicate what shape they are creating o Only one scanner should be used per program Randomly create shapes in the array (and pass in random values to constructors) Display each object, including the perimeter and area O You should use the toString() methods that you have overridden for this O Note that you do not have to explicitly invoke the toString method within a print statement. Display the total perimeter of all objects Display the total area of all objects Display the average perimeter and area for all Shapes. Suggestion: throughout this class (and the others that you create), add comments explaining the various concepts of OOP that you are utilizing.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started