Given the following interfaces in a package named shapes What is a package? How
ID: 653388 • Letter: G
Question
Given the following interfaces in a package named shapes
What is a package?
How to create or use ''shapes'' package?
Shape.java
Shape2D.java
Shape3D.java
Implement the following classes in the same shapes package
Circle: A class implementing the Shape2D interface, whose constructor takes one double argument as its radius.
Sphere: A class implementing the Shape3D interface, whose constructor takes one double argument as its radius.
Rectangle: A class implementing the Shape2D interface, whose constructor takes two double arguments as its sides.
Square: A class extending the Rectangle class, whose constructor takes one double argument as its side.
RectangularCuboid: A class implementing the Shape3D interface, whose constructor takes three doublearguments as its sides.
Cube: A class extending the RectangularCuboid class, whose constructor takes one double argument as its side.
All classes constructors would only accept positive values as valid arguments. Otherwise, they should throw anjava.lang.IllegalArgumentException
Also, for minimality and elegance of design, try to avoid re-implementing methods unnecessarily. For example, aSquare is actually just a Rectangle with its length equal to its width. Can we reuse some code to calculate its area and perimeter, or we have to write everything from scratch?
You may use a main method for testing, but it will not be tested by Web-CAT.
Submit your solution as a zip file containing the shapes package(folder) with all your class implementations.
Explanation / Answer
A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer. You might keep HTML pages in one folder, images in another, and scripts or applications in yet another.
The package keyword is used to create a package in java.
I am pasting all the required class files below.
1)
package shapes;
public class Square implements Shape2D {
private double side;
public Square(double side) {
super();
this.side = side;
}
public Type getType() {
return Type.SQUARE;
}
public double getPerimeter() {
return 4 * side;
}
public double getArea() {
return side * side;
}
}
2)
package shapes;
public class Circle implements Shape2D {
private double radius;
public Circle(double radius) {
super();
this.radius = radius;
}
public Type getType() {
return Type.CIRCLE;
}
public double getPerimeter() {
return 2 * Math.PI * radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
}
3)
package shapes;
public class Cube implements Shape3D {
private double side;
public Cube(double side) {
super();
this.side = side;
}
public Type getType() {
return Type.CUBE;
}
public double getSurfaceArea() {
return 6 * side * side;
}
public double getVolume() {
return side * side * side;
}
}
4)
package shapes;
public class Rectangle implements Shape2D {
private double length;
private double breadth;
public Rectangle(double length, double breadth) {
super();
this.length = length;
this.breadth = breadth;
}
public Type getType() {
return Type.RECTANGLE;
}
public double getPerimeter() {
return 2 * (length + breadth);
}
public double getArea() {
return length * breadth;
}
}
5)
package shapes;
public class RectangularCuboid implements Shape3D {
private double length;
private double breadth;
private double height;
public RectangularCuboid(double length, double breadth, double height) {
super();
this.length = length;
this.breadth = breadth;
this.height = height;
}
public Type getType() {
return Type.RECTANGULAR_CUBOID;
}
public double getSurfaceArea() {
return 2 * (length * breadth + breadth * height + height * length);
}
public double getVolume() {
return length * breadth * height;
}
}
6)
package shapes;
public class Sphere implements Shape3D {
private double radius;
public Sphere(double radius) {
super();
this.radius = radius;
}
public Type getType() {
return Type.SPHERE;
}
public double getSurfaceArea() {
return 4 * Math.PI * radius * radius;
}
public double getVolume() {
return 4 * Math.PI * radius * radius * radius / 3;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.