Question

Create a new package in assignment called shapes. All work for this part should be done...

  • Create a new package in assignment called shapes.
  • All work for this part should be done in the shapespackage.
  • Create a new interface called Shape.
  • Add double area() and double perimter()methods to the Shape interface.
  • Create two classes called Circle and Rectanglewhich implement Shape.
  • Add reasonable fields to the Circle and Rectangle classes which will allow you to calculate the area and perimeter of each shape.
  • Add constructors to Circle and Rectangle so you can initialize your fields.
  • Implement the area() and perimeter() methods for Circle and Rectangle.
  • Create an interface called Shape3D.
  • Add double volume() method to Shape3D.
  • Create a class called Cyclinder which extends Circle and implements Shape3D.
  • Add and initialize any required fields that you may need to calculate a cylinder's volume.
  • Keep in mind that the Cylinder class will have access to its parent (Circle) class' methods. Use this to your advantage to implement the volume()method.
  • Create a class named ShapeRunner with the following main method:
  • public static void main(String[] args) {
  • Shape circle = new Circle(2.0);
  • Shape rectangle = new Rectangle(4.0, 2.0);
  • Shape3D cylinder = new Cylinder(4.0, 1.0);
  • System.out.println("Circle:");
  • System.out.println("Area: " + circle.area() + ". Perimeter: " + circle.perimeter());
  • System.out.println("\nRectangle:");
  • System.out.println("Area: " + rectangle.area() + ". Perimeter: " + rectangle.perimeter());
  • System.out.println("\nCylinder:");
  • System.out.println("Volume: " + cylinder.volume());
  • }
  • Notice how the shapes are declared with their interfaces instead of their concrete types. This allows for more flexibility when trying to extend an application, but also makes sure that you are able to only access methods on the interface's contract. Feel free to play around with different values to test your implementations, but when submitting your final assignment, make sure to leave this main() method as written here.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/**
*
*/
package shapes;

/**
*
*
*/
public interface Shape {
  
   public double area();
   public double perimeter();

}


-----------------------------------
/**
*
*/
package shapes;

/**
*
*
*/
public class Circle implements Shape{
  
   protected double radious;
  
  

   /**
   * @param radious
   */
   public Circle(double radious) {
       super();
       this.radious = radious;
   }

   @Override
   public double area() {
       // Return areas
       return 3.14 * radious * radious;
   }

   @Override
   public double perimeter() {
       // return Perimeter
       return 2 * 3.14 * radious;
   }

}
-----------------------
/**
*
*/
package shapes;

/**
*
*
*/
public class Rectangle implements Shape{
  
   private double lenght;
   private double width;
  
  

   /**
   * @param lenght
   * @param width
   */
   public Rectangle(double lenght, double width) {
       super();
       this.lenght = lenght;
       this.width = width;
   }

   /**
   * @return the lenght
   */
   public double getLenght() {
       return lenght;
   }

   /**
   * @param lenght the lenght to set
   */
   public void setLenght(double lenght) {
       this.lenght = lenght;
   }

   /**
   * @return the width
   */
   public double getWidth() {
       return width;
   }

   /**
   * @param width the width to set
   */
   public void setWidth(double width) {
       this.width = width;
   }

   @Override
   public double area() {
       // return area
       return lenght * width;
   }

   @Override
   public double perimeter() {
       // return perimeter
       return 2 * (lenght + width);
   }

}
--------------------
package shapes;

public interface Shape3D {
   public double volume();
}

-------------------------------
package shapes;

public class Cylinder extends Circle implements Shape3D{
  
  
   private double height;
   /**
   * @param radious
   * @param radious2
   * @param height
   */
   public Cylinder(double radious, double height) {
       super(radious);
      
       this.height = height;
   }
   @Override
   public double volume() {
       // Return volume
       return this.area() * height;
   }
  
  
  
}
-------------------------
package shapes;

public class ShapeRunner {

