Hi,

Im fairly new to java and im working my way through a load of exercises and im a little bit stuck on something.

I have two classes, one of which holds the main, which creates 3 objects.

I would like to know how to how to count the total number of classes, and total the value held within each object.

Basically, count how many CDs exist, then sum the value of each CD.


Ive added the variables to the CD class, but im then required to add something into the creation of the object, which i dont want to do.\

I was thinking i may need another class?

Thanks for any help recieved

class CDDriver
{
	public static void main(String args[])
	{

                CD cd1 = new CD("Kaiser "," up the khazi ",(double) 9.99);
		CD cd2 = new CD("Oasis "," morning glory ",(double) 3.99);
		CD cd3 = new CD("Bob Dylan "," Alreet Sunna ",(double) 6.99);

		System.out.println("Artist \t\tTitle\t\tCost");
		System.out.println("====================================");
		System.out.println(cd1.toString());
		System.out.println(cd2.toString());
		System.out.println(cd3.toString());
                
	}
}
public class CD {

     private double totalPrice;
     private int totalCDs;
     private String Artist;
     private String Title;
     private double Cost;



     public CD(String Artist, String Title, double Cost)
    {
    	this.Artist = Artist;
    	this.Title = Title;
    	this.Cost = Cost;

     }

     public CD(double totalPrice, int totalCDs)

     {
             this.totalPrice = totalPrice;
             this.totalCDs = totalCDs;
     }




    
    public String toString()
    {
    	return "CD details: " + Title +" By: " + Artist + "Price:" + Cost;
    }


}

Define "value". Your CD class doesn't have a data member called "value". It has "Cost". Is that "value"?

If you're adding up all the Costs, you can have a static variable in your CD class. You can also have one for the number of CD's. Something like this:

public class CD
{
  public static double totalCost = 0;
  public static int numCDs = 0;
  private double cost;

  public CD (double cdCost)
  {
    cost = cdCost;
    totalCost += cost;
    numCDs++;
  }

  public static void main (String args[])
  {
    CD cd1 = new CD (9.84);
    CD cd2 = new CD (8.76);
    CD cd3 = new CD (5.67);

    System.out.println ("There are " + Integer.toString (CD.numCDs) + " CD objects.");
    System.out.println ("These CD's cost " + Double.toString (CD.totalCost) + " altogether.");
  }
}

Is that about what you're looking for?

Hi,

I meant that the cost of each CD was totalled, so the numbers in the output were added together, and the total number of CD's were counted.

regards

paul

Its often a good idea in a class like this to create a static Collection that holds all the instances that have been created. This allows you to loop through all the instances counting, totalling value, searching for particular artists etc. Something like

private static ArrayList<CD> allCDs = new ArrayList etc
public static  ArrayList<CD> gatAllCDs() {
   return allCDs;
}
// then, in the constructor...
allCDs.add(this);
// then, from anywhere else...
for (CD cd : CD.getAllCDs()) {
  // do whatever with each cd
}

Edited 7 Years Ago by JamesCherrill: n/a

Wont that pose a problem because an array is for 1 type of data and the details hold String and Double?

IF not could you give an example of how to store an array of different types

The ArrayList holds CD objects. You get at the value etc by using the methods that you should have for each cd, eg

int value = 0;
for (CD cd : CD.getAllCDs()) {
    value += cd.getValue();
}

If at all possible could you show where i would add that into my code?

Im literally a two week old java infant.

Thank you so much for your help

Hi,

Ive made a few amendments to the CD class as shown below to include "setters/accessors". the Driver class still provides the same output, which means that they are working accessors.

My next questions is what do i do with the Driver class regarding these changes

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package weektwohomework;

/**
 *
 * @author paulwhite
 */

//sets class with name CD matching the file name
public class CD {

    //set all variables as private with correct data type
     private double totalPrice;
     private int totalCDs;
     private String Artist;
     private String Title;
     private double Cost;

     // Accessor for totalPrice
     double totalPrice()
     {
         return totalPrice;
     }

     void settotalPrice(double asettotalPrice){
        totalPrice = asettotalPrice;
     }


     //Accessor for totalCD's
     int totalCDs()
     {
         return totalCDs;
     }

     void settotalCDs(int asettotalCDs){
         totalCDs = asettotalCDs;
     }

     //Accessor for Artist
     public String Artist()
     {
         return Artist;
     }

     void setArtist(String asetArtist){
         Artist = asetArtist;
     }

     //Accessor for Title
     public String Title()
     {
         return Title;
     }

     //Accessor for Cost
     public double Cost()
     {
         return Cost;
     }
     void setCost(double asetCost){
         Cost = asetCost;
     }


      public CD(String Artist, String Title, double Cost)
    {
    	this.Artist = Artist;
    	this.Title = Title;
    	this.Cost = Cost;
     }
      

    
    public String toString()
    {
    	return "CD details: " + Title +" By: " + Artist + "Price:" + Cost;

    }

}

Edited 7 Years Ago by whiteyoh: adding code

You have this marked as "Solved", so that means that you don't want people to respond to it anymore. You have a few more threads on the forum. What's the current one? You should have one open and the rest marked as solved so everyone knows where to respond (the one not marked "solved"). This keeps two people from posting the same answer on different threads, each not knowing about the other.

my apologies, i will end all of the completed posts.

Ive been re-wording and reading more rather than putting in large posts and being more specific with my questions.

This question has already been answered. Start a new discussion instead.