Question

In: Computer Science

I. Design and code a class Rectangle that has the following properties: Two private members double...

I. Design and code a class Rectangle that has the following properties:

  1. Two private members double length and width

  2. A constructor that accepts two parameters for the private members

  3. Default constructor that sets both members to 0

  4. Setters/getters for each private member

  5. Method area() that returns the area of the rectangle

  6. Method Perim() that returns the perimeter of the rectangle

  7. Method toString that return the param as a String

  8. 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.

  9. 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:

  1. Private member Rectangle

  2. Private member height.

  3. A constructor that accepts three double (length, width, height)

  4. Setters getters

  5. Method volume that returns the volume of the cube which is the area of the rectangle multiplied by the height

  6. Method toString() that returns the length, width and height as a string

  7. 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.

  8. Method Cube add(Cube C) that returns the sum of the Cube this

Solutions

Expert Solution

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());
   }
}


Related Solutions

/*Design and code and test a class MaxMin that has the following properties * two integer...
/*Design and code and test a class MaxMin that has the following properties * two integer member variables, min and max where min is smaller or equal than max at all times * A default constructor that sets both min and max to 0 * A constructor that accepts one integer and sets both min and max to that integer * A constructor that accepts two integers and sets min the smallest and max the largest * * Setters and...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The...
Following the example of Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with specified width and height A method name getWidth() return the value of width A method named getHeight() returns value of...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** *...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** * Create a point with coordinates <code>(0, 0)</code>. */ public Point2() { complete JAVA code this.set(0.0, 0.0); COMPLETE CODE }    /** * Create a point with coordinates <code>(newX, newY)</code>. * * @param newX the x-coordinate of the point * @param newY the y-coordinate of the point */ public Point2(double newX, double newY) { complete Java code this.set(newX, newY); }    /** * Create a...
I want to indent this c++ program #include<iostream> using namespace std; class Rectangle{ private: double width;...
I want to indent this c++ program #include<iostream> using namespace std; class Rectangle{ private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; double getPerimeter() const; bool isSquare() const; }; void Rectangle::setWidth(double w){ width = w; } void Rectangle::setLength(double l){ length = l; } double Rectangle::getWidth() const{ return width; } double Rectangle::getLength() const{ return length; } // Added Method definitions double Rectangle::getArea() const{ return (width * length); } double Rectangle::getPerimeter() const{...
create a class in C++ having the following 4 private members: one double variable representing the...
create a class in C++ having the following 4 private members: one double variable representing the balance of a bank account on string variable representing the bank account number a function to deposit money from the bank account a function to withdraw money from the bank account the following 2 public members: -two wrapper functions. - one parameterless constructor - one constructor initializing the account balance and account number - sample: #include <iostream> using namespace std; class account { public:...
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle....
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area of...
Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for...
Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for the class, the first being the default constructor and the second which takes the basic dimensions and sets up the private member variables with the appropriate initial values. Methods should be provided that allow a user of the class to find out the length, width, area and perimeter of the shape plus a toString()method to print the values of the basic dimensions. Now implement...
Define the class HotelRoom. The class has the following private data members: the room number (an...
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler...
Define the class HotelRoom. The class has the following private data members: the room number (an...
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT