简体   繁体   中英

Create a method to change the volume of the headphones, java class

This is only my second coding course, but I am loving everything about it. However, I am having trouble with creating a final method before I can turn it in. The program runs perfectly fine as of now, but I need to create a method called changeVolume(value) that changes the volume of the headphone to the value passed into the method. I feel like I am overthinking it, but I cannot seem to create any portion of code to switch the "volume level" for the headphones. Any help would be appreciated, but I need to step away for a bit and rest my eyes so I thought I would reach out for some help before I do!

在此处输入图片说明

The picture above is my program running

The following code below is my class HeadPhone.java, and the main class (TestHeadPhone.java) is where everything will be called. I left out getters/setters and a lot of other things so I hope this gives a good idea. If not, just let me know and I will share any portion of code you need!

//Constants defined
int LOW = 1;
int MEDIUM = 2;
int HIGH = 3;

//Private data fields 
private int volume;
private String manufacturer;
private String headPhoneModel;
private Color headPhoneColor;
private boolean pluggedIn = false;

//Default constructor for default headphones 
public HeadPhone () {
    volume = MEDIUM;
    manufacturer = "Apple";
    headPhoneModel = "Air Pods";
    headPhoneColor = Color.WHITE;
    pluggedIn = false;
    }//End default constructor

    //toString method
    public String toString(){
        return "\n------Your current headphones are------ " + "\n"
                +"Volume Level: " + volume + "\n"
                +"Headphone Manufacturer: " + manufacturer + "\n"
                +"Model of Headphones: " + headPhoneModel + "\n" 
                +"Color of Headphones: " + headPhoneColor + "\n"
                +"The headphones are plugged in: " + pluggedIn;
    }//End toString

You should try to set the volume:

volume = "The volume";

(And can you show more code to understand entierly how youtr program is working? so add the main mathod to your post so I can understand better wath you are tring to achive)

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