Here's the code for all the shapes as well as the shape abstract class and a tester class to demonstrate all the shape classes.
Shape.java (Base abstract class for all shapes):
abstract class Shape
{
protected String color;
protected boolean filled;
Shape(String color, boolean filled)
{
this.color = color;
this.filled = filled;
}
public String toString()
{
return "[color=" + this.color + ", filled=" + this.filled + "]";
}
// abstract methods to be implemented by extending classes.
public abstract double perimeter();
public abstract double area();
// setters and getters from here on.
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
}
Triangle.java:
class Triangle extends Shape
{
private double side1;
private double side2;
private double side3;
// constructor
Triangle(String color, boolean filled, double side1, double side2, double side3)
{
// calling super constructor
super(color, filled);
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
@Override
public double perimeter()
{
return side1 + side2 + side3;
}
@Override
public double area()
{
return (side2/2.0) * side1;
}
@Override
public String toString()
{
return "Triangle:" + super.toString() + "[side1=" + this.side1 + ", side2=" + this.side2 + ", side3=" + this.side3 + "]";
}
public double getSide1() {
return side1;
}
public void setSide1(double side1) {
this.side1 = side1;
}
public double getSide2() {
return side2;
}
public void setSide2(double side2) {
this.side2 = side2;
}
public double getSide3() {
return side3;
}
public void setSide3(double side3) {
this.side3 = side3;
}
}
Rectangle.java:
class Rectangle extends Shape
{
private double length;
private double width;
// constructor
Rectangle(String color, boolean filled, double length, double width)
{
// calling super constructor
super(color, filled);
this.length = length;
this.width = width;
}
@Override
public double perimeter()
{
return 2.0 * (length + width);
}
@Override
public double area()
{
return length * width;
}
@Override
public String toString()
{
return "Rectangle:" + super.toString() + "[length=" + length + ", width=" + width + "]";
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
}
Circle.java:
class Circle extends Shape
{
private double radius;
// constructor
Circle(String color, boolean filled, double radius)
{
// calling super constructor
super(color, filled);
this.radius = radius;
}
@Override
public double perimeter()
{
return 2.0 * Math.PI * radius;
}
@Override
public double area()
{
return Math.PI * Math.pow(radius, 2);
}
@Override
public String toString()
{
return "Circle:" + super.toString() + "[radius=" + this.radius + "]";
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
Tester.java (driver/tester class):
package shapes;
import java.util.ArrayList;
public class Tester
{
public static void main(String args[])
{
ArrayList<Shape> shapes = new ArrayList<Shape>();
// add triangle
shapes.add(new Triangle("white", true, 3.0, 2.5, 2.0));
// add rectangle.
shapes.add(new Rectangle("red", true, 2.0, 4.0));
// add circle.
shapes.add(new Circle("yellow", false, 1.0));
// print all shapes.
System.out.println("Printing all shapes");
for(Shape shape : shapes)
{
// will implicitly call toString()
System.out.println(shape);
System.out.println("Perimeter is: " + shape.perimeter());
System.out.println("Area is: " + shape.area());
}
// change the triangle filled to false.
shapes.get(0).setFilled(false);
// set the rectange color to blue.
shapes.get(1).setColor("blue");
// set the cirle color to black and change the filled to false.
shapes.get(2).setColor("black");
shapes.get(2).setFilled(false);
System.out.println("Printing all shapes after modifications");
for(Shape shape : shapes)
{
// will implicitly call toString()
System.out.println(shape);
}
}
}
Sample Output from tester class:
![terminated> Tester (1) [Java Application] /usr/lib/jvm/java-11-openjdk-amd64/bin/java Printing all shapes Triangle: [color-wh](http://img.homeworklib.com/questions/85e53d60-c98b-11eb-a803-9f4ecec3c078.png?x-oss-process=image/resize,w_560)
Purpose: The purpose of this lab is for you to design and implement several classes that...
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...
Design a general class GeometricObject can be used to model all geometric objects. This class contains the properties color and filled and their appropriate get and set methods. Assume that this class also contains toString() methods. The toString() method returns a string representation of the object. Define the Triangle, Circle, and Rectangle classes that extend the GeometricObject class. The Triangle class inherits all accessible data fields and methods from the GeometricObject class.In addition, it has three double data fields named...
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...
(Need to complete the methods and pass a Junit test i can email them to you.) public class Triangle implements Comparable { /** * Stores the number of objects instantiated from the {@code Triangle} class */ private static int numTriangles; /** * The shortest side of the triangle */ private final double a; /** * The side of medium length of the triangle */ private final double b;...
Java Lab
In this lab, you will be implementing the following class hierarchy. Your concrete classes must call the superclass constructor with the proper number of sides (which is constant for each concrete shape). The perimeter method is implemented in the superclass as it does not change based on the number of sides. The area method must be overridden in each subclass as the area is dependent on the type of polygon. The areas of an equilateral triangle, square, and...
Project 1 – Classes and Top-down Design Overview Software development projects using the object-oriented approach involve breaking the problem down into multiple classes that can be tied together into a single solution. In this project, you are given the task of writing some classes that would work together for providing a solution to a problem involving some basic computations. Learning Objectives The focus of this assignment is on the following learning objectives: • Be able to identify the contents of...
Page 1/2 ECE25100 Object Oriented Programming Lab 8: Inheritance Description: The purpose of this lab is to practice inheritance. To get credit for the lab, you need to demonstrate to the student helper that you have completed all the requirements. Question 1: Consider the following detailed inheritance hierarchy diagram in Fig. 1. 1) The Person.java constructor has two String parameters, a first name and a last name. The constructor initializes the email address to the first letter of the first...
Create a Bitbucket private repository. Name it “CUS1156_Lab2”. Clone it in your local java Eclipse workspace. You will add the java files after you have completed them in Part 1 and Part 2 to this location. Part 1: Abstract classes From class, an abstract class represents some generic concept. Subclasses then provide their own specific implementations of any abstract methods of the abstract class. 1. Implement an abstract class called Shape it was discussed in the lecture. Include an abstract...
IN JAVA Overview In this project you will implement a Java program that will print several shapes and patterns according to uses input. This program will allow the use to select the type (say, rectangle, triangle, or diamond), the size and the fill character for a shape. All operations will be performed based on the user input which will respond to a dynamic menu that will be presented. Specifically, the menu will guide the user to decide between different forms...
****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...