Results 1 to 16 of 16
Thread: class and object members!
- 01-30-2012, 10:16 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
class and object members!
hi everyone hope everyone is fine
i would like to ask for a bit of help with this program i have got to do
i had to create a java project with 3 classes in one called CD another called Track and the last called TestDrive and i am given some tables to make the code for each class i have managed to sort track code from table see code below with image i worked from.
track code:
image worked fromJava Code:public class track { private String title; private int minutes; private int seconds; public String getTitle() { return title; } public String getLength() { String length; length = (minutes < 10) ? "0" : "" + minutes + ":(seconds < 10) ? 0 : " + seconds; return length; } public String toString(String titleLength) { titleLength = title + " (" + getLength() + ")"; return titleLength; } public track(String title, int minutes, int seconds) { this.title = title; this.minutes = minutes; this.seconds = seconds; } }

i have managed to workout half of the CD code but this is where i am stuck
CD code (half)
this is the image of table working from:Java Code:public class CD { private String track; public String getTitle (String title) { return title; } public String getGenre (String genre) { return genre; } public String getArtist (String artist) { return artist; } public void addTrack (track t) {

as you can see from the image i am up to (track t) bit.
and the third table i dont get which is for the TestDrive class is:

any help would be so grateful
many thanks Andy..
- 01-30-2012, 10:21 PM #2
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Re: class and object members!
worked abit of code for TestDrive but stuck on some of it
public class testdriver
{
public static void main(String [] args)
{
track trackObj = new track("Track Title", 1, 1)
track CDObj = new CD("CD Title", "CD genre", "CD artist")
//forgot how to use scanner for java, so need to fix this accordingly
cout<<trackObj.getTitle()<<trackObj.getG…
is this right?
//OPEN FILES AND READ
dont know this bit
cd text file
Title: Sacred Love
Artist: Sting
Genre: Rock
Track: Inside
Length: 03:34
Title: The Voice
Artist: Russell Watson
Genre: Classical
Track: Nella Fantasia
Length: 04:03
thanks againLast edited by andnlou2678; 01-30-2012 at 10:36 PM.
- 01-31-2012, 09:47 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: class and object members!
What is the problem you have with addTrack()?
Your TestDriver (note the use of capitals for class names) should create a Track and CD (which it is).
In order to test they are working simply compare the values returned by the various getXXX methods and the toSring methods with what you expect:
I've given you the method for printing to the console...you might to put something more sensible in the text, though.Java Code:if (cdObj.getArtist().equals(<whatever the artist name is>)) { System.out.println("Yay..."); } else { System.out.println("Boo. Artist was " + cdObj.getArtist()); }
- 01-31-2012, 11:43 AM #4
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Re: class and object members!
for the CD class file not sure what to put from where i am (what to put next) for what is in the table
- 01-31-2012, 12:02 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: class and object members!
From that table, the relevant bits are:
attribute 'track' of type Track.
method addTrack(), parameter of type Track.
You currently have define:
attribute 'track' of type String.
method addTrack(), parameter of type Track.
(Please note the capitalisation, it's an important Java standard).
So, what do you think needs changing to allow you to make that method?
- 01-31-2012, 12:39 PM #6
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Re: class and object members!
am not sure all i know i need it so the method knows about the other stuff ?
- 01-31-2012, 12:50 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: class and object members!
attribute 'track' of type Track.
...
attribute 'track' of type String.
What is wrong with this picture?
- 01-31-2012, 01:04 PM #8
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Re: class and object members!
this is why am confused sorry!
- 01-31-2012, 01:28 PM #9
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Re: class and object members!
ok i have got this by the time I got to the last step (where you take the data from the .txt file) I didn't need half my code.. like there's no need for the turnery operator anymore or anything like that. but i need to do it that way and not working
this what i have got
and for TestDriveJava Code:public class CD { private String title; private String genre; private String artist; private Track track; public String getTitle() { return title; } public String getGenre() { return genre; } public String getArtist() { return artist; } public void addTrack(Track t) { //track = (t = new Track("Leaving So Soon?", 3, 59)); } public Track getTrack() { return track; } public String toString() { String cd = (title + ": " + artist + " (" + genre + ")"); return cd; } public CD(String artist, String title, String genre) { this.artist = artist; this.title = title; this.genre = genre; } }
just need it to work withe the ternary operator (work as a whole)Java Code:import java.util.*; import java.io.*; public class TestDriver { public static void main (String[] args) { try { Scanner fileInput = new Scanner(new File("CD.txt")); String titleOne, titleTwo; String artistOne, artistTwo; String genreOne, genreTwo; String trackOne, trackTwo; String trackOneLength, trackTwoLength; while (fileInput.hasNext()) { titleOne = fileInput.nextLine(); artistOne = fileInput.nextLine(); genreOne = fileInput.nextLine(); trackOne = fileInput.nextLine(); trackOneLength = fileInput.nextLine(); titleTwo = fileInput.nextLine(); artistTwo = fileInput.nextLine(); genreTwo = fileInput.nextLine(); trackTwo = fileInput.nextLine(); trackTwoLength = fileInput.nextLine(); CD cdOne = new CD(titleOne, artistOne, genreOne); Track newTrackOne = new Track(trackOne, trackOneLength); CD cdTwo = new CD(titleTwo, artistTwo, genreTwo); Track newTrackTwo = new Track(trackTwo, trackTwoLength); System.out.println ("CD: " + cdOne.toString() + "\nTrack: " + newTrackOne.getTitle() + "\nTrack Length: " + newTrackOne.getLength()); System.out.println ("\nCD: " + cdTwo.toString() + "\nTrack: " + newTrackTwo.getTitle() + "\nTrack Length: " + newTrackTwo.getLength()); } fileInput.close(); } catch(IOException e) { System.out.println ("Error reading from file..."); } } }
many thanks Andy
- 01-31-2012, 01:47 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: class and object members!
//track = (t = new Track("Leaving So Soon?", 3, 59));
just
Java Code:track = t;
- 01-31-2012, 02:12 PM #11
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Re: class and object members!
Track newTrackOne = new Track(trackOne, trackOneLength);
Track newTrackTwo = new Track(trackTwo, trackTwoLength);
this is where the red lines are using netbeans
- 01-31-2012, 02:49 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: class and object members!
And what are the errors?
That usually highlights compilation problems.
But, as a hint, look at the constructor for Track.
- 01-31-2012, 03:34 PM #13
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
- 01-31-2012, 03:55 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: class and object members!
Your Track constructor is:
You are trying to create a Track object as follows:Java Code:public Track(String title, int minutes, int seconds)
The former has three parameters, a String and two ints.Java Code:Track newTrackOne = new Track(trackOne, trackOneLength);
You are passing in only two parameters with the latter...both Strings.
Since trackOneLength is a String of "mm:ss" format, you will need to turn that into two ints. One for minutes and one for seconds.
- 01-31-2012, 04:56 PM #15
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Re: class and object members!
sorry am lost m8
- 01-31-2012, 05:25 PM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Drawing an object in my canvas class, the object is created in a separate class
By Hornfreak in forum AWT / SwingReplies: 3Last Post: 05-02-2011, 04:37 AM -
Accessing abstract class subclass's data members
By Claymz in forum New To JavaReplies: 23Last Post: 04-18-2011, 11:26 AM -
access class members with linked list
By billq in forum New To JavaReplies: 5Last Post: 05-09-2010, 05:04 PM -
[newbie] getting class members from Arraylist
By jon80 in forum New To JavaReplies: 16Last Post: 05-15-2009, 07:45 AM -
Doclet that prints out all members of the class
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 11:06 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks