| | |
Need Big Help !!
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2005
Posts: 73
Reputation:
Solved Threads: 0
Hi all,
Ok i've started doing a mini project about a CD collection database.
i got the program working ata stage where you can enter a single entry and the record is printed to the screen, the problem im having is i can not seem to modify the program to store an array of CD 'objects' . Each entry in the array needs to be a single object describing a CD.
I also need to add a menu of options like:
1: Input New Entry
2: Print All Entries
3: Quit
if anyone can help i would really apreciate the help.
below is my program of where i have got to so far::
If you copy and paste this you will see it works.
import javax.swing.*;
public class CdStorage
{
public static void main (String[] args)
{
int menu_choice;
CdRecord one = new CdRecord();
one.artist_name = JOptionPane.showInputDialog("Enter artist name.");
one.album_name = JOptionPane.showInputDialog("Enter album name.");
one.no_of_tracks =Integer.parseInt(JOptionPane.showInputDialog("Enter the number of tracks on the album"));
one.printCdRecord();
}//end main
} // end class CdStorage
class CdRecord
{
public String artist_name;
public String album_name;
public int no_of_tracks;
public CdRecord (String artist, String album, int tracks, int year)
{
artist_name = artist; //stores first argument passed into artist_name
album_name = album; //stores second argument passed into album_name
no_of_tracks = tracks; //stores third argument passed into no_of_tracks
}
public CdRecord()
{
artist_name = "A";
album_name = "B";
no_of_tracks = 0;
}
public void printCdRecord ()
{
String o = "Artist Name: " + artist_name + "\nAlbum Name: " +album_name+"\nNo. Of Tracks: " + no_of_tracks;;
System.out.println(o);
}
}//end class cdstorage
Ok i've started doing a mini project about a CD collection database.
i got the program working ata stage where you can enter a single entry and the record is printed to the screen, the problem im having is i can not seem to modify the program to store an array of CD 'objects' . Each entry in the array needs to be a single object describing a CD.
I also need to add a menu of options like:
1: Input New Entry
2: Print All Entries
3: Quit
if anyone can help i would really apreciate the help.
below is my program of where i have got to so far::
If you copy and paste this you will see it works.
import javax.swing.*;
public class CdStorage
{
public static void main (String[] args)
{
int menu_choice;
CdRecord one = new CdRecord();
one.artist_name = JOptionPane.showInputDialog("Enter artist name.");
one.album_name = JOptionPane.showInputDialog("Enter album name.");
one.no_of_tracks =Integer.parseInt(JOptionPane.showInputDialog("Enter the number of tracks on the album"));
one.printCdRecord();
}//end main
} // end class CdStorage
class CdRecord
{
public String artist_name;
public String album_name;
public int no_of_tracks;
public CdRecord (String artist, String album, int tracks, int year)
{
artist_name = artist; //stores first argument passed into artist_name
album_name = album; //stores second argument passed into album_name
no_of_tracks = tracks; //stores third argument passed into no_of_tracks
}
public CdRecord()
{
artist_name = "A";
album_name = "B";
no_of_tracks = 0;
}
public void printCdRecord ()
{
String o = "Artist Name: " + artist_name + "\nAlbum Name: " +album_name+"\nNo. Of Tracks: " + no_of_tracks;;
System.out.println(o);
}
}//end class cdstorage
well, first you need to use ArrayList class, this class enables you to add and delete elements from it, check out API documentation.
after you hava an ArrayList just use Add method to add an element to your ArrayList...
an example :
then when you want to use them again in your code, you typecast "object" to "CDRecord" like this,
after you hava an ArrayList just use Add method to add an element to your ArrayList...
an example :
Java Syntax (Toggle Plain Text)
ArrayList CDArrayList = new ArrayList(); //simple creation of ArrayList CDArrayList.add(one); //"one" from your code
then when you want to use them again in your code, you typecast "object" to "CDRecord" like this,
Java Syntax (Toggle Plain Text)
( (CDRecord)CDArrayList.get(0) ).printRecord();
well there are some points you need to fix, before going on to other subject,
generally in oop we don't make our variables "public" so make them "private" then you need to change the way you create your CDRecord object, like
well, i could re-write the code but that doesn't give anything, hope i could help by trying to give you some clues
generally in oop we don't make our variables "public" so make them "private" then you need to change the way you create your CDRecord object, like
Java Syntax (Toggle Plain Text)
CDRecord one = new CDRecord(artist, album, tracks); //you need to create artist, album and tracks variables like you did
well, i could re-write the code but that doesn't give anything, hope i could help by trying to give you some clues
well, let me give you a basic object creation example, and then you convert it to your code
this is basic creation of an object... practice this, then you can start creating objects and putting them into arrays....
Java Syntax (Toggle Plain Text)
//this is simple point class, stores two integer, which are read-only. //so you need to specify these two integer while object creation.... public class point { private int x; private int y; public point(int x, int y) { this.x = x; this.y = y; } public int getx() { return x; } public int gety() { return y; } //somewhere in your main method point testpoint = new testpoint(50,100); System.out.println("testpoint location : " + testpoint.getx() + ", " + testpoint.gety() ); //you can also pass variables as arguments.... int x; int y; point testpoint2; //we only declare point, we define it later... x = Integer.parseInt(JOptionPane.showInputDialog("X-coordinate?")); y = Integer.parseInt(JOptionPane.showInputDialog("Y-coordinate?")); //here comes the definition testpoint2 = new point(x,y); System.out.println("testpoint2 location : " + testpoint.getx() + ", " + testpoint.gety() ); }
this is basic creation of an object... practice this, then you can start creating objects and putting them into arrays....
well, it was not the actual code, so copy & paste won't work (:
ok, here is the full!!! (:
point.java :
pointUser.java :
simply create these two files, and compile pointUser, then run pointUser...
it is very simple, in this example you create points, which have read-only properties x and y. you can't hava direct access to these members, but you can get their values by calling
you can (if you want) give write-access by writing "setter" methods
try putting these two setter methods to point.java and then add these lines inside your "main" method in pointUser.java
then recompile and run pointUser.
so why are we using this? instead just making everything "public"?
because we want controlled access to members of objects, with these "getters" and "setter" we can have control over members, if we want read-only we make only getter. if we want read-write, we make getter and setter, and so on (:
another thing is construction of point, point(int, int) constructor is simple, it just assign starting values... because we don't have setters in the point class, only place we can assign values is constructors.
well, try to adopt this point class to your CDRecord class, I think this will be a good excercise...
then when you feel comfortable with simple classes, you can move to array stuff...
well, hope this helps....
ok, here is the full!!! (:
point.java :
Java Syntax (Toggle Plain Text)
//this is simple point class, stores two integer, which are read-only. //so you need to specify these two integer while object creation.... public class point { private int x; private int y; public point(int x, int y) { this.x = x; this.y = y; } public int getx() { return x; } public int gety() { return y; } }
pointUser.java :
Java Syntax (Toggle Plain Text)
//this class simply creates a point, display something... public class pointUser { public static void main(String[] args) { //some variables... int x,y; point mypoint_one = new point(15,20); //decleration and definition point mypoint_two; //only decleration System.out.println("mypoint_one : "+mypoint_one.getx()+ ", "+ mypoint_one.gety() ); x=100; y=1; mypoint_two = new point(x,y); System.out.println("mypoint_two : "+mypoint_two.getx()+ ", "+ mypoint_two.gety() ); } }
simply create these two files, and compile pointUser, then run pointUser...
it is very simple, in this example you create points, which have read-only properties x and y. you can't hava direct access to these members, but you can get their values by calling
Java Syntax (Toggle Plain Text)
mypoint_one.getx(); //returns mypoint_one.x mypoint_one.gety(); //returns mypoint_one.y
you can (if you want) give write-access by writing "setter" methods
Java Syntax (Toggle Plain Text)
public void setx(int x) { this.x = x; } public void sety(int y) { this.y = y; }
try putting these two setter methods to point.java and then add these lines inside your "main" method in pointUser.java
Java Syntax (Toggle Plain Text)
mypoint_one.setx(50); mypoint_one.sety(10); System.out.println("mypoint_one : "+mypoint_one.getx()+ ", "+ mypoint_one.gety() );
then recompile and run pointUser.
so why are we using this? instead just making everything "public"?
because we want controlled access to members of objects, with these "getters" and "setter" we can have control over members, if we want read-only we make only getter. if we want read-write, we make getter and setter, and so on (:
another thing is construction of point, point(int, int) constructor is simple, it just assign starting values... because we don't have setters in the point class, only place we can assign values is constructors.
well, try to adopt this point class to your CDRecord class, I think this will be a good excercise...
then when you feel comfortable with simple classes, you can move to array stuff...
well, hope this helps....
![]() |
Similar Threads
- PHP vs ASP... the big ShOwdOwN (IT Professionals' Lounge)
- Motorola T720 Phone and Verizon Wireless (Cellphones, PDAs and Handheld Devices)
- Big Game need progrmmers (C++)
- Is my sig to big? (Geeks' Lounge)
Other Threads in the Java Forum
- Previous Thread: A question on java script?(Please answer ASAP)
- Next Thread: Want to use radio button or menu bar instead of combo box
| Thread Tools | Search this Thread |
android api applet application array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) calculator chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia link linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing threads time tree unlimited webservices windows





