Creating a class,for a box. (Java) -


hi had assignment class:

  1. implement class named box have following attributes , methods: int length, width, height string color

  2. constructor method that:

    • will initialize 3 integers 10, 8, 6
    • will initialize color “black”
  3. a setter , getter method each of 4 attributes
  4. a method volume of box
  5. a method surface area of box (all 6 sides)

i have getters , setters length width , color. problem volume not calculate if set values different.

it takes initialized values. ideas go it? code below class. example .setlength(7) , instead of printing total 7*8*6, prints out 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;       }   } 

get rid of vol property , setvol setter; that's not part of spec class , root cause of problems. rewrite getvol compute volume length, width, , height each time called.

your current design doesn't work because vol not recalculated whenever length, width, or height changed. keep current set of fields , rewrite dimension setters recalculate vol property each time 1 called. speed getvol getter method @ cost of greater complexity class design , slower setter methods. it's trade-off can make or not, see fit. however, need rid of setvol method, because when set volume, there's no way know how set dimensions values consistent.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -