Question:
Java class not compiling. not sure how to build my constructor. help!?
2009-12-09 18:55:18 UTC
import java.text.NumberFormat;

public class Statistics
{
private Statistic[] data;
private int count;
private double totalValue;
private int currentSize;
//-----------------------------------------------------------------
// Creates an initially empty data.
//-----------------------------------------------------------------
public Statistics()
{
currentSize = 50;
data = new Statistics[currentSize];
count = 0;
totalValue = 0.0;
}
//-----------------------------------------------------------------
// Adds a Statistics to the data, increasing the size of the
// data if necessary.
//-----------------------------------------------------------------
public void addStatistic (String title, String artist, double value,
int tracks)
{
if (count == currentSize)
increaseSize();

data[count] = new Statistic (title, artist, value, tracks);
totalValue += value;
count++;
}

//-----------------------------------------------------------------
// Adds a Statistics to the collection, increasing the size of the
// collection if necessary.
//-----------------------------------------------------------------
public void addStatistic (Statistic acd)
{
if (count == currentSize)
increaseSize();

data[count] = acd;
totalValue +=acd.getValue();
count++;
}
//-----------------------------------------------------------------
// find a Statistic in the data, if found, returns an index to the Statistic,
// if not found in the data return -1
//-----------------------------------------------------------------
public int find (Statistic acd)
{
int index;
boolean found = false;
for (index = 0; index {
if( data[index].equals(acd) )
found = true;
}
if(found)
return (index-1);
else
return -1;
}

//_________________________________
// retrieve a Statistic from the data
// return a Statistic
//____________________________________

public Statistic retrieve(int index)
{
return data[index];
}

public Statistic retrieve(Statistic acd)
{
Statistic someStatistic = null;
int index = find(acd);

if (index >-1)
someStatistic = data[index];

return someStatistic;

}
// delete
//precondition: index has to be grater then -1
public String delete ( int index)
{
if ( index >-1)
{
for ( int i = index; i data[i] = data[i+1];

count --;

return "removed";
}
else
return "index is negative - did not remove";


}// end delete

public String delete (Statistic acd)
{
int index = find(acd);
if ( index >-1)
{
for ( int i = index; i data[i] = data[i+1];

count --;

return "removed";
}
else
return "index is negative - did not remove";

}// end delete

// insert a Statistic after index
// post condition index has to be greater then -1, and acd is not null

public String insert (Statistic acd, int index)
{
String isdone;

if( index >-1)
{
// shift the data
for ( int i = count - 1 ; i> index ; i--)
data[i+1] = data[i];

data[index+1] =acd;

isdone = " a Statistic is inserted";
}
else
isdone = " a Statistic is not inserted";
return isdone;
}//end insert


//-----------------------------------------------------------------
// Returns a report describing the Statistics data.
//-----------------------------------------------------------------
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();

String report = "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n";
report += "My Statistics Collection\n\n";

report += "Number of Statistic's: " + count + "\n";
report += "Total value: " + fmt.format(totalValue) + "\n";
report += "Average cost: " + fmt.format(totalValue/count);

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

for (int Statistic = 0; statistic < count; statistic++)
report += data[statistic].toString() + "\n";

return report;
}

//-----------------------------------------------------------------
// Doubles the size of the data by creating a larger array
// and copying into it the existing collection.
//-----------------------------------------------------------------
private void increaseSize ()
{
currentSize *= 2;
Statistic[] temp = new Statistic[currentSize];
for (int statistic = 0; statistic < data.length; statistic++)
temp[statistic] = data[statistic];

data = temp;
}
}

errors look like this(only a few examples).......................................

Statistics.java.java:3: class Statistics is public, should be declared in
Three answers:
husoski
2009-12-09 19:12:56 UTC
Follow the advice. Rename your source file to "Statistics.java". Case matters, except for the .java suffix, so you can't call it "statistics.java" or "STATISTICS.JAVA". Also, I mistyped "Statistics" twice as "Staticstics" during the composition of this answer. Make sure your spelling is good on the file name.



Added on edit: Your data[] array is an array of Statistic objects, but you are initializing it with an array of Statistics objects instead. Even if this wasn't a type mismatch, it would quickly gobble the world. Each Statistics object created would in turn create 50 more Statistics objects, and each of them would...



Ad nauseum.
Amin
2014-06-28 02:51:53 UTC
Actually, I'm a java beginner, I found some tutorials of a guy called Bucky on you-tube, I start following those tutorials, but for last days I got trouble with (Java Programming Tutorial - 41 - Building Objects for Constructors) which is available at https://www.youtube.com/watch?v=MK2SMJZbUmU (about 8 minutes)

according to the code in this tutorial, one should get the following result:

00:00:00

05:00:00

05:13:00

05:13:43

But I just got:

00:00:00

00:00:00

00:00:00

43:00:00

I don't know what was my mistake?

is this series suitable for me as a beginner? any suggestions.

sorry for being long, if someone have a time can help me.
cb
2009-12-09 19:12:50 UTC
It appears that you have an extra extension on your source file name. Try renaming "Statistics.java.java" to "Statistics.java".


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...