You are viewing our Forum Archives. To view or take place in current topics click here.
Java. Help with arrays.
Posted:

Java. Help with arrays.Posted:

-Patman
  • TTG Master
Status: Offline
Joined: Jun 29, 201014Year Member
Posts: 823
Reputation Power: 34
Status: Offline
Joined: Jun 29, 201014Year Member
Posts: 823
Reputation Power: 34
I'm learning Java, I'm not the greatest so please don't laugh. Basically what I'm trying to do here is to write a method which allows me to find the longest track in the array. The method that I've written doesn't seem to work so any help would be appreciated!




public class CD
{
  private String artist;
  private String title;
  private int numTracks;
  // Declare array called tracks here
  private Track[] tracks;

  public CD(String a, String t)
  {
    artist = a;
    title = t;
    // Initialise numTracks here the current number of tracks in the array tracks
    numTracks = 0;
    tracks = new Track[8];
    // Create the array tracks here - need to use some 'sensible' maximum size!
  }

  public String getArtist()
  {
    return artist;
  }

  public String getTitle()
  {
    return title;
  }

  public void addTrack(Track t)
  {
    // add track t to the array tracks + update the count of tracks
    tracks[numTracks] = t;
  }


  public String totalRunningTime() {                     
     // calculate total running time and return in mins:secs format as a String
     int total = 0;
     

     for (int i = 0; i < tracks.length; i++)
     {
       total = total + tracks[i];
     }
     return tot; // modify this
  }

 
  public String longestTrack() {                     
     // return the title of the longest track
   
     
   
     return "nowt so far"; // modify this
  }

  // Challenge exercise
  public ArrayList<String> findTracks(String s) {
    // Return an ArrayList containing all the titles that containg String s
    return null; //modify this
  }
}
#2. Posted:
UnrealEgg
  • Powerhouse
Status: Offline
Joined: May 30, 201014Year Member
Posts: 438
Reputation Power: 49
Status: Offline
Joined: May 30, 201014Year Member
Posts: 438
Reputation Power: 49
Something I noticed:


  public void addTrack(Track t)
  {
    // add track t to the array tracks + update the count of tracks
    tracks[numTracks] = t;
  }


While this will assign "t" to the index at numTracks (0 by default), you never increment numTracks. So you will just end up only ever replacing tracks[0] every time you add a track.


For longestTrack(), it depends on how the Track class is working. But I presume something like this would maybe what you're looking for?


public String longestTrack() {                     
   // return the title of the longest track
   Track tempTrack = tracks[0];

   for(Track track : tracks){
      if(track.getLength() > tempTrack.getLength()){
         tempTrack = track;
      }
   }

   return tempTrack.getName();
}
#3. Posted:
CoNdEmR
  • TTG Fanatic
Status: Offline
Joined: Apr 29, 200915Year Member
Posts: 4,420
Reputation Power: 1211
Status: Offline
Joined: Apr 29, 200915Year Member
Posts: 4,420
Reputation Power: 1211
Something like this i would assume. Nothing to complicated, just something i wiped up quick.

package ass8;

class CD{
   
   private String title;
   private String artist;
   private static int numTracks = 0;
   
   public CD(String t, String a){
      title = t;
      artist = a;
      numTracks++;
   }
   
   public String toString(){ return title+" : "+artist; }
   
   
}

class Tracks{
   
   private CD tracks[];
   private int size;
   
   public Tracks(int n){
      size = 0;
      tracks = new CD[n];
   }
   
   public void add(String a, String t){
      if(size < tracks.length){
         tracks[size] = new CD(a,t);
         size++;
      }
   }
   
   public String totalTime(){
      int count = 0;
      int j = 0;
      while(j < tracks.length){
         count = count + tracks[j];
      }
   }
}


However i don't see how exactly you're measuring the length of each track when you don't have an actual varaible to check. At the moment from what i can tell you're measuring the length of the artist, title and that's it.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.