Menu

Using Rectangle and RectangleStorage class (created during lecture session), develop a menu driven application that should provide the following features: Accept the data for a rectangle from the user…

Question

Answered step-by-step

Using Rectangle and RectangleStorage class (created during lecture…

Using Rectangle and RectangleStorage class (created during lecture session), develop a menu driven application that should provide the following features:

  1. Accept the data for a rectangle from the user, createe a rectangle object with it and add it to the rectangle array (1 point)
  2. Accept the data for a rectangle from the user which a user wants to update and update the rectangle. (0.5 point)
  3. Retrieve and display all the details of a rectangle (1 point)
  4. Retrieve and display the details about all the rectangles. (0.5 point)

 

 

 

 

 

 

import Week8.*;

 

public class RectangleStorage {

 

 

private Rectangle[] rectangles;

 

private final int CAPACITY = 5;

private int count = 0;

 

public RectangleStorage() {

rectangles = new Rectangle[CAPACITY];

for(int i=0; i<CAPACITY; i++)

rectangles[i] = new Rectangle();

 

}

public int getCapacity() {

return CAPACITY;

}

public Rectangle getRectangle(int loc) {

if (loc < count)

return rectangles[loc];

else

return null;

}

public Rectangle[] getRectangles() {

return this.rectangles;

}

public void setRectangles(Rectangle[] rectangles) {

this.rectangles = rectangles;

}

public boolean addRectangle(Rectangle r) {

if (count == CAPACITY)

return false;

rectangles[count++] =r;

return true;

}

public boolean updateRectangle(Rectangle r, int loc) {

if (loc >= count )

return false;

rectangles[loc] = r;

return true;

}

 }

 

 

import Week8.*;

 

public class MainApp {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

RectangleStorage rs =new RectangleStorage();

 

Rectangle r = Main.createRectangle();

 

rs.addRectangle(r);

 

Rectangle r1 = Main.createRectangle();

 

rs.addRectangle(r1);

 

Rectangle r2 = Main.createRectangle();

 

rs.addRectangle(r2);

 

Rectangle r3 = Main.createRectangle();

 

rs.addRectangle(r3);

 

Rectangle r4 = Main.createRectangle();

 

rs.addRectangle(r4);

 

Rectangle r5 = Main.createRectangle();

 

rs.addRectangle(r5);

 

Rectangle [] data = rs.getRectangles();

 

for (int i=0; i<data.length; i++) {

System.out.println(data[i]);

}

 

System.out.println(r);

 

 

}

 

}

 

package week8;

public class Rectangle {
private double length, width;

private static String schoolName=”humber”;

public static String getSchoolName() {
return schoolName; 
}

public static void setSchoolName(String name) {
 schoolName=name;
}
public Rectangle() {
//  length=1;
//  width=1;
 this(1,1);
}

public Rectangle(double length, double width) {
 this.length=length;  //referring the instance variable using this
 this.width=width;
}
public Rectangle(int length, int width) {
 this.length=length;
 this.width=width;
}
public void display() {
 System.out.print(“Length: ” +length + “width: ” +width);
}
public double getArea() {
 double area=this.length * this.width;
 return area;
}

public String toString() {
 return “Length: ” +length + “, width: ” +width;
}
}

 

 

 

 

this is the week 8 code 

OR

Leave a Reply

Your email address will not be published. Required fields are marked *