3 Attachment(s)
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:
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;
}
}
image worked from
Attachment 2779
i have managed to workout half of the CD code but this is where i am stuck
CD code (half)
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)
{
this is the image of table working from:
Attachment 2780
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:
Attachment 2781
any help would be so grateful
many thanks Andy..
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 again
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:
Code:
if (cdObj.getArtist().equals(<whatever the artist name is>)) {
System.out.println("Yay...");
} else {
System.out.println("Boo. Artist was " + cdObj.getArtist());
}
I've given you the method for printing to the console...you might to put something more sensible in the text, though.
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
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?
Re: class and object members!
am not sure all i know i need it so the method knows about the other stuff ?
Re: class and object members!
attribute 'track' of type Track.
...
attribute 'track' of type String.
What is wrong with this picture?
Re: class and object members!
this is why am confused sorry!
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
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;
}
}
and for TestDrive
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...");
}
}
}
just need it to work withe the ternary operator (work as a whole)
many thanks Andy
Re: class and object members!
//track = (t = new Track("Leaving So Soon?", 3, 59));
just
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
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.
1 Attachment(s)
Re: class and object members!
can you please help me with this bit it just doesnt like the track and dont understand why
it wont run in netbeans because it has those two red lines with that code
Attachment 2783
thats the only red i get
Re: class and object members!
Your Track constructor is:
Code:
public Track(String title, int minutes, int seconds)
You are trying to create a Track object as follows:
Code:
Track newTrackOne = new Track(trackOne, trackOneLength);
The former has three parameters, a String and two ints.
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.
Re: class and object members!
Re: class and object members!
Then you really need to go back and read your notes on constructors, methods and parameters.