944,205 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2036
  • Java RSS
Mar 22nd, 2005
0

Need Big Help !!

Expand Post »
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
Similar Threads
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
nabil1983 is offline Offline
73 posts
since Mar 2005
Mar 22nd, 2005
0

Re: Need Big Help !!

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 :

Java Syntax (Toggle Plain Text)
  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,
Java Syntax (Toggle Plain Text)
  1. ( (CDRecord)CDArrayList.get(0) ).printRecord();
Reputation Points: 25
Solved Threads: 11
Junior Poster
tonakai is offline Offline
121 posts
since Feb 2005
Mar 22nd, 2005
0

Re: Need Big Help !!

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......
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
nabil1983 is offline Offline
73 posts
since Mar 2005
Mar 22nd, 2005
0

Re: Need Big Help !!

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
Java Syntax (Toggle Plain Text)
  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
Reputation Points: 25
Solved Threads: 11
Junior Poster
tonakai is offline Offline
121 posts
since Feb 2005
Mar 22nd, 2005
0

Re: Need Big Help !!

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
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
nabil1983 is offline Offline
73 posts
since Mar 2005
Mar 23rd, 2005
0

Re: Need Big Help !!

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.
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
nabil1983 is offline Offline
73 posts
since Mar 2005
Mar 23rd, 2005
0

Re: Need Big Help !!

well, let me give you a basic object creation example, and then you convert it to your code

Java Syntax (Toggle Plain Text)
  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....
Reputation Points: 25
Solved Threads: 11
Junior Poster
tonakai is offline Offline
121 posts
since Feb 2005
Mar 25th, 2005
0

Re: Need Big Help !!

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!!
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
nabil1983 is offline Offline
73 posts
since Mar 2005
Mar 25th, 2005
0

Re: Need Big Help !!

well, it was not the actual code, so copy & paste won't work (:
ok, here is the full!!! (:

point.java :
Java Syntax (Toggle Plain Text)
  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 :
Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  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....
Reputation Points: 25
Solved Threads: 11
Junior Poster
tonakai is offline Offline
121 posts
since Feb 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: A question on java script?(Please answer ASAP)
Next Thread in Java Forum Timeline: Want to use radio button or menu bar instead of combo box





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC