In: Computer Science
I. Design and code a class Rectangle that has the following properties:
Two private members double length and width
A constructor that accepts two parameters for the private members
Default constructor that sets both members to 0
Setters/getters for each private member
Method area() that returns the area of the rectangle
Method Perim() that returns the perimeter of the rectangle
Method toString that return the param as a String
Method Add that accepts a Rectangle and returns a rectangle with the length as sum of lengths of this and the input rectangle and the width as the sum too.
Method equals() that accepts a rectangle and returns true if the two rectangles are the same and false if not.
II. Design a Class Cube that has the following:
Private member Rectangle
Private member height.
A constructor that accepts three double (length, width, height)
Setters getters
Method volume that returns the volume of the cube which is the area of the rectangle multiplied by the height
Method toString() that returns the length, width and height as a string
Method equals that accepts another Cube and returns true if the two cubes are the same and false if not. Use the equals in Rectangle.
Method Cube add(Cube C) that returns the sum of the Cube this
I. class Rectangle
public class Rectangle
{
private double length;
private double width;
// Constructor
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
// Getter methods
public double getLength() {
return length;
}
public double getWidth() {
return width;
}
// Setter methods
public void set(double l, double w)
{
length = l;
width = w;
}
public double area()
{
return length * width;
}
public double Perim()
{
return 2 * (length + width);
}
public String toString() {
return "Rectangle - " + length + " X " + width;
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Rectangle)) {
return false;
}
}
}
public class Main
{
public static void main(String[] args) {
Rectangle rectangle1obj = new Rectangle(0,0);
rectangle1obj.set(4, 6);
System.out.println("Area of Rectangle is " +
rectangle1obj.area());
System.out.println("Perimeter of Rectangle is " +
rectangle1obj.perim());
System.out.println(rectangle1obj.toString());
System.out.println(rectangle1obj.equals());
}
}
II . Class Cube
public class Cube
{
private double length;
private double width;
private double rectangle;
private double height;
// Constructor
public Cube(double length, double width, double height) {
this.length = length;
this.width = width;
this.height = height;
}
// Getter methods
public double getLength() {
return length;
}
public double getWidth() {
return width;
}
public double getheight() {
return height;
}
// Setter methods
public void set(double l, double w, double h)
{
length = l;
width = w;
heigth = h;
}
//volume of the cube which is the area of the rectangle multiplied
by the height
public double volume()
{
return (length * width)*height;
}
public double cubeAdd(double n)
{
double sum = 0;
for (int x = 1; x <= n; x++)
sum += x * x * x;
return sum;
}
public String toString() {
return "Rectangle - " + length + " X " + width + " X " +
height;
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Cube)) {
return false;
}
}
}
public class Main
{
public static void main(String[] args) {
Cube cube1obj = new Cube();
cube1obj.set(4, 6);
cube1obj.cubeAdd(5);
System.out.println("Volume of cube is " + cube1obj.volume());
System.out.println(cube1obj.cubeAdd());
System.out.println(cube1obj.toString());
System.out.println(cube1obj.equals());
}
}