   public static void main(String[] args) {
       //Create Circle object
       Shape circle = new Circle(2.0);
       //Create REctangle oject
       Shape rectangle = new Rectangle(4.0, 2.0);
       //Create Clynder object
       Shape3D cylinder = new Cylinder(4.0, 1.0);
       //Call methods to calculate value
       System.out.println("Circle:");
       System.out.println("Area: " + circle.area() + ". Perimeter: " + circle.perimeter());
       System.out.println("\nRectangle:");
       System.out.println("Area: " + rectangle.area() + ". Perimeter: " + rectangle.perimeter());
       System.out.println("\nCylinder:");
       System.out.println("Volume: " + cylinder.volume());
   }

}

---------------------
Output
Circle:
Area: 12.56. Perimeter: 12.56

Rectangle:
Area: 8.0. Perimeter: 12.0

Cylinder:
Volume: 50.24

Add a comment
Know the answer?
Add Answer to:
Create a new package in assignment called shapes. All work for this part should be done...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This assignment helps you understand Inheritance. We want to create a superclass or parent class called...

    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...

  • Q2) Interface Create a program that calculates the perimeter and the area of any given 2D...

    Q2) Interface Create a program that calculates the perimeter and the area of any given 2D shape. The program should dynamically assign the appropriate calculation for the given shape. The type of shapes are the following: • Quadrilateral 0 Square . Perimeter: 4xL • Area:LXL O Rectangle • Perimeter: 2(L+W) • Area:LxW Circle Circumference: I x Diameter (TT = 3.14) Area: (TT xD')/4 Triangle (assume right triangle) o Perimeter: a+b+c O Area: 0.5 x base x height (hint: the base...

  • Using Python Write the following well-documented (commented) program. Create a class called Shape that has a...

    Using Python Write the following well-documented (commented) program. Create a class called Shape that has a method for printing the area and the perimeter. Create three classes (Square, Rectangle, and Circle) which inherit from it. Square will have one instance variable for the length. Rectangle will have two instance variables for the width and height. Circle will have one instance variable for the radius. The three classes will have methods for computing the area and perimeter of its corresponding shape....

  • 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, Rectang...

    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...

  • write a completed program (included the main) for the Q: add an equals method to each...

    write a completed program (included the main) for the Q: add an equals method to each of the Rectangle circle and triangle classes introduced in this chapter. two shapes are considered equal if their fields have equivalent values. based on public class Circle implements Shape f private double radius; // Constructs a new circle with the given radius. public Circle (double radius) f this.radius - radius; // Returns the area of this circle. public double getArea) return Math.PI *radius *...

  • Language is JAVA. Clarification for the Shape class (highlighted): The following requirements specify what fields you...

    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...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****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...

  • in C++ use Inheritance for the following Shape Circle Sphere Cylinder Rectangle Square Cube You must define one class for each shape. And, use public inheritance when deriving from base cla...

    in C++ use Inheritance for the following Shape Circle Sphere Cylinder Rectangle Square Cube You must define one class for each shape. And, use public inheritance when deriving from base classes. Circle int r; void area(); void perimeter(); void volume(); //No volume for circle Sphere int r; void area();//Sphere surface area= 4 × pi × radius2 void perimeter(); //No perimeter for Sphere void volume();//Sphere volume= 4/3 × pi × radius2 Cylinder int r, height; void area();// Cylinder surface area=perimeter of...

  • Use Python 3 Create a program that uses Turtle to draw shapes. Show the following menu:...

    Use Python 3 Create a program that uses Turtle to draw shapes. Show the following menu: Enter Circle Enter Rectangle Remove Shape Draw Shapes Exit Circles – User inputs position, radius, and color. The position is the CENTER of the circle Rectangles – User inputs position, height, width, color. The position is the lower left-hand corner Colors – Allow red, yellow, blue, and green only Remove – Show the number of items in the list and let the user enter...

  • I need help for part B and C 1) Create a new project in NetBeans called...

    I need help for part B and C 1) Create a new project in NetBeans called Lab6Inheritance. Add a new Java class to the project called Person. 2) The UML class diagram for Person is as follows: Person - name: String - id: int + Person( String name, int id) + getName(): String + getido: int + display(): void 3) Add fields to Person class. 4) Add the constructor and the getters to person class. 5) Add the display() method,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT