Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Can you help me with my code

/*need to define two set methods to set the title and numPlay of the Music,
define two get methods to retrieve the title and numPlay of the Music,
to define the isPopular() method. If the numPlay is greater that 1000, the methods returns
true otherwise returns false, to create a Music object with title "Radetzky March" 
and the numPlay 5,000, and display a string containing the value of the object's instance variables.*/

public class Music{
  private String title;
  private int numPlay;

  public Music( ){
    title = “”;
    numPlay = 0;
  }
  public boolean isPopular(){ 
    //…………….
  }
  public String toString(){
    return String.format( "%s is played %d", title, numPlay);
  }
} //end class definition

3 Answers

Hi Mario,

The two set methods would look something like:

public void setTitle(String title){
  this.title = title;
}

public void setNumPlay(int plays){
  this numPlay = plays;
}

They just set the values of the two private member variables. The two get methods do the opposite - they retrieve the values held within the private member variables and might look something like this:

public String getTitle(){
  return this.title;
}

public int getNumPlay(){
  return this.numPlay;
}

The isPopular method returns a boolean depending on whether numPlay is greater than 1,000. That might look like:

public boolean isPopular(){
  return numPlay > 1000;
}

That leaves you to create an instance of this class then call the `toString method on that. I'll leave that with you. :wink:

Steve.

Hi Steve,

Awesome!!! Thank you so much!!!!!

Also thank you for explaining, really helped me a lot :p