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());
  }
}

1. You have not created a constructor.
2. Why do you have a display method with the parameters your assignment spec said should be inside the constructor?
3. Your spec says you need to create an array of objects, which in this case will be TVshow objects, not an array of Strings
4. If you do not know how to create instances of your class, then you are moving too quickly. And should study up on object programming.
5. If you do know, then you will need a loop, asking for input, create an object using that info in it's constructor, and add each object to an array.

Edited 5 Years Ago by Akill10: n/a

1. You have not created a constructor.
2. Why do you have a display method with the parameters your assignment spec said should be inside the constructor?
3. Your spec says you need to create an array of objects, which in this case will be TVshow objects, not an array of Strings
4. If you do not know how to create instances of your class, then you are moving too quickly. And should study up on object programming.
5. If you do know, then you will need a loop, asking for input, create an object using that info in it's constructor, and add each object to an array.

1. ok, I have added this above the first get method

//contructor that requires values for all of the data fields
  public showListing(String name, String day, int channel)
  {  
	 showName = name;
	 showDay = day;
	 showChannel = channel;
  }

2. Good point, I moved those to the constructor called showListing, now the display method is just this:

public void display()
  {
    System.out.println(showName + " airs on " + showDay + 
	   ", on channel " + showChannel + ".");
  }

3. What do you mean TVShow objects instead of an array of Strings? The problem I'm doing is in the Arrays chapter. this is my first lesson in Arrays so they are still pretty new to me. Creating the TVShow objects is one of my biggest confusions right now.

4. I'm thinking I know. I just took two weeks off of my online class (started a new job) and its hard to come back and remember everything.

5. Is this what you had in mind? I am struggling with this as it is a little more intricate than I'm used to.

for (x = 0; x < show.length; ++x)
	 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:");

Edited 5 Years Ago by curbster: n/a

Good point, I moved those to the constructor called showListing,

1. You cannot call a constructor by any name. It has to have the same name as the class.

What do you mean TVShow objects instead of an array of Strings?

2. Create an array just like you do so for primitive types.

object[] mytvshow = new object[5];

3. Once you create these objects ,get the input from user into local variables and pass them as params to your constructor.

Comments
His feedback was clear and helped me to understand. Thank you!

If you see that I am not understanding something, would you mind giving an example I could follow? thanks!

public class a{
a()
{
//print this is my constructor.
}
}

1. Your constructor has to be TVShow(String name, String day, int channel).

2. To create an array of objects for class a that i've shown , the following syntax has to be followed

a[] myvariableofa = new a[5];

Create it in the same way for your TVShow class.

Edited 5 Years Ago by adarshcu: n/a

Thank you for the examples! I follow those a lot easier. For some reason I'm having a hard time with the terms. I can read the code just fine but when it comes to creating it from a bunch of terms in the directions I struggle. Anyway, following your recommendations I have changed the code as:

in the UseTVShow class:

...
public class UseTVShow
{
  public static void main(String[] args)
  {
  //instantiate a new array with 5 elements.
  TVShow[] show = new TVShow[5];
...

In the TVShow class:

public class TVShow
{
  private String showName = "";
  private String showDay = "";
  private int showChannel = 0;
  
  //contructor that requires values for all of the data fields
  public void TVShow(String name, String day, int channel)
  {  
	 showName = name;
	 showDay = day;
	 showChannel = channel;
  }

so this is a start. Now I have to figure out how to load the array with 2 strings and an int. With just one object I'm not seeing how to do this. If I created 3 arrays or one multi-dimensional array I would understand but how do you load an array with multiple strings and ints from one object?

What you have done above is correct. As regards to your doubts that you will have to create a multidimensional array to store 2 strings and an int , you are wrong. Now that you have got an array of objects.

for(all elements in object array)
{
//get variable values from user.
//create a new TVShow object , pass the variables to the constructor.
 show[i] = TVShow object created above.
}

Hope you get the point here. You load an object into the array of abjects , and not individual data. That is the whole point of an object and a class.

Edited 5 Years Ago by adarshcu: n/a

Thank you for the examples! I follow those a lot easier. For some reason I'm having a hard time with the terms. I can read the code just fine but when it comes to creating it from a bunch of terms in the directions I struggle. Anyway, following your recommendations I have changed the code as:

in the UseTVShow class:

...
public class UseTVShow
{
  public static void main(String[] args)
  {
  //instantiate a new array with 5 elements.
  TVShow[] show = new TVShow[5];
...

In the TVShow class:

public class TVShow
{
  private String showName = "";
  private String showDay = "";
  private int showChannel = 0;
  
  //contructor that requires values for all of the data fields
  public void TVShow(String name, String day, int channel)
  {  
	 showName = name;
	 showDay = day;
	 showChannel = channel;
  }

so this is a start. Now I have to figure out how to load the array with 2 strings and an int. With just one object I'm not seeing how to do this. If I created 3 arrays or one multi-dimensional array I would understand but how do you load an array with multiple strings and ints from one object?

My original post was assuming you understood those different terms, apologies.

This article has been dead for over six months. Start a new discussion instead.