0

I am currently working on the following questions as revision:

A VolumeModel stores the data for a volume control object. The volume has level and it can also be put in a muted state.

(The following underneath is what is drawn in a UML diagram:)

VolumeModel - The Class name

muted: int - The attribute

level: int - The attribute

+..methods.. - The methods for the class

a) Write down all the method headers, including the constructors, that you would expect to see in this class so that it conforms with conventional standards supporting data encapsulation. (Do not write the method bodies or documentation).

As my answer for that question I wrote it as this:

public VolumeModel()
public VolumeModel(boolean muted, int level)
public void setLevel(int level)
public int getLevel()
public setMuted(boolean muted)
public boolean isMuted()
public void turnUp()
public void turnUp(int level)
public String toString()
public boolean equals(VolumeModel V)

I do not know if that is ALL the expected method headers, including the constructors, that is expected to be seen in this class because of the following question (that is related to the above question) underneath:

b) Using the method you declared in part (a), write the code for a test program to:

  • create an instance of a volume model...
  • with a volume level 1 and not muted:
  • in a loop, steadily increase the volume to level 10:
  • then mute the volume;
  • finally output the state of the volume model object

As my answer to that question above so far, I have done this:

VolumeModel vml = new VolumeModel();
vml.setLevel(1);
vml.setMuted(false);

VolumeModel volumelevel = new VolumeModel(true, 11);
while (!vml.equals(volumelevel)
{
 //to be completed  
}

That piece of code above is not finished as I am struggling on that, so if there are any tips that someone could give me then I would appreciate it.

Also, any suggestions and feedback on my following answer of question a) would also be of help.

Thanks in advance.

p.s - I am unable to try this code out on my eclipse software because my eclipse software is not working on my laptop for some reason, however when I find time to go to my computer labs then I will see if I can test the code on the eclipse software on their computers over there.

user1279780
  • 99
  • 3
  • 10
  • 1
    I suggest you test the code actually works and if you find a bug, step through the code in your debugger to see what it doesn't do what you expect. I would comment that you don't appear to be using all the methods you define in a) in your code in b). – Peter Lawrey Apr 13 '12 at 14:55
  • @Peter Lawrey - Thanks for your comment. I do not disagree with you, once I get hold of a computer lab next week I will test it out properly but right now I am only able to answer these questions in theory. – user1279780 Apr 13 '12 at 15:06
  • 1
    In which case I can only answer your theorical needs. ;) I would suggest that your methods in (a) should only be what you use in (b). – Peter Lawrey Apr 13 '12 at 15:08

2 Answers2

2

Some remarks:

  • you forgot to declare the return type of setMuted()
  • there is no javadoc. Although most of the methods are obvious, what does turnUp(int level) do, for example? Is the argument the new level (in which case it does the same thing as setLevel()), or is it an increment? What's the state of the object when constructed using the no-arg constructor?
  • if equals() is overridden, then hashCode() must also be. The contract is that equal objects MUST have the same hashCode. Moreover, the equals method should take an Object as argument. You may implement an equals(VolumeModel) method, but be aware that you would then not override the Object.equals() method, and this method would thus never be called by the collection classes.
  • it's bizarre to have turnUp methods, but no turnDown.
  • it's bizarre to not have limits (although it's not asked in the question).

Finally, your snippet doesn't do what is asked. It should:

  • create a unique VolumeModel instance, with the volume set to 1 and not muted
  • call turnUp() in a loop of 9 iterations
  • mute it by calling setMuted(true)
  • call getLevel() and isMuted() and check that the values are 10 and true respectively. The question doesn't ask you to implement equals and to test the equality between two VolumeModel instances.
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 1
    I would add that you should have `equals(Object)` rather than `equals(VolumeModel)` – Peter Lawrey Apr 13 '12 at 15:08
  • Thank you for the comments, my assumption was that turnUp(int level) increases the volume and that setLevel() would only set the level up to level 10 (I am probably wrong here) because question (b) only wants the volume to be increased to level 10. – user1279780 Apr 13 '12 at 15:13
1

You don't need to create a new VolumeModel to test. You also don't need the equals method.

Instead of doing a while cycle try this:

for(i=2;i<11;i++){
    vml.turnUp(); //provided that turnUp sets the volume up a level
}
vml.setMuted(true);

At this point you should specify how you want to output the state of the Object. Instead of trying to reimplement toString, as it seems you were doing, you can create your own outputState() this way

public void outputState(VolumeModel v)

The return type is void because you can simply put a series of System.out, or whichever output system you prefer.

Hope this helps.

Kappei
  • 714
  • 2
  • 15
  • 34