CDROM-Guide forums  

PDA

View Full Version : Java Code Problem


   
grae
Jan 13, 2003, 07:04 PM
I am using JCreator and need to import an individual class file that has been made. I have all of the other code done, but when i run it, it all works, except for the bits relating to this other class file. Ive only been doing java for about a month, but this is what I have. It is for cataloguing a CD Collection. I need to import a CD.class file so the CD and toString() functions work. cheers for any help

CDMain
---------

//CD Main class
import avi.*;
public class CDMain
{
public static void main(String args[])
{
Window screen = new Window("CDMain.java");
screen.showWindow();

CDCollection cdcol1 = new CDCollection(0);

int index;
int arraySize = cdcol1.totalNumberOfCollection();
String cdDetails;

cdcol1.addCD("Warning","Green Day",11.99,16);
cdcol1.addCD("OK Computer","Radiohead",12.99,11);

screen.write("My CD Collection");
screen.write("\n\nNumber of CDs: "+cdcol1.totalNumberOfCollection());
screen.write("\nTotal Cost: "+cdcol1.totalCostOfCollection());
screen.write("\nAverage Cost: "+cdcol1.totalCostOfCollection()/cdcol1.totalNumberOfCollection());
screen.write("\n\nCD List: \n");

for (index = 0; index != arraySize; index++)
{
cdDetails = cdcol1.getCDDetails(index);
screen.write("\n"+cdDetails);
}

screen.write("\n\nDetails of Highest Cost CD: "+cdcol1.getHighestCost());
screen.write("\n\nDetails of Lowest Cost CD: "+cdcol1.getLowestCost());
}
}





CDCollection
---------------

//CD Collection class to hold details of a CD collection
import avi.*;
public class CDCollection
{
private int arraySize;
private int index;
private double indexHighestCost;
private double indexLowestCost;
private double highestCost;
private double lowestCost;
private double totalCost;
private String[] titleArray;
private String[] artistArray;
private double[] costArray;
private int[] tracksArray;

Window screen = new Window("CDMain.java");

public CDCollection(int aS)
{
arraySize = aS;
String[] titleArray = new String[arraySize];
String[] artistArray = new String[arraySize];
double[] costArray = new double[arraySize];
int[] tracksArray = new int[arraySize];
}

public void addCD(String ti,String ar,double co,int tr)
{
screen.showWindow();
arraySize = arraySize + 1;
CD arraySize = new CD(ti,ar,co,tr);
}

public void calcHighestCost()
{
highestCost = costArray[0];
for (index = 1; index != arraySize; index++)
if(costArray[index] > highestCost) highestCost = costArray[index];
indexHighestCost = index;
}

public void calcLowestCost()
{
lowestCost = costArray[0];
for (index = 1; index != arraySize; index++)
if(costArray[index] < lowestCost) lowestCost = costArray[index];
indexLowestCost = index;
}

public void calcTotalCost()
{
totalCost = 0;
for (index = 0; index != arraySize; index++)
totalCost = totalCost + costArray[index];
}

public String getCDDetails(int id)
{
String example = "id";
return example.toString();
}

public double getHighestCost()
{
return highestCost;
}

public double getLowestCost()
{
return lowestCost;
}

public double totalCostOfCollection()
{
return totalCost;
}

public int totalNumberOfCollection()
{
return arraySize;
}

public double getHighCostIndex()
{
return indexHighestCost;
}

public double getLowCostIndex()
{
return indexLowestCost;
}
}