Question

Write an interface Shape with methods getShapeName and getPerimeter. Write classes called Square, Rectangle, Circle and...

Write an interface Shape with methods getShapeName and getPerimeter. Write classes called Square, Rectangle, Circle and Triangle that implement the interface. Supply the proper parameters to the Square, Rectangle, Triangle and Circle constructors such that the perimeter can be calculated. Run THRou JDK compiler please

Sample code:

//Save this in Shape.java
public interface Shape{
        public double getPerimeter();
        public String getShapeName();
}

//Save this in Triangle.java
public class Triangle implements Shape{
        private double side1;
        private double side2;

        Triangle(double length1, double length2, double length3 ){
                side1 = length1;
                side2 = length2;
                side3 = length3;
        }

        public String getShapeName(){
                return "Triangle";
        }
        
        public double getPerimeter(){
                return side1 + side2 + side3;
        }
}


// Save this in Main.java
public class Main{
        public static void main(String[] args){
                Shape shape = new Triangle(3, 4, 5);
                System.out.println(String.valueOf(shape.getPerimeter()));
                System.out.println(shape.getShapeName());

                // Now do the same thing for the Square, Rectangle and Circle classes.

                
                // Shape circle = new Circle(PARAMS);
                // System.out.println(String.valueOf(circle.getPerimeter());
                // System.out.println(circle.getShapeName());

                // Shape square = new Circle(PARAMS);
                // System.out.println(String.valueOf(square.getPerimeter());
                // System.out.println(square.getShapeName());

                // Shape rectangle = new Circle(PARAMS);
                // System.out.println(String.valueOf(rectangle.getPerimeter());
                // System.out.println(rectangle.getShapeName());
        }
}

Homework Answers

Answer #1

Below is your code: -

Main.java

public class Main {
   public static void main(String[] args) {
       Shape shape = new Triangle(3, 4, 5);
       System.out.println(String.valueOf(shape.getPerimeter()));
       System.out.println(shape.getShapeName());

       Shape circle = new Circle(3);
       System.out.println(String.valueOf(circle.getPerimeter()));
       System.out.println(circle.getShapeName());

       Shape square = new Square(4);
       System.out.println(String.valueOf(square.getPerimeter()));
       System.out.println(square.getShapeName());

       Shape rectangle = new Rectangle(4, 5);
       System.out.println(String.valueOf(rectangle.getPerimeter()));
       System.out.println(rectangle.getShapeName());
   }
}

Triangle.java

public class Triangle implements Shape{
private double side1;
private double side2;
private double side3;
  
Triangle(double length1, double length2, double length3 ){
side1 = length1;
side2 = length2;
side3 = length3;
}

public String getShapeName(){
return "Triangle";
}
  
public double getPerimeter(){
return side1 + side2 + side3;
}
}

Shape.java

public interface Shape{
public double getPerimeter();
public String getShapeName();
}

Circle.java

public class Circle implements Shape {

   private double radius;

   Circle(double r) {
       this.radius = r;
   }

   public double getPerimeter() {
       return 2 * Math.PI * radius;
   }

   public String getShapeName() {
       return "Circle";
   }

}

Square.java

public class Square implements Shape {

   private double side;

   Square(double s) {
       this.side = s;
   }

   public double getPerimeter() {
       return 4 * side;
   }

   public String getShapeName() {
       return "Square";
   }

}

Rectangle.java

public class Rectangle implements Shape {

   private double length;
   private double breadth;

   Rectangle(double l, double b) {
       this.length = l;
       this.breadth = b;
   }

   public double getPerimeter() {
       return 2 * (this.length + this.breadth);
   }

   public String getShapeName() {
       return "Rectangle";
   }

}

Output

12.0
Triangle
18.84955592153876
Circle
16.0
Square
18.0
Rectangle

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...
I am not sure what I am doing wrong in this coding. I keep getting this...
I am not sure what I am doing wrong in this coding. I keep getting this error. I tried to change it to Circle2D.java but it still comes an error: Compiler error: class Circle2D is public, should be declared in a file named Circle2D.java public class Circle2D { public class Exercise10_11 { public static void main(String[] args) {     Circle2D c1 = new Circle2D(2, 2, 5.5);     System.out.println("area: " + c1.getArea());     System.out.println("perimeter: " + c1.getPerimeter());     System.out.println("contains(3, 3): "...
Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a...
Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a class named Triangle that extends GeometricObject. The class contains: Three double fields named side1, side2, and side3 with default values = 1.0. These denote the three sides of the triangle A no-arg constructor that creates a default triangle A constructor that creates a triangle with parameters specified for side1, side2, and side3. An accessor method for each side. (No mutator methods for the sides)...
Whenever I try to run this program a window appears with Class not Found in Main...
Whenever I try to run this program a window appears with Class not Found in Main project. Thanks in Advance. * * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Assignment10; /** * * @author goodf */ public class Assignment10{ public class SimpleGeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated;    /**...
1) add toString method to BankAccount and SavingsAccount classes 2) Override the withdraw() in SavingsAccount so...
1) add toString method to BankAccount and SavingsAccount classes 2) Override the withdraw() in SavingsAccount so that it will not withdraw more money than is currently in the account. 3) Provide constructors for SavingsAccount 4) Add this feature to SavingsAccount: If you withdraw more than 3 times you are charged $10 fee and the fee is immediately withdrawn from your account.once a fee is deducted you get another 3 free withdrawals. 5) Implement the Comparable Interface for SavingsAccount based on...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class called Student which implements Duty. Class Student adds 1 data field, id, and 2 methods, getId and setId, along with a 1-argument constructor. The duty of a Student is to study 40 hours a week. b. Write a class called Professor which implements Duty. Class Professor adds 1 data field, name, and 2 methods, getName and setName, along with a 1-argument constructor. The duty...
Hello. I have an assignment that is completed minus one thing, I can't get the resize...
Hello. I have an assignment that is completed minus one thing, I can't get the resize method in Circle class to actually resize the circle in my TestCircle claass. Can someone look and fix it? I would appreciate it! If you dont mind leaving acomment either in the answer or just // in the code on what I'm doing wrong to cause it to not work. That's all I've got. Just a simple revision and edit to make the resize...
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...
Lab: RectClass (constructor) Code in C++ This program creates a Rectangle object, then displays the rectangle's...
Lab: RectClass (constructor) Code in C++ This program creates a Rectangle object, then displays the rectangle's length, width, and area Define an overloaded constructor and use it when creating the Rectangle object instead of using the setters. Change this program to calculate and display the rectangle's perimeter. Example: In feet, how wide is your house? 20 In feet, how long is your house? 25 The house is 20.00 feet wide. The house is 25.00 feet long. The house has 500.00...
For this part, you will write a PostfixCalculator class that has methods for processing each possible...
For this part, you will write a PostfixCalculator class that has methods for processing each possible input. You will write a Tester class that reads a line of input from the user, with each symbol separated by a space, and prints out the numeric value of the top of the stack. If the user specifies an incomplete expression, print out the top of the stack and print out a message saying that the stack contains more than one item. If...