Use eclipse program java and write the following programs. 1.The program should
ID: 3592225 • Letter: U
Question
Use eclipse program java and write the following programs.
1.The program should print out the color and calculate the perimeter and area of three types of shapes. Define an abstract shape class, a Triangle class, a Rectangle class and a Circle Class; calculate the result in a main method. You need to utilize abstract class and abstract methods to define the common fields and methods. The program should utilize polymorphism. Observe if you initialize an abstract class.
2.Utilize interface and write the same Shape program. Print out the color, perimeter and area of the three shapes. Observe the difference between interface and abstract class (from question 1). Observe if you can initialize an interface.
Explanation / Answer
Shape.java
public abstract class Shape {
// Declaring instance variables
private String color;
// Parameterized constructor
public Shape(String color) {
super();
this.color = color;
}
// Getters
public String getColor() {
return color;
}
// Declaring abstract methods
public abstract double calArea();
public abstract double calPerimeter();
// toString method is used to display the contents of an object inside it
@Override
public String toString() {
return " Color=" + color;
}
}
________________
Circle.java
public class Circle extends Shape {
//Declaring instance variables
double radius;
//Parameterized constructor
public Circle(String color, double radius) {
super(color);
this.radius = radius;
}
//Method which calculates the area
@Override
public double calArea() {
return Math.PI * radius * radius;
}
//Method which calculates the Perimeter
@Override
public double calPerimeter() {
return 2 * Math.PI * radius;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Circle :: radius:" + radius + ",Area :" + calArea() + ". Perimeter:" + calPerimeter() + super.toString();
}
}
_________________
Rectangle.java
public class Rectangle extends Shape {
private double height;
private double width;
public Rectangle(String color, double height, double width) {
super(color);
this.height = height;
this.width = width;
}
//Method which calculates the area
@Override
public double calArea() {
return width * height;
}
//Method which calculates the Perimeter
@Override
public double calPerimeter() {
return 2 * (height + width);
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Rectangle :: Width:" + width + ",Height :" + height + ", Area :" + calArea() + ". Perimeter:" + calPerimeter() + super.toString();
}
}
__________________
Triangle.java
public class Triangle extends Shape {
private double side1;
private double side2;
private double side3;
public Triangle(String color, double side1, double side2, double side3) {
super(color);
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getSide1() {
return side1;
}
public double getSide2() {
return side2;
}
public double getSide3() {
return side3;
}
//Method which calculates the area
@Override
public double calArea() {
double p;
double area;
p = (side1 + side2 + side3) / 2;
area = Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
return area;
}
//Method which calculates the Perimeter
@Override
public double calPerimeter() {
return side1 + side2 + side3;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Triangle :: Side1:" + side1 + ", Side2 :" + side2 + ", Side3:" + side3 + ", Area :" + calArea() + ". Perimeter:" + calPerimeter() + super.toString();
}
}
_____________________
Test.java
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
// Creating an ArrayList which holds Shape type Obejcts
ArrayList < Shape > arl = new ArrayList < Shape > ();
// Creating an Circle Class object
Circle c1 = new Circle("Green", 5.0);
// Adding to the ArrayList
arl.add(c1);
// Creating an Rectangle Class object
Rectangle r1 = new Rectangle("Red", 6.0, 4.0);
// Adding to the ArrayList
arl.add(r1);
// Creating an Triangle Class object
Triangle t1 = new Triangle("Blue", 3, 4, 5);
// Adding to the ArrayList
arl.add(t1);
// Iterating over ArrayList based on the type of Object display the area
// and perimeter
for (int i = 0; i < arl.size(); i++) {
System.out
.println("------------------------------------------------");
if (arl.get(i) instanceof Circle) {
Circle c = (Circle) arl.get(i);
System.out.println(c.toString());
} else if (arl.get(i) instanceof Rectangle) {
Rectangle r = (Rectangle) arl.get(i);
System.out.println(r.toString());
} else if (arl.get(i) instanceof Triangle) {
Triangle t = (Triangle) arl.get(i);
System.out.println(t.toString());
}
}
System.out.println("------------------------------------------------");
}
}
___________________
Output:
------------------------------------------------
Circle :: radius:5.0,Area :78.53981633974483. Perimeter:31.41592653589793 Color=Green
------------------------------------------------
Rectangle :: Width:4.0,Height :6.0, Area :24.0. Perimeter:20.0 Color=Red
------------------------------------------------
Triangle :: Side1:3.0, Side2 :4.0, Side3:5.0, Area :6.0. Perimeter:12.0 Color=Blue
------------------------------------------------
_________________
2)
Shape.java
public interface Shape {
String getColor();
double calArea();
double calPerimeter();
}
________________
Circle.java
public class Circle implements Shape {
//Declaring instance variables
double radius;
private String color;
public Circle(String color, double radius) {
super();
this.radius = radius;
this.color = color;
}
@Override
public String getColor() {
return color;
}
//Method which calculates the area
@Override
public double calArea() {
return Math.PI * radius * radius;
}
//Method which calculates the Perimeter
@Override
public double calPerimeter() {
return 2 * Math.PI * radius;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Circle :: radius:" + radius + ",Area :" + calArea() + ". Perimeter:" + calPerimeter() + ", Color :" + color;
}
}
__________________
Rectangle.java
public class Rectangle implements Shape {
private String color;
private double height;
private double width;
public Rectangle(String color, double height, double width) {
super();
this.color = color;
this.height = height;
this.width = width;
}
@Override
public String getColor() {
return color;
}
//Method which calculates the area
@Override
public double calArea() {
return width * height;
}
//Method which calculates the Perimeter
@Override
public double calPerimeter() {
return 2 * (height + width);
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Rectangle :: Width:" + width + ",Height :" + height + ", Area :" + calArea() + ". Perimeter:" + calPerimeter() + ", Color :" + color;
}
}
__________________
Triangle.java
public class Triangle implements Shape {
private String color;
private double side1;
private double side2;
private double side3;
public Triangle(String color, double side1, double side2, double side3) {
super();
this.color = color;
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getSide1() {
return side1;
}
public double getSide2() {
return side2;
}
public double getSide3() {
return side3;
}
//Method which calculates the area
@Override
public double calArea() {
double p;
double area;
p = (side1 + side2 + side3) / 2;
area = Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
return area;
}
//Method which calculates the Perimeter
@Override
public double calPerimeter() {
return side1 + side2 + side3;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Triangle :: Side1:" + side1 + ", Side2 :" + side2 + ", Side3:" + side3 + ", Area :" + calArea() + ". Perimeter:" + calPerimeter() + ",Color:" + color;
}
@Override
public String getColor() {
return color;
}
}
___________________
Test.java
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
// Creating an ArrayList which holds Shape type Obejcts
ArrayList < Shape > arl = new ArrayList < Shape > ();
// Creating an Circle Class object
Circle c1 = new Circle("Green", 5.0);
// Adding to the ArrayList
arl.add(c1);
// Creating an Rectangle Class object
Rectangle r1 = new Rectangle("Red", 6.0, 4.0);
// Adding to the ArrayList
arl.add(r1);
// Creating an Triangle Class object
Triangle t1 = new Triangle("Blue", 3, 4, 5);
// Adding to the ArrayList
arl.add(t1);
// Iterating over ArrayList based on the type of Object display the area
// and perimeter
for (int i = 0; i < arl.size(); i++) {
System.out
.println("------------------------------------------------");
if (arl.get(i) instanceof Circle) {
Circle c = (Circle) arl.get(i);
System.out.println(c.toString());
} else if (arl.get(i) instanceof Rectangle) {
Rectangle r = (Rectangle) arl.get(i);
System.out.println(r.toString());
} else if (arl.get(i) instanceof Triangle) {
Triangle t = (Triangle) arl.get(i);
System.out.println(t.toString());
}
}
System.out.println("------------------------------------------------");
}
}
____________________
Output:
------------------------------------------------
Circle :: radius:5.0,Area :78.53981633974483. Perimeter:31.41592653589793, Color :Green
------------------------------------------------
Rectangle :: Width:4.0,Height :6.0, Area :24.0. Perimeter:20.0, Color :Red
------------------------------------------------
Triangle :: Side1:3.0, Side2 :4.0, Side3:5.0, Area :6.0. Perimeter:12.0,Color:Blue
------------------------------------------------
We observed that the difference between abstract class and interface is we can write the common method implementation of all classes in the abstract class and methods that having different method body should implement in its subclasses.But we should provide the method declarations in abstract class.
Here getColor() method is having same method for all the classes.So we wrote the method name and its implementation in abstract class Shape.
But the area and perimeter is not same for circle,rectangle,triangle classes.So we have to provide the method implementation in each sub class .
But If we provide the method declarations in Interface we should provide method bodies in its implementation classes.
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.