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
In this assignment, you will be building upon the work that you did in Lab #5A...
In this assignment, you will be building upon the work that you did in Lab #5A by expanding the original classes that you implemented to represent circles and simple polygons. Assuming that you have completed Lab #5A (and do not move on to this assignment unless you have!), copy your Circle, Rectangle, and Triangle classes from that assignment into a new NetBeans project, then make the following changes: Create a new Point class containing X and Y coordinates as its...
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): "...
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following...
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following 4 fields: int x (x-coordinate for its top left corner) int y (y coordinate for its top left corner) double height (height of the rectangle) double width (width of the rectangle) Your rectangle objects should have the following methods: public Rectangle(int newx, int newy, int newwidth, int newheight) Constructor that initializes the x-coordinate, y-coordinate for the top left corner and its width and height....
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;    /**...
java Create a program that defines a class called circle. Circle should have a member variable...
java Create a program that defines a class called circle. Circle should have a member variable called radius that is used to store the radius of the circle. Circle should also have a member method called calcArea that calculates the area of the circle using the formula area = pi*r^2. Area should NOT be stored in a member variable of circle to avoid stale data. Use the value 3.14 for PI. For now, make radius public and access it directly...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • A system is to be developed for an airport. When passengers have boarded an aircraft, a...
    asked 6 minutes ago
  • After reading Module 5 PowerPoint 1 - The Philosophy of Human Existance and Health Care Policy,...
    asked 14 minutes ago
  • 1. Do you feel play has a place in supporting literacy development in early childhood? Explain...
    asked 18 minutes ago
  • How should roles be selected for the Emergency Operations Center (EOC)?  Is seniority less important than experience?...
    asked 31 minutes ago
  • Discuss routing issues and solutions namely, count-to-infinity, split horizon, split horizon with poison reverse, and hold-down...
    asked 31 minutes ago
  • Find an optimal parenthesization of a matrix-chain product whose sequence of dimensions is〈5,10,3,12,5,50,6〉.
    asked 1 hour ago
  • Water at 60 F (density=62.4lbm/ft^3 and dynamic viscosity = 7.5x10^-4 lbm/ft-s) is to be pumped through...
    asked 1 hour ago
  • ibuprofen an aspirin substitute has the following percent composition C, 75.69%; H,8.80%; O,15.51%. determine the empirical...
    asked 1 hour ago
  • Describe in brief some of the aspects of understanding words in the study of language
    asked 1 hour ago
  • Anticipated sales for Safety Grip Company were 75,000 passenger car tires and 23,000 truck tires. Rubber...
    asked 1 hour ago
  • QUESTION ONE a) Differentiate between traceable and common costs b) Assume that you are living in...
    asked 1 hour ago
  • How do do you think that online course is different than the regular classroom setting experience
    asked 1 hour ago