Results 1 to 16 of 16
  1. #1
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default 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:

    Java 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
    class and object members!-untitled-3.png

    i have managed to workout half of the CD code but this is where i am stuck

    CD code (half)


    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)
       {
    this is the image of table working from:
    class and object members!-untitled.png

    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:

    class and object members!-untitled-2.png

    any help would be so grateful

    many thanks Andy..

  2. #2
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default 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
    Last edited by andnlou2678; 01-30-2012 at 10:36 PM.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default 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:
    Java 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.

  4. #4
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default 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

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default 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?

  6. #6
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: class and object members!

    am not sure all i know i need it so the method knows about the other stuff ?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: class and object members!

    attribute 'track' of type Track.
    ...
    attribute 'track' of type String.

    What is wrong with this picture?

  8. #8
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: class and object members!

    this is why am confused sorry!

  9. #9
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default 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


    Java 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

    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...");
    	}
        }
    }
    just need it to work withe the ternary operator (work as a whole)

    many thanks Andy

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: class and object members!

    //track = (t = new Track("Leaving So Soon?", 3, 59));
    just
    Java Code:
    track = t;

  11. #11
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default 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

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default 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.

  13. #13
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default 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

    class and object members!-untitled.png


    thats the only red i get

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: class and object members!

    Your Track constructor is:
    Java Code:
    public Track(String title, int minutes, int seconds)
    You are trying to create a Track object as follows:
    Java 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.

  15. #15
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: class and object members!

    sorry am lost m8

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: class and object members!

    Then you really need to go back and read your notes on constructors, methods and parameters.

Similar Threads

  1. Replies: 3
    Last Post: 05-02-2011, 04:37 AM
  2. Accessing abstract class subclass's data members
    By Claymz in forum New To Java
    Replies: 23
    Last Post: 04-18-2011, 11:26 AM
  3. access class members with linked list
    By billq in forum New To Java
    Replies: 5
    Last Post: 05-09-2010, 05:04 PM
  4. [newbie] getting class members from Arraylist
    By jon80 in forum New To Java
    Replies: 16
    Last Post: 05-15-2009, 07:45 AM
  5. Doclet that prints out all members of the class
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •