Write a ONE java program to implement the following requirements: Design 3 class
ID: 3815593 • Letter: W
Question
Write a ONE java program to implement the following requirements: Design 3 classes: Shape8, Circle8, Square8 as below. Shape8 is an abstract class ComputeData is an interface Italicized methods mean abstract methods computeData +getDiagonal (): double -color: String -area: double +Shape8(String color) +getArea():double +setArea (area: double):void +getColor():String +compareto(o: Shape8): int +toString(): String -radius: double +Circle8(color: String, radius: double) +toString(): String ....necessary methods -width: double +Square8(color: String, width: double) +toString(): String ....necessary methods The program should be named: xxxx_lab8.java (xxxx is your email ID). Implement the necessary methods for each class. The test class xxxxx_lab8 should have a main() method that will call three methods: readData(), sort(), report(). readData() method will take 3 input records from keyboard, the input formats are as below (color, radius or width, type) For example: blue 10 circle yellow 5.5 square red 5 circle Use a Shape8 array to hold these objects. If the type is circle, create a Circle8 object to hold the data. If the type is square, create a Square8 object to hold the data. sort() method will sort the three objects' areas from low to high. report() method will print the sorted objects with the following information: a. color, area and radius for circle object. b. color, area, width, and diagonal for square object. The above test data should have the following output: Color: yellow, Area: 30.25, Width:5.5, Diagonal: 7.7781745930520225 Color: red, Area: 78.53981633974483, Radius: 5 Color: blue, Area: 314.1592653589793, Radius: 10Explanation / Answer
Please find the .java files. Create a Java project in Eclipse 'Shape'. Then create a package 'shape' under this project and save all this files under this package. Compile and run the TestLab8.java file.
Shape8.java
package shape;
public abstract class Shape8 implements Comparable<Shape8>{
private String color;
private double area;
public Shape8(String color){
this.color = color;
}
public abstract double getArea();
public void setArea(double area){
this.area = area;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int compareTo(Shape8 obj){
//Comparison logic for area of different objects
if(obj.area > this.area)
return -1;
else if (obj.area < this.area)
return 1;
else return 0;
}
public String toString(){
//Providing generic output for all objects with color and area
return "Color: "+color+", Area: "+area;
}
}
ComputeData.java
package shape;
public interface ComputeData {
public abstract double getDiagonal();
}
Circle8.java
package shape;
public class Circle8 extends Shape8{
private double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public Circle8(String color, double radius){
super(color);
this.radius = radius;
//Calculating the area of object and setting to area variable
setArea(getArea());
}
@Override
public double getArea() {
return radius*radius*Math.PI;
}
public String toString(){
//Appending Radius to generic output of object
return super.toString()+", Radius: "+radius;
}
}
Square8.java
package shape;
public class Square8 extends Shape8 implements ComputeData{
private double width;
public Square8(String color, double width){
super(color);
this.width = width;
//Calculating the area of object and setting to area variable
setArea(getArea());
}
@Override
public double getArea() {
return width*width;
}
@Override
public double getDiagonal() {
return Math.sqrt(width*width*2);
}
public String toString(){
//Appending Width and Diagonal to generic output of object
return super.toString()+", Width: "+width+", Diagonal: "+getDiagonal();
}
}
TestLab8.java
package shape;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class TestLab8 {
//To take input from keyboard
static Scanner sc = new Scanner(System.in);
//To store objects created from user input
static ArrayList<Shape8> sh = new ArrayList<Shape8>();
public static void readData(){
String input;
for (int i=0; i<3; i++){
input = sc.nextLine();
String ar[] = input.split(" ");
if(ar[2].equalsIgnoreCase("circle")){
Circle8 circle8 = new Circle8(ar[0], Double.parseDouble(ar[1]));
sh.add(circle8);
}else if( ar[2].equalsIgnoreCase("square")){
Square8 square8 = new Square8(ar[0], Double.parseDouble(ar[1]));
sh.add(square8);
}else
System.out.println("Invalid input");
}
}
public static void sort(){
//Sorting the objects created as per the compareTo function implemented in Shape8 class
Collections.sort(sh);
}
public static void report(){
for(Shape8 shape8: sh){
System.out.println(shape8);
}
}
public static void main(String[] args) {
readData();
sort();
report();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.