Java Programming Thank you Design and implement: an abstract class named Geometr
ID: 3851754 • Letter: J
Question
Java ProgrammingThank you Design and implement: an abstract class named GeometricObject which has an abstract method getPerimeter (). two classes named Circle and Square which are subclasses of GeometricObject. Their data fields should have default values set up in the no-arg constructors, and they should override the toString () method. (a) the first test program is as follows: public static void main (String [] orgs) { GeometricObject [] geoList = new GeometricObject [4]: geolist [0] = new Circle (): geolist [l] = new Circle (10.0): geolist [2] = new Square (): geoList [3] = new Square (10.0): for (int i = 0: i
Explanation / Answer
Here is the answer for both the parts. In case of any doubts, please post a comment and I shall respond. If happy with the answer , please rate it. Thank you very much.
part 1
public abstract class GeometricObject implements Comparable<GeometricObject>{
public abstract double getPerimeter();
}
public class Circle extends GeometricObject{
private double radius;
//default constructor
public Circle()
{
radius = 1;
}
public Circle(double r)
{
radius = r;
}
@Override
public double getPerimeter() {
return 2 * Math.PI * radius;
}
public String toString()
{
return "The Circle with radius " + String.format("%.4f", radius) +
" has perimeter " + String.format("%.4f", getPerimeter());
}
}
public class Square extends GeometricObject {
private double width;
//default constructor
public Square()
{
width = 1;
}
public Square(double w)
{
width = w;
}
@Override
public double getPerimeter() {
return 4 * width;
}
public String toString()
{
return "The Square with width " + String.format("%.4f", width) +
" has perimeter " + String.format("%.4f", getPerimeter());
}
}
public class GeometricTest1 {
public static void main(String[] args) {
GeometricObject[] geoList = new GeometricObject[4];
geoList[0] = new Circle();
geoList[1] = new Circle(10.0);
geoList[2] = new Square();
geoList[3] = new Square(10.0);
for(int i = 0; i < geoList.length ; i++)
System.out.println(geoList[i]);
}
}
output
The Circle with radius 1.0000 has perimeter 6.2832
The Circle with radius 10.0000 has perimeter 62.8319
The Square with width 1.0000 has perimeter 4.0000
The Square with width 10.0000 has perimeter 40.0000
part 2
//GeometricObject implements Comparable interface so that it can be sorted using Array.sort()
public abstract class GeometricObject implements Comparable<GeometricObject>{
public abstract double getPerimeter();
//to compare perimeters of 2 geometric objects
@Override
public int compareTo(GeometricObject o) {
double diff = getPerimeter() - o.getPerimeter();
if(diff < 0) //return -ve when this perimeter is less than the other
return -1;
else if (diff > 0) //return +ve when this perimeter is greater than other
return 1;
else // 0 when both are equal
return 0;
}
}
import java.util.Arrays;
public class GeometricTest2 {
public static void main(String[] args) {
GeometricObject[] geoList = new GeometricObject[4];
geoList[0] = new Circle();
geoList[1] = new Circle(10.0);
geoList[2] = new Square();
geoList[3] = new Square(10.0);
Arrays.sort(geoList);
for(int i = 0; i < geoList.length ; i++)
System.out.println(geoList[i]);
}
}
output
The Square with width 1.0000 has perimeter 4.0000
The Circle with radius 1.0000 has perimeter 6.2832
The Square with width 10.0000 has perimeter 40.0000
The Circle with radius 10.0000 has perimeter 62.8319
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.