Need Big Help !!

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2005
Posts: 73
Reputation: nabil1983 is an unknown quantity at this point 
Solved Threads: 0
nabil1983 nabil1983 is offline Offline
Junior Poster in Training

Need Big Help !!

 
0
  #1
Mar 22nd, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 121
Reputation: tonakai is an unknown quantity at this point 
Solved Threads: 11
tonakai's Avatar
tonakai tonakai is offline Offline
Junior Poster

Re: Need Big Help !!

 
0
  #2
Mar 22nd, 2005
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 :

  1. ArrayList CDArrayList = new ArrayList(); //simple creation of ArrayList
  2. 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,
  1. ( (CDRecord)CDArrayList.get(0) ).printRecord();
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 73
Reputation: nabil1983 is an unknown quantity at this point 
Solved Threads: 0
nabil1983 nabil1983 is offline Offline
Junior Poster in Training

Re: Need Big Help !!

 
0
  #3
Mar 22nd, 2005
Apreciate the help but truth is i have no idea bout how to apply the changes .
Im a newbie in java and was able to get thus far by reading a java guide, but i dont understand ne further than what i have known......
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 121
Reputation: tonakai is an unknown quantity at this point 
Solved Threads: 11
tonakai's Avatar
tonakai tonakai is offline Offline
Junior Poster

Re: Need Big Help !!

 
0
  #4
Mar 22nd, 2005
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
  1. 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
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 73
Reputation: nabil1983 is an unknown quantity at this point 
Solved Threads: 0
nabil1983 nabil1983 is offline Offline
Junior Poster in Training

Re: Need Big Help !!

 
0
  #5
Mar 22nd, 2005
cheers for tha help.
imma try it in a lil while..

well u could modify my program and highlight it ,, see if i can make an understanding.. lol
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 73
Reputation: nabil1983 is an unknown quantity at this point 
Solved Threads: 0
nabil1983 nabil1983 is offline Offline
Junior Poster in Training

Re: Need Big Help !!

 
0
  #6
Mar 23rd, 2005
OK i've tried wat u said. But some weird reason i keep gettin errors for the arrays i create, tried different solutions none help.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 121
Reputation: tonakai is an unknown quantity at this point 
Solved Threads: 11
tonakai's Avatar
tonakai tonakai is offline Offline
Junior Poster

Re: Need Big Help !!

 
0
  #7
Mar 23rd, 2005
well, let me give you a basic object creation example, and then you convert it to your code

  1. //this is simple point class, stores two integer, which are read-only.
  2. //so you need to specify these two integer while object creation....
  3. public class point {
  4. private int x;
  5. private int y;
  6.  
  7. public point(int x, int y) {
  8. this.x = x;
  9. this.y = y;
  10. }
  11. public int getx() {
  12. return x;
  13. }
  14.  
  15. public int gety() {
  16. return y;
  17. }
  18.  
  19. //somewhere in your main method
  20. point testpoint = new testpoint(50,100);
  21. System.out.println("testpoint location : " + testpoint.getx() + ", " + testpoint.gety() );
  22.  
  23. //you can also pass variables as arguments....
  24. int x;
  25. int y;
  26. point testpoint2; //we only declare point, we define it later...
  27. x = Integer.parseInt(JOptionPane.showInputDialog("X-coordinate?"));
  28. y = Integer.parseInt(JOptionPane.showInputDialog("Y-coordinate?"));
  29.  
  30. //here comes the definition
  31. testpoint2 = new point(x,y);
  32. System.out.println("testpoint2 location : " + testpoint.getx() + ", " + testpoint.gety() );
  33. }

this is basic creation of an object... practice this, then you can start creating objects and putting them into arrays....
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 73
Reputation: nabil1983 is an unknown quantity at this point 
Solved Threads: 0
nabil1983 nabil1983 is offline Offline
Junior Poster in Training

Re: Need Big Help !!

 
0
  #8
Mar 25th, 2005
ok dude i tried the simple object coding u gave, but i keep getting errors. 6 to be exact, Identifier expected etc.......

nething pleez!!!
im gettin kind of desperate for help now!!
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 121
Reputation: tonakai is an unknown quantity at this point 
Solved Threads: 11
tonakai's Avatar
tonakai tonakai is offline Offline
Junior Poster

Re: Need Big Help !!

 
0
  #9
Mar 25th, 2005
well, it was not the actual code, so copy & paste won't work (:
ok, here is the full!!! (:

point.java :
  1. //this is simple point class, stores two integer, which are read-only.
  2. //so you need to specify these two integer while object creation....
  3. public class point {
  4. private int x;
  5. private int y;
  6.  
  7. public point(int x, int y) {
  8. this.x = x;
  9. this.y = y;
  10. }
  11. public int getx() {
  12. return x;
  13. }
  14.  
  15. public int gety() {
  16. return y;
  17. }
  18. }

pointUser.java :
  1. //this class simply creates a point, display something...
  2. public class pointUser {
  3. public static void main(String[] args) {
  4. //some variables...
  5. int x,y;
  6. point mypoint_one = new point(15,20); //decleration and definition
  7. point mypoint_two; //only decleration
  8.  
  9. System.out.println("mypoint_one : "+mypoint_one.getx()+ ", "+ mypoint_one.gety() );
  10. x=100;
  11. y=1;
  12. mypoint_two = new point(x,y);
  13. System.out.println("mypoint_two : "+mypoint_two.getx()+ ", "+ mypoint_two.gety() );
  14. }
  15. }

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
  1. mypoint_one.getx(); //returns mypoint_one.x
  2. mypoint_one.gety(); //returns mypoint_one.y

you can (if you want) give write-access by writing "setter" methods
  1. public void setx(int x) {
  2. this.x = x;
  3. }
  4. public void sety(int y) {
  5. this.y = y;
  6. }

try putting these two setter methods to point.java and then add these lines inside your "main" method in pointUser.java
  1. mypoint_one.setx(50);
  2. mypoint_one.sety(10);
  3. 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....
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC