Create a set of classes of Shapes (triangle, quadrilateral, circle) with the fol
ID: 3876571 • Letter: C
Question
Create a set of classes of Shapes (triangle, quadrilateral, circle) with the following inheritance relationship. Shapes.jpg Declare and define the classes as Shapes-- Abstract base class. Triangle -- Abstract derived class with base class as Shapes IsoscelesTriangle -- derived class with base class as Triangle.(Attribute with two sides) EquilateralTriangle -- derived class with base class as Triangle(Attribute with one side) Quadrilateral --Abstract derived class with base class as Shapes. Rectangle -- derived class with base class as Quadrilateral.(Attribute with two sides-- length and breadth) Square -- derived class with base class as Quadrilateral.(Attribute with one side) Rhombus -- derived class with base class as Quadrilateral. (Attribute with two sides-- length and breadth) Circle -- Derived class with base class as Shapes. (Attribute with radius) Display a Menu Interface to the user to Create an instance of the Object. The Menu should be like. Create an Instance of Triangle Create IsoscelesTriangle Create EquilateralTriangle Quadrilateral Create Rectangle Create Square Create Rhombus Create Circle Create should create an instance of the derived class selected and then call the getData() function of that class and store that instance in the array of Shapes(Base Class). You can add constructors for the derived classes with default values. Write a test main function to take the input from the user for all the shapes and store them in a array, then print the area and perimeter of the class. Refer: http://www.math.com/tables/geometry/index.htm All the measurements is in Centimeters
Explanation / Answer
Shapes.java
public abstract class Shapes {
}
Triangle.java
public abstract class Triangle extends Shapes {
}
IsoscelesTriangle.java
public class IsoscelesTriangle extends Triangle {
double side1;
double side2;
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 IsoscelesTriangle(double side1, double side2) {
super();
this.side1 = side1;
this.side2 = side2;
}
public double perimeter()
{
return 2*side1+side2;
}
public double area()
{
return side1*side2/2;
}
}
EquilateralTriangle.java
public class EquilateralTriangle extends Triangle {
double side;
public EquilateralTriangle(double side) {
super();
this.side = side;
}
public double getSide() {
return side;
}
public void setSide(double side) {
this.side = side;
}
public double perimeter()
{
return 3*side;
}
public double area()
{
return 1.73205*side*side/4;
}
}
Quadrilateral.java
public abstract class Quadrilateral extends Shapes {
}
Rectangle.java
public class Rectangle extends Quadrilateral {
double length;
double breadth;
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getBreadth() {
return breadth;
}
public void setBreadth(double breadth) {
this.breadth = breadth;
}
public Rectangle(double length, double breadth) {
super();
this.length = length;
this.breadth = breadth;
}
public double perimeter()
{
return 2*(length+breadth);
}
public double area()
{
return length*breadth;
}
}
Square.java
public class Square extends Quadrilateral {
double side;
public Square(double side) {
super();
this.side = side;
}
public double getSide() {
return side;
}
public void setSide(double side) {
this.side = side;
}
public double perimeter()
{
return 4*side;
}
public double area()
{
return side*side;
}
}
Rhombus.java
public class Rhombus extends Quadrilateral {
double length;
double breadth;
public Rhombus(double length, double breadth) {
super();
this.length = length;
this.breadth = breadth;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getBreadth() {
return breadth;
}
public void setBreadth(double breadth) {
this.breadth = breadth;
}
public double perimeter()
{
return 2*(length+breadth);
}
public double area()
{
return length*breadth/2;
}
}
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("Enter your choice");
System.out.println("1. Create IsoscelesTriangle ");
System.out.println("2. Create EquilateralTriangle");
System.out.println("3. Create Rectangle");
System.out.println("4. Create Square");
System.out.println("5. Create Rhombus");
System.out.println("6. Create Circle");
int choice=sc.nextInt();
switch(choice)
{
case 1:
System.out.println("Enter two sides of triangle");
double side1=sc.nextDouble();
double side2=sc.nextDouble();
IsoscelesTriangle isosceles=new IsoscelesTriangle(side1, side2);
System.out.println("Area of the triangle is "+isosceles.area());
System.out.println("Perimeter of the triangle is "+ isosceles.perimeter());
break;
case 2:
System.out.println("Enter side of the triangle");
double side=sc.nextDouble();
EquilateralTriangle equilateralTriangle=new EquilateralTriangle(side);
System.out.println("Area of the triangle is "+equilateralTriangle.area());
System.out.println("Perimeter of the triangle is "+ equilateralTriangle.perimeter());
break;
case 3:
System.out.println("Enter length and breadth");
double length=sc.nextDouble();
double breadth=sc.nextDouble();
Rectangle rectangle =new Rectangle(length, breadth);
System.out.println("Area of rectangle is "+rectangle.area());
System.out.println("Perimeter of the rectangle is "+rectangle.perimeter());
break;
case 4:
System.out.println("Enter side of the square");
double squareside=sc.nextDouble();
Square square=new Square(squareside);
System.out.println("area of the square is "+square.area());
System.out.println("Perimeter of the square is "+square.perimeter());
break;
case 5:
System.out.println("Enter length and breadth of rhombus");
double rlength=sc.nextDouble();
double rbreadth=sc.nextDouble();
Rhombus rhombus=new Rhombus(rlength, rbreadth);
System.out.println("area of the rhombus is "+rhombus.area());
System.out.println("Perimeter of the rhombus is "+rhombus.perimeter());
break;
case 6:
System.out.println("Enter radius of circle");
double radius=sc.nextDouble();
Circle circle=new Circle(radius);
System.out.println("area of the circle is "+circle.area());
System.out.println("Perimeter of the circle is "+circle.perimeter());
break;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.