How to properly set Getters setters and convertmintosec?

Viewed 153

Basically I'm EXTREMELY new to java, I'm struggling HARD CORE I've gotten this far

public class Song {

  //Instance field

  private String Maker;
  private String Name;
  private int Length;
  private String Type;
  private int yearReleased;


    //0 Arg

    public Song() {
        Maker = "Logic";
        Name = "1-800";
        Length = 4;
        Type = "Hip Hop";
        yearReleased = 2017;
    }

    //Multi Arg

    public Song(String Maker, String Name, int Length, String Type) {
        this.Maker = Maker;
        this.Name = Name;
        this.Length = Length;
        this.Type = Type;
        this.yearReleased = yearReleased;
    }

    public String getyearReleased() {
        return Song;
    }
    public void setName(string name) {
        this.yearReleased = yearReleased;
    }
    public String getyearReleased() {
        return yearReleased;
    }

    //Method

    public String toString() {
        String output;
        output = "The artist of this song is " + Maker + "\n" +
             "The name of this song is called " + Name + "\n" +
             "This song is " + Length + " Minutes long" + "\n" +
             "The genre of this song is " + Type;
        return output;
     }
}

I already made the driver. Yes this will have an error, I also need to add convertMinToSec() but what is happening? how do i properly set up the getters setters AND the convert to min sec?

You know when your teacher starts off at 2+2 you look away and look back and they are at 2>(2+2)^8 = x/5*3ve+32% Yea,I would compare this to that.

Someone please help me by telling me how to properly set up getters setters and convert min to sec. I have already tried to properly set but failed miserably.

My expected result is for it to recall the output correctly and the year released but it doesn't it errors by saying incorrect symbols, this is my first time making a post so I know it's going to seem absolutely stupid, but to me it's not, I looked online and couldn't find good content about this, I'm relying on you guys to show noobies like me the ropes when I don't know something! Thank you for taking your time to answer if you answer.

3 Answers

Let me try to explain to you the concept of getters and setters.

Your class Song has some private data fields. It means that they are hidden from the outside world. Now, if any entity outside this class needs to use these fields, it will have to go through an additional step. This step is either a getter or a setter method.

The getter method(s) helps the entities get (or retrieve) the value from these fields. And the setter method(s) helps set (or assign) values to these fields.

Remember that getters and setters are to get from/set to the fields, and not the whole class. Which is why there is a problem with the following:

public String getyearReleased() {
        return Song;
}

In the above code, you first say that you want to return a String from the getyearReleased() method, but you see that you are actually returning Song, which is a class.

Here is a simple example of your class with 2 fields and how you get/set them through a main() method in an outside class.

public class Song {
    private String songName;
    private int yearReleased;

    // Getter method for Song Name
    public String getSongName() {
        System.out.println("Getting the song name.");
        return songName;
    }
    // Getter method for Year of Release
    public int getYearReleased() {
        System.out.println("Getting the year of release.");
        return yearReleased;
    }

    // Setter method for Song Name
    public void setSongName(String mySong) {
        System.out.println("Setting the song name.");
        this.songName = mySong;
    }
    // Setter method for Year of Release
    public void setYearReleased(int mySongYear) {
        System.out.println("Setting the year of release.");
        this.yearReleased = mySongYear;
    }

}

You should note that the setters don't return anything because they are just setting the values. But the getters return the same data type as of the field being retrieved.

Here is the class from which you can call the getter and setter methods. This class will contain the main() method:

public class SongDemo {
    public static void main(String[] args) {
        Song demoSong = new Song();

        // call the setters
        demoSong.setSongName("Summer of 69");
        demoSong.setYearReleased(1988);

        // call the getters and print the values returned
        System.out.println(demoSong.getSongName());
        System.out.println(demoSong.getYearReleased());
    }
}

This is what your output looks like:

Setting the song name.
Setting the year of release.
Getting the song name.
Summer of 69
Getting the year of release.
1988

Hope it helps.

There are generally accepted conventions that should be followed - it makes reading the code easier.

All variable names should start with a lower case letter. Example:

private String Maker;

should be

private String maker;

Getters / Setters should be named in a camel-case way starting with get/set. This makes it what is known as a POJO (plain-old-java-object). Many libraries require this naming of getter/setters to work correctly. Example:

public String getyearReleased()

should be

public int getYearReleased() {
    return yearReleased;
}

A getter should just return the variable that matches the getter method name. Some of your getters are returning other things. Don't do this.

Converting minutes to seconds. Notice the camel-cap way to name the method.

public long convertMinToSecond(long minutes) {
    return minutes * 60;
}

Well don't get overwhelmed and start at the beginning:

First "rule": all your variables should start small maker not Maker. Like yearReleased . This is called camelCase because the words look like they have "humps" like a camel - but ever saw a camel start with a hump?

This leads us to the second "rule": Class names start with a capital letter like String or Song.

And there is the first typo:

public string getyearReleased(){    // it should say String not string 
   return yearReleased;
}

And to be absolutely coherent, since your variable is called yearReleased your method should respect the writing of the variable and be called getYearReleased.

Now you have this method two times in your code: The first occurrence says return Song; which does not make sense. (you want the "year" not the "song"). But since you have the second occurrence, you can delete that one.

So you have the "getter" for yearReleased, now you need a "setter"

public void setYearReleased(int yearReleased){
    this.yearReleased=yearReleased;
}

See how this is exactly like in your constructor? You give the method a variable called "yearReleased" and it will assign the value to the "global variable" this.yearReleased. And there you have set the value to your object.

Basically getter/setters are like:

 private String maker;   // your global field / variable / member ... whatever you want to call it

 /** the getter */
 public String getMaker(){   //fyi because you are taping two words together the second one gets a capital letter.
    return this.maker;
 }

 /** the setter */
 public void setMaker(String maker){
    this.maker = maker;        
 }

The setters would also work with

 public void setMaker(String theMakerOfThisAwesomeSong){
    this.maker = theMakerOfThisAwesomeSong;
 }

same result.

Once you have written all your getters and setters - normally this is done for all fields like maker, name, length, type, yearReleased - then you can fix your constructor:

public Song(String maker, String name, int length, String type, int yearReleased){   // you forgot to include the yearReleased 
    ...
}

Last but not least, the method convertMinToSec. I guess it should convert the length of the song in seconds, which basically is like length*60.

Don't ask me about the necessity of such a method - you just can do it:

 public int convertMinToSec(){
      return // calculate how many seconds the song lasts
 }

That's it. Sorry if this is so long, but I hope it helps you anyway.

Related