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:
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...
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...
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...
JAVA: Quadrilateral Inheritance Hierarchy Write an inheritance hierarchy for classes Quadrilateral, Parallelogram, Rectangle, and Square. Use Quadrilateral as the superclass of the hierarchy. Create and use a Point class to represent the points in each shape. Make the hierarchy as deep as possible, i.e. more than two levels. Specify the instance variables and methods for each class. The private instance variables of Quadrilateral should be the x-y coordinate pairs for the four endpoints of the Quadrilateral. A program that instantiates...
****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...
Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles. You will need to define a Triangle class containing a single instance variable, representing the length of one of the triangle’s sides. Have the program create circles, rectangles, and triangles with equal probability. Circle and Rectangle is done, please comment on your methods so i can understand */ package NervousShapes; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class...
Language is JAVA. Clarification for the Shape class (highlighted): The following requirements specify what fields you are expected to implement in your Shape class; - A private String color that specifies the color of the shape - A private boolean filled that specifies whether the shape is filled - A private date (java.util.date) field dateCreated that specifies the date the shape was created Your Shape class will provide the following constructors; - A two-arg constructor that creates a shape with...
Lab 7 Add three instance variables to your class RightTriangle from lab 5. The first two are of type int and are called xLoc and yLoc and the third is of type int called ID. Also add a static variable of type double called scaleFactor. This should be initialized to a default value of 1. Update the constructor to set the three new instance variables and add appropriate get and set methods for the four new variables. All set methods...