user2872194
user2872194

Reputation: 147

Creating a class,for a box. (Java)

Hi I had this assignment for my class:

  1. Implement a class named Box that will have the following attributes and methods: int length, width, height String color

  2. Constructor method that:

    • will initialize the 3 integers to 10, 8, 6
    • will initialize the color to “black”
  3. A setter and getter method for each of the 4 attributes
  4. A method to get the volume of the box
  5. A method to get the surface area of the box (all six sides)

I have all my getters and setters for length width and color. My only problem now is that for volume it will not calculate properly if I set the values to be different.

It only takes the initialized values. Any Ideas to go about it? My code is below for the class. example I could .setLength(7) and instead of printing the total 7*8*6, it prints out the total of 10*8*6.

    public class Box
    {
      private int height = 6;

      public void se(int height){
      this.height=height;
     } 
      public int getHeight(){
         return height;
     } 
    private int width = 8;
    public void setWidth(int width){
      this.width=width;
    } 
      public int getWidth(){
         return width;
    } 
    private int length= 10;
    public void setLength(int length){
      this.length=length;
    } 
      public int getLength(){
         return length;
    }
     private String color="Black";
     public void setColor(String color){
      this.color=color;
    } 
     public String getColor(){
      return color;
    }
    private int vol=length*width*height;
      public void setVol(int vol){
      this.vol=vol;

    }
    public int getVol(){
       return vol;
      }
  }

Upvotes: 0

Views: 4095

Answers (2)

Scary Wombat
Scary Wombat

Reputation: 44854

You need to create a getter function for vol

e.g.

public int getVol () {
   vol=length*width*height;
   return vol;
}

of course the setting of the intermediate vol is not necessary. and you could just

return length*width*height;

This ensure that the current vol is always correctly calculated.

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234847

Get rid of the vol property and the setVol setter; that's not part of the spec for the class and is the root cause of your problems. Rewrite getVol to compute the volume from the length, width, and height each time it is called.

Your current design doesn't work because vol is not recalculated whenever length, width, or height is changed. You could keep your current set of fields and rewrite the dimension setters to recalculate the vol property each time one is called. That would speed up the getVol getter method at the cost of greater complexity for the class design and slower setter methods. It's a trade-off that you can make or not, as you see fit. However, you need to get rid of the setVol method, because when you set the volume, there's no way to know how to set the dimensions so that the values are consistent.

Upvotes: 2

Related Questions