0

What I'am trying to do with this program is output the information of a song using the toString on Song class. When I output it, everything is fine except the SongType/genre. It is still outputting UNDETERMINED.

 abstract class Song implements ISong //SONG CLASS
 {
private String name; 
private String rating;
private int id;
private SongType genre;

public Song()
{
    name = " ";
    rating = " ";
    id = 0;
    genre = SongType.UNDETERMINED;
}
public Song(String name, String rating, int id)
{
    this.name = name;
    this.rating = rating;
    this.id = id;
    this.genre =Song.UNDETERMINED;
}
public void setName(String name)
{
    this.name = name;
}
public void setRating(String rating)
{
    this.rating = rating;
}
public void setID(int id)
{
    this.id = id;
}
public String getName()
{
    return(this.name);
}
public String getRating()
{
    return(this.rating);
}
public int getID()
{
    return(this.id);
}


@Override
public String toString()
{
    return("Song: " + this.name +
           "\nID: " + this.id +
           "\nRating: " + this.rating +
           "\nGenre: " + this.genre);
}
}

class Pop extends Song //POP CLASS
{   

public Pop(String name, String rating, int id)
{
    super(name, rating, id);
}
}

interface ISong //INTERFACE
{
public enum SongType {POP, COUNTRY, HIPHOP, SOUL, UNDETERMINED;}

}

public class test{
public static void main(String [] args)
{
    Song one = new Pop("Pop Song", "Five", 123); 
    System.out.println(one);
}

}

1 Answers1

2

When I output it, everything is fine except the SongType/genre. It is still outputting UNDETERMINED.

But where do you actually set your genre field to anything but SongType.UNDETERMINED?

I suggest that you give the Song class and ISong interface a public SongType getGenre() method that returns the current genre, as well as an appropriate setter method, public void setGenre(SongType genre) and constructors that accept a SongType genre parameter if need be. The toString() method should call the getGenre() method to get the current genre state.

Most important, you will need to set the genre in a concrete class to something other than SongType.UNDETERMINED before trying to print it out.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Would it be possible for the constructor in Pop to change the genre to pop or do I need a getter and setter? – edward lavaire Jul 22 '15 at 16:39
  • @edwardlavaire: try it and see. :) But regardless, you absolutely need a getter. A setter is needed only if you want the field to be mutable, and don't set it in the constructor. – Hovercraft Full Of Eels Jul 22 '15 at 16:39
  • I tried but since genre is private, it wont let me change it – edward lavaire Jul 22 '15 at 16:53
  • @edwardlavaire: so the abstract class will in fact need to have the setter method, and make sure that it's a `final` method so that it can't be overridden and thus can be safely used in child class constructors if need be. .. or make the field protected. – Hovercraft Full Of Eels Jul 22 '15 at 16:59