Question

java languageProblem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method:UML Class Diagram: Circle - radius: double constructor (radius: double) + area): double tostring(): String +constructor) +perUML Class Diagram: Rectangle width: double - length: double +constructor (width: double, length: double) +constructor () +areUML Class Diagranm Triangle -a: double -b: double -C: double +constructor (a: double, b: double, c: double) +constructor() +a

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 Triangle, which extends it. UML Object Diagram Shape CircleRecangleTriangle UML Class Diagram: Shape +area(): double + perimeter): double ·italicized class/method names within UML Class Diagram indicate abstract class/methods Shape Method API: Modifier and Type Method and Description abstract doublearea() Returns the area of this shape abstract double perimeter() Returns the perimeter of this shape
UML Class Diagram: Circle - radius: double constructor (radius: double) + area): double tostring(): String +constructor) +perimeter(): double Italicized class/method names within UML Class Diagram indicate abstract class/methods Circle Constructor Summary: Constructor Circle (double radius) Circle() Description Creates a Circle with given radius Creates a Circle with radius 1 SalaryEmployee Method API: Modifier and Type Method and Description double area) Returns Math.PI (radius) perimeter() Returns 2 Math.PIradius tostring() Returns the String "Circle" double String
UML Class Diagram: Rectangle width: double - length: double +constructor (width: double, length: double) +constructor () +area(): double + perimeter(): double toString(): String Italicized class/method names within UML Class Diagram indicate abstract class/methods Rectangle Constructor Summary: Constructor Rectangle (double width, double length) Creates a Rectangle with given length, width Rectangle() Description Creates a Rectangle with width 1, length 1 Rectangle Method API: Modifier and Type Method and Description double area() Returns width * length perimeter() Returns 2" (width + length) toString() Returns the String "Rectangle" double String
UML Class Diagranm Triangle -a: double -b: double -C: double +constructor (a: double, b: double, c: double) +constructor() +area(): double +perimeter() double + toString(): String Italicized class/method names within UML Class Diagram indicate abstract class/methods Triangle Constructor Summary Constructor Triangle(double a, double b, double c) Creates a Triangle with given sides a, b, c Triangle() Description Creates a Triangle with all sides- 1 Triangle Method API Modifier and Type Method and Description double area() Returns area of triangle using heron's formula perimeter() Returns a b c toString() Returns the String "Triangle" double String Tester Files: Use the TestShape.java file to test your implementation. Compare your results with the TestShape.txt file Sample Method Calls Sample Method Results Shape[] shapesnew Shape[3]; shapes [0] new Circle(3); shapes [1]new Rectangle(4,2); shapes [2]-new Triangle (1,2,3); "Circle: area: 28.3, perimeter: 18.81n" "Rectangle: area: 8.0, perimeter: 12.01n" "Triangle: area: 0.0, perimeter: 6.01n" for (Shape polygon shapes) double areapolygon.area) double perimeter- polygon.perimeter); System .out.printf("%s: area: %.elf perimeter : %.81f\n", polygon, area, perimeter)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

abstract class Shape
{
   abstract public double area(); //Method to be overridden
   abstract public double perimeter();  //Method to be overridden
}

class Circle extends Shape
{
   private double radius;
   public Circle(double radius)
   {
       this.radius=radius;
   }
   public Circle()
   {
       this.radius=0;
   }
   public double area()
   {
       return Math.PI*this.radius*this.radius;
   }
   public double perimeter()
   {
       return 2*Math.PI*this.radius;
   }
   public String toString()
   {
       return "Circle";
   }
}


class Rectangle extends Shape
{
   private double width,length;
   public Rectangle(double length,double width)
   {
       this.length=length;
       this.width=width;
   }
   public Rectangle()
   {
       this.width=0;
       this.length=0;
   }
   public double area()
   {
       return this.length*this.width;
   }
   public double perimeter()
   {
       return 2*(this.length+this.width);
   }
   public String toString()
   {
       return "Rectangle";  
   }
}


