Hi I'm in my first Java class, towards the end and am struggling with some code dealing with arrays. I'm not looking for the answers but am spinning my wheels (as usual) and am stuck on a particular problem. I'll list the problem here and then explain where I am in the process. Any help will be greatly appreciated!
Problem:
"Create a class named TVShow.java. Data Fields can
include a String containing the Name of the show,
a String containing the day that the show is
broadcast(i.e. “Monday”)and an integer for the channel
where the show can be viewed. Methods will include a
constructor that requires values for all of the data
fields, plus methods to get, set and display the values
of the data fields. Create a UseTVShow.java file that
prompts the user to input up to 5 TV shows and
stores the data in an array of objects first, then
displays them as a list."
I am having a hard time figuring out how to link the two classes together with Arrays, more specifically the display() method within the TVShow class. Then in the UseTVShow class I need help using the get(), set(), and display() methods because there is something I am just not understanding. Take a look at the code and see if you can find my mistakes or what my code is lacking.
Thanks!
import java.util.*;
import javax.swing.*;
public class TVShow
{
private String showName;
private String showDay;
private int showChannel;
//get methods
public String getShowName()
{
return showName;
}
public String getDay()
{
return showDay;
}
public int getChannel()
{
return showChannel;
}
//set methods
public void setShowName(String nameOfShow)
{
showName = nameOfShow;
}
public void setDay(String dayOfWeek)
{
showDay = dayOfWeek;
}
public void setChannel(int myChannel)
{
showChannel = myChannel;
}
//display's the show name, day, and channel.
public void display(String name, String day, int channel)
{
showName = name;
showDay = day;
showChannel = channel;
System.out.println(showName + " airs on " + showDay + ", on channel " +
showChannel + ".");
}
}
import java.util.*;
import javax.swing.*;
public class UseTVShow
{
public static void main(String[] args)
{
//instantiate a new array with 5 elements.
//TVShow[] show = new TVShow[5];
String[] show = new String[5];
int highestSub = show.length - 1;
int x = 0;
String showString = "";
show[x] = JOptionPane.showInputDialog(null,
"Enter a list of up to 5 TV shows, or xxx to quit:");
while(!show[x].equals("xxx") && x < highestSub)
{
showString = showString + show[x] + "\n";
++x;
if(x < highestSub)
show[x] = JOptionPane.showInputDialog(null,
"Enter a list of up to 5 TV shows, or xxx to quit:");
}
for (x = 0; x < show.length; ++x)
System.out.println(show[x].display());
}
}