Question
java In Eclipse Create a package called shape and write these classes in it: Define a public interface called Shape it must have a public
java In Eclipse
Create a package called shape and write these classes in it:
Define a public interface called Shape it must have a public double method called area().
Define another interface called TwoDShape that extends the Shape and has a public double method called perimeter().
Define another interface called ThreeDShape that extends Shape and has a public double method called volume();
Define an abstract class called AbstractShape that implements both TwoDShape and ThreeDShape interfaces: It will have
a private static double instance variable called numShapesCreated;
a void method called incrementNumShapesCreated that increments numShapesCreated by 1;
a getNumShapesCreated() method that returns the value of numShapesCreated..
public abstract methods called area(), perimeter() and volume()
Define the following classes: Rectangle, Circle, Cuboid and Sphere that extend AbstractShape. Each of these should have a static instance variable that represents the number of objects of that kind created (numRectanglesCreated, numCirclesCreated, numCuboidsCreated, numSpheresCreated). The constructor of each should increment the specific objects created variable and also the parent class variable numShapesCreated. I have given the code for Rectangle as a model:
Each of these classes must have pre-conditions and post-conditions as shown in this example:
public class Rectangle extends AbstractShape
{
private double length;
private double width;
private static int numRectanglesCreated = 0;
public Rectangle(double len, double wid) //constructor
{
//pre-condition
assert (len >= 0 && wid >= 0) :
"length and width must be positive";
length = len;
width = wid;
incrementNumRectanglesCreated();
incrementNumShapesCreated();
}
public double area()
{
double area = length * width;
//post-condition
assert (area >= 0) : "Area must be positive";
return area;
}
public double perimeter()
{
double perimeter = 2 * (length + width);
//post-condition
assert (perimeter >= 0) : "Perimeter must be positive";
return area;
}
public double volume()
{
return 0.0;
}
public void incrementNumRectanglesCreated()
{
numRectanglesCreated++;
}
}
Similarly write the Circle and Cuboid classes:
Circle has double radius and two methods called area() and perimeter(). (area = pi* radius * radius; perimeter = 2 * pi * radius; volume is zero)
Cuboid has double length, width and height and methods area() perimeter() and volume(). Note that area() here means the surface area of the cuboid = 2 * (length * width + width * height + height * length; perimeter is assumed to be zero and volume is length * width * height).
Sphere has a double radius and has area() and volume() methods. (area = 4.0 * pi * radius * radius; perimeter is zero and volume = (4.0 * pi * radius * radius * radius) / 3.0)
Write a ShapeTester class that has only the main method that does the following things:
Declares an array: AbstractShape[ ] myShapes = new AbstractShape[16];
Creates 16 Shape objects and fills them in the array: 4 objects of each type using the data given below.
Shape | length | width | height | radius |
Rectangle | 10 | 5 | - | - |
Rectangle | 20 | 30 | - | - |
Rectangle | 50 | 10 | - | - |
Rectangle | -10 | -20 |
|
|
Circle | - | - | - | 20 |
Circle | - | - | - | 10 |
Circle | - | - | - | 30 |
Circle | - | - | - | -20 |
Cuboid | 20 | 10 | 6 | - |
Cuboid | 40 | 5 | 50 | - |
Cuboid | 30 | 20 | 20 | - |
Cuboid | -20 | 30 | -10 |
|
Sphere | - | - | - | 15 |
Sphere | - | - | - | 25 |
Sphere | - | - | - | 12 |
Sphere | - | - | - | -10 |
In a loop: prints the area, perimeter and volume of each shape. (Use formatted printing, System.out.printf () method to print numbers with only two decimal places. See sample programs in the PART-B: Java Input/Output document.)
After the loop, it should also print the totals (something like this:
Total number of Rectangle objects = ____
Total number of Circle objects = ____
Total number of Cuboid objects = ____
Total number of Sphere objects = ____
Total number of Shape objects = ____
The first time you run the program make sure that the assertions are NOT enabled. The programs should run fine. In the second run, you should enable the assertions and show that it crashes the program when the first assertion fails.
How to turn-on/off the assertions? (ea = enable assertions) Window/Preferences/Click on Java/Click on Installed JREs/Click on the jre on the right/click on edit In the Default VM Arguments type ea and click finish and then OK. To disable it, just go back there and change ea into blank.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started