in CDDriver:
Java Code:
System.
out.
println("\n" +
"Total price of all CDs is " + CD.
getTotalPrice());
CD.getTotalPrice() will return "0" unless you've previously done a CD.setTotalPrice().
More importantly:
Java Code:
System.
out.
println(number +
"\t\t" + myCD.
findTotalPrice());
does not match this from the CD class:
Java Code:
public double findTotalPrice(CDDriver myCD)
Also you can't do this:
Java Code:
System.
out.
println(myCD
[i
].
getCost());
because myCD is not an array here.
try this:
Java Code:
class CD
{ String artist;
String title;
double cost;
static double totalPrice;
static int totalCDs;
double total=
0;
CD
(String a,
String t,
double c
) { artist=a;
title=t;
cost=c;
} public String toString
() { return artist +
"\t " + title +
"\t " + cost;
} public String getArtist
() { return artist;
} public String getTitle
() { return title;
} public double getCost
() { return cost;
} public static double getTotalPrice
() { return totalPrice;
} public int getTotalCDs
() { return totalCDs;
} public static void setTotalPrice
(double newTotalPrice
) { totalPrice = newTotalPrice;
} public static void setTotalPrice
(CD
[] myCD
) { double total =
0;
for (int i=
0; i<myCD.
length; i++
) { total += myCD
[i
].
getCost();
} totalPrice = total;
} public static double getTotalPrice
(CD
[] myCD
) { double total =
0;
for (int i=
0; i<myCD.
length; i++
) { total += myCD
[i
].
getCost();
} return total;
} public static double findTotalPrice
(CD
[] myCD
) { for (int i=
0; i<myCD.
length; i++
) { System.
out.
println(myCD
[i
].
getCost());
} return getTotalPrice
(myCD
);
}}
and
Java Code:
class CDDriver
{ public static void main
(String args
[]) { CD myCD
[] =
new CD
[3];
myCD
[0] =
new CD
("Killers\t",
"Hot Fuss",
6.
49);
myCD
[1] =
new CD
("Kaiser Chiefs",
"Employment",
6.
49);
myCD
[2] =
new CD
("Leonard Cohen",
"The Essential",
7.
99);
System.
out.
println("Artist \t\tTitle\t\tCost");
System.
out.
println("============================== ======");
for(int i=
0; i<myCD.
length; i++
) { System.
out.
println(myCD
[i
].
toString());
} int maxIndex=
0;
double maxCost=myCD
[0].
getCost();
for(int i=
1; i<myCD.
length; i++
) { if (myCD
[i
].
getCost()>maxCost
) { maxCost=myCD
[i
].
getCost();
maxIndex=i;
} } printTotals
(myCD, myCD.
length);
System.
out.
println("\nThe most expensive CD is in location "+maxIndex+
" and is: \n"+ myCD
[maxIndex
].
toString());
System.
out.
println("\n" +
"Total price of all CDs is " + CD.
getTotalPrice());
} public static void printTotals
(CD
[] myCD
/*, int number <- not needed*/) { System.
out.
println("Total Number of CDs \t Total Price");
System.
out.
println(myCD.
length +
"\t\t" + myCD.
findTotalPrice(myCD
));
}}
P.S.: And by the way, I like your taste in music. ;-)