Java programming question)
Need Help Writing an Essay?
Tell us about your assignment and we will find the best writer for your paper.
Get Help NowWrite an abstract class named BaseShape. It should have anabstract method which is called printName which prints out the nameof the shape.
Write an interface named Measurable with the methods
- calculateArea
- calculatePerimeter
Write concrete classes for the following shapes which implementsthe Measurable interface.
- Triangle
- Square
- Rectangle
- Trapezoid
- Circle
Expert Answer
Note: Could you plz go through this code and let meknow if u need any changes in this.Thank You
_________________
// Measurable.java
public interface Measurable {
public void calculateArea();
public void calculatePerimeter();
}
__________________________
// BaseShape.java
public abstract class BaseShape {
private String name;
public BaseShape(String name) {
this.name = name;
}
public String getName() {
return name;
}
public abstract void printName();
}
_______________________
// Circle.java
public class Circle extends BaseShape implements Measurable{
private double radius;
public Circle(String name, double radius) {
super(name);
this.radius = radius;
}
@Override
public void calculateArea() {
double area = Math.PI * radius *radius;
System.out.printf(“Area :%.2fn”,area);
}
@Override
public void calculatePerimeter() {
double perim = 2 * Math.PI *radius;
System.out.printf(“Perimeter:%.2fn”, perim);
}
@Override
public void printName() {
System.out.println(“Name :” +getName());
}
}
_______________________
// Rectangle.java
public class Rectangle extends BaseShape implements Measurable{
private double length;
private double width;
public Rectangle(String name, double length, doublewidth) {
super(name);
this.length = length;
this.width = width;
}
@Override
public void calculateArea() {
double area = length * width;
System.out.printf(“Area :%.2fn”,area);
}
@Override
public void calculatePerimeter() {
double perim = 2 * (length +width);
System.out.printf(“Perimeter:%.2fn”, perim);
}
@Override
public void printName() {
System.out.println(“Name :” +getName());
}
}
____________________________
// Square.java
public class Square extends BaseShape implements Measurable{
private double side;
public Square(String name, double side) {
super(name);
this.side = side;
}
@Override
public void calculateArea() {
double area = side * side;
System.out.printf(“Area :%.2fn”,area);
}
@Override
public void calculatePerimeter() {
double perim = 4 * side;
System.out.printf(“Perimeter:%.2fn”, perim);
}
@Override
public void printName() {
System.out.println(“Name:”+getName());
}
}
__________________________
// Trapezoid.java
public class Trapezoid extends BaseShape implements Measurable{
private double b1;
private double b2;
private double a;
private double b;
private double h;
public Trapezoid(String name, double b1, double b2,double a, double b,
double h){
super(name);
this.b1 = b1;
this.b2 = b2;
this.a = a;
this.b = b;
this.h = h;
}
@Override
public void calculateArea() {
double area = ((b1 + b2) / 2.0) *h;
System.out.printf(“Area :%.2fn”,area);
}
@Override
public void calculatePerimeter() {
double perim = a + b + b1 +b2;
System.out.printf(“Perimeter:%.2fn”, perim);
}
@Override
public void printName() {
System.out.println(“Name :” +getName());
}
}
____________________________
// Triangle.java
public class Triangle extends BaseShape implements Measurable{
private double side1;
private double side2;
private double side3;
public Triangle(String name, double side1, doubleside2, double side3) {
super(name);
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
@Override
public void calculateArea() {
double s = (side1 + side2 + side3)/ 2.0;
// calculating area
double area = Math.sqrt(s * (s -side1) * (s – side2) * (s – side3));
System.out.printf(“Area :%.2fn”,area);
}
@Override
public void calculatePerimeter() {
// calculating perimeter
double peri = side1 + side2 +side3;
System.out.printf(“Perimeter:%.2fn”, peri);
}
@Override
public void printName() {
System.out.println(“Shape :” +getName());
}
}
__________________________
// Driver.java
import java.util.ArrayList;
public class Driver {
public static void main(String[] args) {
ArrayList arl =new ArrayList();
arl.add(new Triangle(“Triangle”, 3,4, 5));
arl.add(new Square(“Square”,4));
arl.add(new Rectangle(“Rectangle”,3, 4));
arl.add(new Trapezoid(“Trapezoid”,3, 11, 8, 10, 7));
arl.add(new Circle(“Circle”,5.5));
for (int i = 0; i <arl.size(); i++) {
if (arl.get(i)instanceof Triangle) {
arl.get(i).printName();
((Triangle) arl.get(i)).calculateArea();
((Triangle)arl.get(i)).calculatePerimeter();
System.out.println(“—————————“);
} else if(arl.get(i) instanceof Square) {
arl.get(i).printName();
((Square) arl.get(i)).calculateArea();
((Square)arl.get(i)).calculatePerimeter();
System.out.println(“—————————“);
} else if(arl.get(i) instanceof Rectangle) {
arl.get(i).printName();
((Rectangle) arl.get(i)).calculateArea();
((Rectangle)arl.get(i)).calculatePerimeter();
System.out.println(“—————————“);
} else if(arl.get(i) instanceof Trapezoid) {
arl.get(i).printName();
((Trapezoid) arl.get(i)).calculateArea();
((Trapezoid)arl.get(i)).calculatePerimeter();
System.out.println(“—————————“);
} else if(arl.get(i) instanceof Circle) {
arl.get(i).printName();
((Circle) arl.get(i)).calculateArea();
((Circle)arl.get(i)).calculatePerimeter();
System.out.println(“—————————“);
}
}
}
}
____________________________
Output:
Shape :Triangle
Area :6.00
Perimeter :12.00
—————————
Name :Square
Area :16.00
Perimeter :16.00
—————————
Name :Rectangle
Area :12.00
Perimeter :14.00
—————————
Name :Trapezoid
Area :49.00
Perimeter :32.00
—————————
Name :Circle
Area :95.03
Perimeter :34.56
—————————
_______________Could you plz rate me well.ThankYou
I lOVE this Professional essay writing website. This is perhaps the fifth time I am placing an order with them, and they have not failed me not once! My previous essays and research papers were of excellent quality, as always. With this essay writing website, you can order essays, coursework, projects, discussion, article critique, case study, term papers, research papers, research proposal, capstone project, reaction paper, movie review, speech/presentation, book report/review, annotated bibliography, and more.
Post your homework questions and get original answers from qualified tutors!