2 Replies - 1903 Views - Last Post: 06 February 2012 - 04:25 PM Rate Topic: -----

#1 narutoninjakid   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 15-November 11

Java Tunes Class Help Beginner Please

Posted 06 February 2012 - 03:44 PM

hey guys i really need some help here are the classes involved

CD class
 
//********************************************************************
//  CD.java       Author: Lewis/Loftus/Cocking
//
//  Represents a compact disc.
//********************************************************************

import java.text.NumberFormat;

public class CD
{
   private String title, artist;
   private double cost;
   private int tracks;

   //-----------------------------------------------------------------
   //  Creates a new CD with the specified information.
   //-----------------------------------------------------------------
   public CD (String name, String singer, double price, int numTracks)
   {
      title = name;
      artist = singer;
      cost = price;
      tracks = numTracks;
   }

   //-----------------------------------------------------------------
   //  Returns a description of this CD.
   //-----------------------------------------------------------------
   public String toString()
   {
      NumberFormat fmt = NumberFormat.getCurrencyInstance();

      String description;

      description = fmt.format(cost) + "\t" + tracks + "\t";
      description += title + "\t" + artist;

      return description;
   }
}



here is the cd collection class
 
//********************************************************************
//  CDCollection.java       Author: Lewis/Loftus/Cocking
//
//  Represents a collection of compact discs.
//********************************************************************

import java.text.NumberFormat;

public class CDCollection
{
   private CD[] collection;
   private int count;
   private double totalCost;

   //-----------------------------------------------------------------
   //  Creates an initially empty collection.
   //-----------------------------------------------------------------
   public CDCollection ()
   {
      collection = new CD[100];
      count = 0;
      totalCost = 0.0;
   }

   //-----------------------------------------------------------------
   //  Adds a CD to the collection, increasing the size of the
   //  collection if necessary.
   //-----------------------------------------------------------------
   public void addCD (String title, String artist, double cost,
                      int tracks)
   {
      if (count == collection.length)
         increaseSize();

      collection[count] = new CD (title, artist, cost, tracks);
      totalCost += cost;
      count++;
   }

   //-----------------------------------------------------------------
   //  Returns a report describing the CD collection.
   //-----------------------------------------------------------------
   public String toString()
   {
      NumberFormat fmt = NumberFormat.getCurrencyInstance();
      String report = "******************************************\n";
      report += "My CD Collection\n\n";

      report += "Number of CDs: " + count + "\n";
      report += "Total cost: " + fmt.format(totalCost) + "\n";
      report += "Average cost: " + fmt.format(totalCost/count);

      report += "\n\nCD List:\n\n";


      for (int cd = 0; cd < count; cd++)
         report += collection[cd].toString() + "\n";

      return report;
   }

   //-----------------------------------------------------------------
   //  Doubles the size of the collection by creating a larger array
   //  and copying the existing collection into it.
   //-----------------------------------------------------------------
   private void increaseSize ()
   {
      CD[] temp = new CD[collection.length * 2];

      for (int cd = 0; cd < collection.length; cd++)
         temp[cd] = collection[cd];

      collection = temp;
   }
}



here is the tune class given if it helps you guys in any way, not helping me.
 
//********************************************************************
//  Tunes.java       Author: Lewis/Loftus/Cocking
//
//  Driver for demonstrating the use of an array of objects.
//********************************************************************

public class Tunes
{
   //-----------------------------------------------------------------
   //  Creates a CDCollection object and adds some CDs to it. Prints
   //  reports on the status of the collection.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      CDCollection music = new CDCollection ();

      music.addCD ("By the Way", "Red Hot Chili Peppers", 14.95, 10);
      music.addCD ("Come On Over", "Shania Twain", 14.95, 16);
      music.addCD ("Soundtrack", "The Producers", 17.95, 33);
      music.addCD ("Play", "Jennifer Lopez", 13.90, 11);

      System.out.println (music);

      music.addCD ("Double Live", "Garth Brooks", 19.99, 26);
      music.addCD ("Greatest Hits", "Stone Temple Pilots", 15.95, 13);

      System.out.println (music);
   }
}



here is the class im working on
 
// BetterTunes.Java- improved version of tunes program
// U.Ogbe
// 2/6/2012

import java.util.Scanner;

public class BetterTunes2
{
    public static void main (String [] args)
    {
      String title, artist;
      double cost;
      int tracks;



     Scanner scan = new Scanner (System.in);
     Scanner scan2 = new Scanner (System.in);
     Scanner scan3 = new Scanner (System.in);

    System.out.print("what is the title of your cd: ");
    title = scan.nextLine();

    System.out.print("who is the artist of your cd: ");
    artist = scan.nextLine();

    System.out.print("what is the cost of your cd: ");
    cost = scan2.nextDouble();

    System.out.print("how many tracks are on your cd: ");
    tracks = scan3.nextInt();



    }
}



i need to make my tunes class able to
1. user adds cd's
2. CD class comparable " implement comparable"
3. add search by artist to CDCollection
4. sort
5. binary search

Help on how to do any of these appreciated !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Is This A Good Question/Topic? 0
  • +

Replies To: Java Tunes Class Help Beginner Please

#2 narutoninjakid   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 15-November 11

Re: Java Tunes Class Help Beginner Please

Posted 06 February 2012 - 04:05 PM

okay i figured out how to addcd i need help with the others now

heres new product
 
import java.util.Scanner;

public class BetterTunes2
{
    public static void main (String [] args)
    {
      CDCollection music = new CDCollection();

		Scanner scan = new Scanner(System.in);

		String add = "yes";
		String name, singer;
		double price;
		int numTracks;

		while (add.equalsIgnoreCase("yes"))
		{
		System.out.print("Enter the title of the CD: ");
		name = scan.next();
		System.out.print("Enter the artist: ");
		singer = scan.next();
		System.out.print("Enter the cost: ");
		price = scan.nextDouble();
		System.out.print("Enter the number of tracks in the CD: ");
		numTracks = scan.nextInt();

		music.addCD(name, singer, price, numTracks);

		System.out.print("Do you want to add another CD? ");
		add = scan.nextLine();


		System.out.print(music);

		}
    }
}


Was This Post Helpful? 0
  • +
  • -

#3 narutoninjakid   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 15-November 11

Re: Java Tunes Class Help Beginner Please

Posted 06 February 2012 - 04:25 PM

need help with compareTo
 
public int compareTo (Object obj)
		{
			int otherValue = cd
		}

		}



want to make other value price of cd being compared , how would i do that alo can i say this.price for price of cd in collection?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1