Print Array as String through class file?

Printable View

• 06-08-2010, 01:45 AM
ocarabal
Print Array as String through class file?
Trying to figure out how to display the reordered array and the average from toString() method through the second file. The second file has the main. Another homework help.
Code:

```import java.util.Scanner; average //This is the Average class {         private int[] data = new int[5];  //contains the scores         private double mean = 0;  //arithmetic average of the scores         private int total;         /**         Constructor - allocate memory for the array.  Create a loop to gather all of the scores to be stored         will call the methods to sort and handle the collected data         */           average()         {                 Scanner keyboard = new Scanner(System.in);                 for (int number = 0; number < data.length; number++)                 {                         System.out.println("Please input number "+(number+1)+".");                         data[number] = keyboard.nextInt();                 }                 calculateMean(data);                 selectionSort(data);                 toString();           }           /**           calculateMean method will calculate the mean by adding it to a running total and then dividing by the quantity of the elements           */           public void calculateMean(int[] array)           {                 for (int number = 0; number < array.length; number++)                 {                         total =+ array[number];                 }                 mean = total / array.length;           }           /**           Returns the calculateMean data as String           and the selectionSort as String           */           public void toString(int[] array)           {                 for (int number = 0; number < array.length; number++)                 {                         System.out.println(array[number]);                 }                 System.out.println("Mean: "+mean);           }           /**           selectionSort reorganizes the data from highest to lowest           */           public static void selectionSort(int[] array)           {                 int startScan, index, minIndex, minValue;                 for (startScan = 0; startScan<(array.length-1); startScan++)                 {                         minIndex = startScan;                         minValue = array[startScan];                         for (index = startScan + 1; index < array.length; index++)                         {                                 if (array[index] < minValue)                                 {                                         minValue = array[index];                                         minIndex = index;                                 }                         }                         array[minIndex] = array[startScan];                         array[startScan] = minValue;                 }           } }```
Second file:
Code:

```public class driverAverage {   public static void main (String[ ] args)         {                 System.out.println("This program will help you figure out the average age of a group of drivers.");                 average averageDriver = average();         } }```
• 06-08-2010, 03:15 AM
Norm
Quote:

how to display the reordered array and the average from toString() method through the second file
Do you mean class when you say file? Files can contain more than one class.
To call the toString() method of the average class from the main() method in the driverAverage class, code: averageDriver.toString();
If the toString() method returns a String, you can display it with the println() method like what's in the main() method
System.out.println(averageDriver.toString());
• 06-08-2010, 03:30 AM
ocarabal
That's what I meant, but I already have the methods called in the average class. Why doesn't it display when the constructor calls the methods. So the second class starts a new instance of average and in the constructor it calls the methods to display the average and the reordered list. Why doesn't it show up?
• 06-08-2010, 05:22 AM
ocarabal
Well don't worry about answering this question I figured it out. I wasn't returning the data so that the other methods could retrieve them, but thanks though.