need help with this java program use the code below to solve this problem The Sh
ID: 3781084 • Letter: N
Question
need help with this java program
use the code below to solve this problem
The Shape class is abstract and contains one method: getArea. Your task is to create
two subclasses of Shape: Circle and Square. Then, you should write a driver program
with a main method that creates an array capable of holding four Shape objects.
Populate your array with two Circle objects and two Square objects. Finally, call the
getArea method on each Shape in your array and display the results
.
You should not make any changes to Shape.java.
Hints: In case you have repressed your memories of geometry class, the area of
a circle is pi times the radius squared, where pi is approximately 3.14159. Java has the value of pi stored more accurately as the constant Math.PI. The area of a square is
the length of its side, squared. You will likely need to declare fields in your Circle and Square classes to hold the radius and side length, respectively.
You will also need constructors for each class, to initialize the fields of the class.
Example Assume your array of Shape object contains the following:
A circle with a radius of 1
A circle with a radius of 5.27
A square with a side of length 14
A square with a side of length 10
Then the output of your driver program should be:
3.141592653589793
87.25113860888395
196.0
100.0
here is the code so far:
******lab.pkg1.practice.starting.code******(driver class)
package lab.pkg1.practice.starting.code;
public class Lab1PracticeStartingCode {
public static void main(String[] args) {
// TODO write code here to create an array of four Shape objects,
// call the getArea method of each one, and display the results.
}
}
*****shape.java*******
package lab.pkg1.practice.starting.code;
public abstract class Shape {
public abstract double getArea();
}
Explanation / Answer
Shape.java
package lab.pkg1.practice.starting.code;
//This is an abstract class
public abstract class Shape {
public abstract double getArea();
}
________________________
Circle.java
package lab.pkg1.practice.starting.code;
public class Circle extends Shape {
//Declaring instance variable
double radius;
//Parameterized constructor
public Circle(double radius) {
super();
this.radius = radius;
}
//This method finds and returns the area of the circle
@Override
public double getArea() {
return Math.PI*radius;
}
}
________________________
Square.java
package lab.pkg1.practice.starting.code;
public class Square extends Shape {
//Declaring instance variable
double side;
//Parameterized constructor
public Square(double side) {
super();
this.side = side;
}
//This method finds and returns the area of the square
@Override
public double getArea() {
return side*side;
}
}
___________________________
Lab1PracticeStartingCode.java( Driver class )
public class Lab1PracticeStartingCode {
public static void main(String[] args) {
//Creating an array of type shape which conatins the Circle class objects and Square class Objects
Shape arr[]={ new Circle(1),
new Circle(5.27),
new Square(14),
new Square(10)
};
//This for loop will find the area of the Circle and Square based on the shapes
for(int i=0;i<arr.length;i++)
{
//Displaying the area
System.out.println(arr[i].getArea());
}
}
}
_________________________
Output:
3.141592653589793
16.556193284418207
196.0
100.0
______________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.