class Triangle extends Shape
{
   private double a,b,c;
   public Triangle(double a,double b,double c)
   {
       this.a=a;
       this.b=b;
       this.c=c;
   }
   public Triangle()
   {
       this.a=0;
       this.b=0;
       this.c=0;
   }
   public double area()
   {
       double s=0.5*(this.a+this.b+this.c);
       return Math.sqrt(s*(s-this.a)*(s-this.b)*(s-this.c));
   }
   public double perimeter()
   {
       return this.a+this.b+this.c;
   }
   public String toString()
   {
       return "Triangle";
   }
}

class Main
{
   public static void main(String[] args) { //Testing code
       Shape shapes[]=new Shape[3];
       shapes[0]=new Circle(3);
       shapes[1]=new Rectangle(4,2);
       shapes[2]=new Triangle(1,2,3);
       for(Shape polygon:shapes)
       {
           double area=polygon.area();
           double perimeter=polygon.perimeter();
           System.out.printf("%s: area: %.01f perimeter: %.01f\n",polygon,area,perimeter);
       }
   }
}

Sample i/o:

sushmanths-MacBook-Pro:Desktop sushmanth$ java Main Circle: area: 28.3 perimeter: 18.8 Rectangle: area: 8.0 perimeter: 12.0 T

Explanation:

Class Shape, Circle, Rectangle, Triangle is defined. The methods are and perimeter in the base class are overridden in the derived classes. Default and a parameterised constructor is given for all three derived classes. A test code is written using Main class and a sample i/o is attached for reference

Add a comment
Know the answer?
Add Answer to:
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...
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
  • Java Problem 2: Employee (10 points) (Software Design) Create an abstract class that represents an Employee and contains abstract method payment. Create concrete classes: SalaryEmployee, CommissionEm...

    Java Problem 2: Employee (10 points) (Software Design) Create an abstract class that represents an Employee and contains abstract method payment. Create concrete classes: SalaryEmployee, CommissionEmployee, HourlyEmployee which extend the Employee class and implements that abstract method. A Salary Employee has a salary, a Commision Employee has a commission rate and total sales, and Hourly Employee as an hourly rate and hours worked Software Architecture: The Employee class is the abstract super class and must be instantiated by one of...

  • Please write in Java Language Write an abstract class Shape with an attribute for the name...

    Please write in Java Language Write an abstract class Shape with an attribute for the name of the shape (you'll use this later to identify a particular shape). Include a constructor to set the name of the shape and an abstract calculateArea method that has no parameters and returns the area of the shape. Optionally, include a constructor that returns the name of the shape and its area (by calling the calculateArea method). Write a Rectangle class that inherits from...

  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. design...

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

  • (The Rectangle class) Following the example of the Circle class in Section 9.2, design a class...

    (The Rectangle class) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains: ■ Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. ■ A no-arg constructor that creates a default rectangle. ■ A constructor that creates a rectangle with the specified width and height. ■ A method named getArea()...

  • Design a general class GeometricObject can be used to model all geometric objects. This class contains...

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

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

  • Please use Java to answer the question. (The Rectangle class) Following the example of the Circle...

    Please use Java to answer the question. (The Rectangle class) Following the example of the Circle class in Section 8.2, design a class named Rectangle to represent a rectangle. The class contains: ■ Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. ■ A no-arg constructor that creates a default rectangle. ■ A constructor that creates a rectangle with the specified width...

  • NEEDS TO BE IN PYTHON: (The Rectangle class) Following the example of the Circle class in...

    NEEDS TO BE IN PYTHON: (The Rectangle class) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains: - Two data fields named width and height. - A constructor that creates a rectangle with the specified width and height. The default values are 1 and 2 for the width and height, respectively. - A method named getArea() that returns the area of this rectangle. - A method named...

  • Following the example of the Circle class in Section 8.2, design a class named Rectangle to...

    Following the example of the Circle class in Section 8.2, design a class named Rectangle to represent a rectangle. The class contains: ■ Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. ■ A no-arg constructor that creates a default rectangle. ■ A constructor that creates a rectangle with the specified width and height. ■ A method named getArea() that returns the...

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