Results 1 to 4 of 4
- 12-01-2009, 09:21 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
So Confused (array/arrayList in constructor)
This is the first Java Lab that I have encountered which I can not complete on my own, and it has started to really frustrate me.
THANKS FOR READING
My Lab Instructions
Here is what I have come up with so farWrite a program that keeps a list of all our CDs. Each CD contains the name of CD, name of songs, and the name of artist. For this purpose, there is a class called CD with the following instance fields:
String cdName
String[] songName (you can assume that each CD has 20 songs)
String artistName
You need to implement a method called printCD that prints the name of all songs.
There will be another class called CDList that is an array list of CDs. This is basically your CD collection. For this class, you will have a method that calls the printCD method of CD class to print the name of all songs in your CD collection.
The CDTester, should create objects belong to CD and pass them to CDList to keep them in an array list. Then the name of all the songs should be printed.
The Code Above compiles without syntax problemsJava Code:/** * Write a description of class CD here. * * @author (your name) * @version (a version number or a date) */ public class CD { // instance variables - replace the example below with your own private String albumName=""; private String artistName=""; String[] songName=new String[20]; /** * Constructor for objects of class CD */ public CD(String albumName, String artistName, String[] songName) { // initialise instance variables albumName=albumName; artistName=artistName; songName=songName; } }
HOWEVER I keep getting the error "Cannot find symbol - Constructor CD()"
on the code *CD album = new CD(); //want to create a new object called cdd*
Java Code:import java.util.Scanner; import java.util.ArrayList; /** * Write a description of class CDTester here. * * @author (your name) * @version (a version number or a date) */ public class CDTester { public static void main(String[] args) { Scanner in=new Scanner(System.in); //name the collection I want the user to make a new playlist System.out.println("Please Enter collection name: "); String playList = in.next(); System.out.println(""); //loop until user promps exit Then I want the user to be able to add cds to the playlist int i = 1; while(i==1) { //Users Options System.out.println("Enter 1 to add CD to " + playList + "'s collection"); System.out.println("Enter 2 to display current songs in " + playList + "'s collection"); System.out.println("Enter 0 to exit program"); System.out.print("Please select an option: "); int input = in.nextInt(); System.out.println(""); //Execute the users command if(input==1) //add cd { System.out.println("Enter CD Name "); String cdName=in.next(); CD album = new CD(); //Want to create a new object called cd //collectionName.add(new CD(artistName));//set to add new item to array list System.out.println("Enter Artist Name: "); artistName=in.next(); //loop to gather song names int count = 0; for(int x=-1; x<=18; x++) { count=count+1; System.out.println("Enter Song " + count + ": "); String name=in.next(); songName[x]= ame; } } else if(input==2) //output songlist back to user { System.out.println("Option 2"); System.out.println("Song List"); System.out.println(""); //printCD(collectionName); } else if(input==0) //exit program { System.out.println("Program Exited"); i=0; } else //error has occured System.out.println("An error has occured: "); System.out.println(""); } } }
I have to leave for work, but I hope I left you with enough info to receive some tips and help me correct my errors.
NOTE: Can I make a constructor with an array or arrayList in the parameters?
Thanks in advance for your comments, and I will get back to you in 5 hours.
- 12-01-2009, 09:43 PM #2
You either need to add parameters to your new CD or create a default constructor and add in getter/setter methods to your CD class.
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 12-01-2009, 09:44 PM #3
And yes your constructor can accept any valid object type.
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 12-01-2009, 09:53 PM #4
Senior Member
- Join Date
- Nov 2009
- Posts
- 150
- Rep Power
- 4
like he said.
see it as a printer.
a printer wont print unless you tell hem what to print.
new printTask(); wont work cause what does he has to print
new printTask("print this") will work.
this is because printers are made to accept that string and print it.
your method cd
public CD(String albumName, String artistName, String[] songName)
is made so it only creates a cd if you give him an albumName,artistName and songnames.
if you call new CD() he doesn't do something because you don't tell hem what he wants. a printer wants text, your cd wants an albumName,artistName and songnames.
Similar Threads
-
Problem with using an array in a constructor
By planesinspace in forum New To JavaReplies: 3Last Post: 08-28-2009, 09:17 AM -
Sending an array in a constructor?
By dch414 in forum New To JavaReplies: 2Last Post: 09-14-2008, 09:59 PM -
Array Constructor
By Javanoob828282 in forum New To JavaReplies: 1Last Post: 04-30-2008, 10:25 PM -
problems with asigning elements of an array to a constructor
By rednessc in forum New To JavaReplies: 1Last Post: 12-14-2007, 07:25 AM -
Use ArrayList Constructor...
By romina in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:37 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks