简体   繁体   中英

Need mutator methods how do I do them

I am doing a UML and I am not quite sure how to do these mutator methods I am supposed to do this:

+turnOn(): void       //sets on to true
+turnOff(): void      //sets on to false
+channelUp(): void    //increases channel by 1 if on, rolls to 1 after maximum
+channelDown(): void  //decreases channel by 1 if on, rolls to maximum after 1
+volumeUp(): void     //increases the volume by 1 if on and less than maximum
+volumeDown(): void   //decreases volume by 1 if on and greater than 0
+toString( ): String  //returns the current state(instance variable values)

my code right now: (keep in mind the mutator part isn't right)

public class TV {
    private int volume;
    private int channel;
    private boolean on;
    private int maxVolume;
    private int maxChannel;

    TV() {
        volume = 1;
        channel = 1;
        on = false;
        maxVolume = 1;
        maxChannel = 1;
    }
    public int getChannel() {
        return channel;
    }

    public int getVolume() {
        return volume;
    }

    public boolean isOn() {
        return on;
    }

    public int getMaxChannel() {
        return maxChannel;
    }

    public int getMaxVolume() {
        return maxVolume;
    }
    public void setChannel(int i) {
        if (isOn() && i >= 1 && i <= maxChannel) channel = i;
    }

    public void setVolume(int i) {

        if (isOn() && i >= 0 && i <= maxVolume) volume = i;
    }

    public void setMaxChannel(int i) {
        maxChannel = i;
    }

    public void setMaxVolume(int i) {
        maxVolume = i;
    }

    // + turnOn() * * This is where the mutator methods begin I need help here * *

    // if (channel == maxChannel) channel = 1;
    // else channel++;

    //if (channel == 1) channel = max;
    //else channel--;

    // if (volume == maxVolume) volume = 1;
    // else channel++;
    //if (volume == 1) volume = max;
    // else channel--;

    public string toString() {
        return "channel: " + channel + "\nvolume: " + volume +
            "\non: " + on + "\nmax Channel: " + maxChannel +
            "\nmax Volume: " + maxVolume;
    }
}

Mutator generally means the same things as 'setter'

So in your above code, a 'getter' would be:

public int getMaxChannel() {
  return maxChannel;
}

and a 'mutator' or 'setter' would be:

public void setMaxChannel(int maxChannel) {
  this.maxChannel = maxChannel;
}

We use setters and mutator as interchangeably.

A mutator method is used to set a value of a private field. It follows a naming scheme prefixing the word "set" to the start of the method name. These methods do not have a return type and accept a parameter that is the same data type as their corresponding private field. The parameter is then used to set the value of that private field.

Below are some examples of mutators or setters:

public void setMaxChannel(int i) {
        maxChannel = i;
    }

public void setChannel(int c) {
        channel=c;
    }

Sample methods:

public void turnOn() {
   this.on = true;
}

public void channelUp() {
   if (on) {
      if (channel == maxChannel) {
         channel = 1;
      }
      else {
         channel++;
      }
   }
}

public void volumeDown() {
   if (on && volume > 0) {
      volume--;
   }
}

Other methods follows the same logic.

Strings in java are objects, so your toString method signature should read public String toString() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM