Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By JosAH

Thread: Writing a tracklist class

  1. #1
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Cool Writing a tracklist class

    I am trying to write a tracklist class. It basically stores tracks for a CD as Strings. I think my code is okay. The only problem is the add method.

    Java Code:
    class tracklist {
    //define variables
    	CD[] a;
    	int numElements;
    //no arg constructer    
       tracklist() {
    		a = new CD[100];
    		numElements = 0;		
       }
    	public boolean add(String track) {
    		if (numElements <= 100) {
    			a[numElements] = track;
          	    numElements++; 
    			return true;
    		} else {
                return false;
            }
    	}
    	public int count() {
    		return numElements;
    	}
       public void display(int indent){
            for (int i = 1; i <= 100; i++){
    			System.out.println(i + " " + a[i]);
            }
    	}
    }
    I'm trying to get my code to compile but I get the following error message:

    Assignment3.java:62: incompatible types
    found : java.lang.String
    required: assignment3.CD
    a[numElements] = track;
    ^
    1 error
    How would I fix this??

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,189
    Blog Entries
    7
    Rep Power
    24

    Default Re: Writing a tracklist class

    Your array contains CD type elements; you are trying to store a String type element in there (that's what your compiler said). For now (I think) changing your array a to a String[] will do. b.t.w. why are you trying to display 100 elements when only 'numElements' are stored in your array?

    kind regards,

    Jos
    son012189 likes this.
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Default Re: Writing a tracklist class

    Thanks, I changed the array to a String[] and got it to compile. I have no idea why I was trying to display 100 elements. I didn't notice that. I'll fix that part up.

Similar Threads

  1. Writing generic array class
    By aqeel2010 in forum New To Java
    Replies: 6
    Last Post: 11-29-2011, 07:01 AM
  2. Writing a class
    By smray7 in forum New To Java
    Replies: 3
    Last Post: 11-30-2010, 07:16 PM
  3. Help with writing a class
    By MasqueradeToday in forum New To Java
    Replies: 1
    Last Post: 10-07-2010, 11:53 PM
  4. writing to text Area through Class
    By dilpreet28 in forum New To Java
    Replies: 3
    Last Post: 07-06-2010, 12:11 PM
  5. writing an array class
    By wardd85 in forum New To Java
    Replies: 5
    Last Post: 07-16-2008, 10:59 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
  •