An abstract class doesn't have a constructor (because you cannot make an object of the abstract class). It should have at least one method, which then has to be overridden in all derived classes. Here is an example:
You are to create an abstract class called Shape, which has an abstract method called computeArea().
Derive a Circle class from Shape. (Circle is similar to your previous Circle class, but alter the name of the area method to compute Area().
Derive a Rectangle class from Shape, changing the name of its area method to computeArea()
Derive a Diamond class from Shape. This is a new class will have attributes length and width. You will need constructors, getters and setters, and a computeArea() method, and a toString method.
In main, create an ArrayList of Shapes. Create two circles, 2 rectangles, and 2 diamonds and add them to the ArrayList.
Loop through your ArrayList outputting the area of each shape.
Answer:
Please find the code below, I am also enclosing the screenshot for your reference, Here the main catch is that, as ll the classing extending the abstract class Shapes. Hence, you can create the objects of Circle, Rectangle, Diamond and assign the reference to Shapes. Therefore, based on the reference, the respective computeArea() method gets called. Please refer through the comments for understanding.
Shapes.java
abstract class Shape { // abstract class abstract void computeArea(); }
Rectangle.java
public class Rectangle extends Shape{ // This ckass is extending an abstract class Shapes @Override void computeArea() { //display the area of a rectangle System.out.println("The area of rectangle : "); } }
Circle.java
public class Circle extends Shape { // This ckass is extending an abstract class Shapes @Override void computeArea() { // Compute area of a circle System.out.println("The area of circle : "); } }
Diamond.java
import java.util.ArrayList; public class Diamond extends Shape { // This ckass is extending an abstract class Shapes // Attributes as mentioned in the assignment int length; int width; // tostring method // You can modify in the way you want @Override public String toString() { return "Diamond{" + "length=" + length + ", width=" + width + '}'; } // getters and setters public int getLength() { return length; } public void setLength(int length) { this.length = length; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } // compute area methods @Override void computeArea() { System.out.println("The area of diamond : "); } public static void main(String [] args){ // Create an arraylist ArrayList<Shape> shapes = new ArrayList<>(); // adding 2 circles to the list shapes.add(new Circle()); shapes.add(new Circle()); // adding 2 rectangles to the list shapes.add(new Rectangle()); shapes.add(new Rectangle()); // adding 2 diamonds to the list shapes.add(new Diamond()); shapes.add(new Diamond()); // traversing a list and diaplaying the area for (Shape shape : shapes){ shape.computeArea();// calling computeArea methos, wwith reference to the items present in the list } } }
Thank you,
Good Luck!!!
An abstract class doesn't have a constructor (because you cannot make an object of the abstract...
Need help to create general class Classes Data Element - Ticket Create an abstract class called Ticket with: two abstract methods called calculateTicketPrice which returns a double and getld0 which returns an int instance variables (one of them is an object of the enumerated type Format) which are common to all the subclasses of Ticket toString method, getters and setters and at least 2 constructors, one of the constructors must be the default (no-arg) constructor. . Data Element - subclasses...
Define an abstract base class SHAPE that includes protected data members for the (x,y) position of a shape, a public method to move a shape , and a public abstract method show() to output a shape .Derive subclasses for lines, circles and rectangles. Also define the class POLYLINE with SHAPE as the base class. You can represent a line as two points, a circle as a center and a radius. A rectangle as two points on diagonally opposite corners. Implement...
This assignment helps you understand Inheritance. We want to create a superclass or parent class called Shape. This shape class will have just one method to setBorderColor. Now we need 3 subclasses that extend the Shape class, Circle, Square and Rectangle. In these classes we will need to compute the area of each of the shapes and print it to the console using System.out.println You will need to take in an additional parameter in each of these subclasses -- side...
Implement a new Generic class called a ShapeBox. This class will have two instance data, a Shape object (as in the graphics Shape like Circle, Rectangle, Ellipse), and a double to store the Shape's area. The specific type of Shape will be specified via Generics so that your class will need a placeholder for the type of Shape. This placeholder must extend Shape. Additionally, ShapeBox will implement the Comparable interface requiring that you implement a compareTo method to compare this...
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...
A general shape class is shown below. Shape has a dimension. It also defines constructors, getters, setters and a toString method. class Shape{ int dimension; public Shape(){} public Shape(int newDimension){ dimension = newDimension; } public int getDimension(){ return dimension; } public void setDimension(int newDimension){ dimension = newDimension; } public String toString(){ return "Shape has dimension "+dimension; } } a. Define classes Circle and Square, which inherit from Shape class. b. Both classes must have two constructors similar to Shape class....
Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...
Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman, sophomore, junior, or senior). Define the possible status values using an enum. Staff has an office, salaray, and date-hired. Implement the above classes in Java. Provide Constructors for classes to initialize private variables. Getters and setters should only be provided if needed. Override the toString() method...
Create a program that calculates the area of various shapes. Console Specifications Create an abstract class named Shape. This class should contain virtual member function named get_area() that returns a double type. Create a class named Circle that inherits the Shape class and contains these constructors and member functions: Circle(double radius) double get_radius() void set_radius(double radius) double get_area() Create a class named Square that inherits the Shape class and contains these constructors and member functions: Square(double width) double get_width() void...
Please do this in C++. Thank you in advance. Note that you have to carefully create your class using the names and capitalization shown below. Put comments into your program, make sure you indent your program properly, and use good naming conventions. Create a class called entry. It should have two private data members of type std::string. One represents a name and the other represents an email address. Make sure you give them ,meaningful names. Create public getters and setters...