Results 1 to 4 of 4
- 06-08-2010, 01:45 AM #1
Member
- Join Date
- Jun 2010
- Location
- HI
- Posts
- 9
- Rep Power
- 0
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.
Second file:Java 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; } } }
Java 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 #2
Do you mean class when you say file? Files can contain more than one class.how to display the reordered array and the average from toString() method through the second file
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 #3
Member
- Join Date
- Jun 2010
- Location
- HI
- Posts
- 9
- Rep Power
- 0
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 #4
Member
- Join Date
- Jun 2010
- Location
- HI
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
String array from file to ComboBox
By cselic in forum AWT / SwingReplies: 3Last Post: 05-06-2010, 05:29 PM -
Print String from an Array
By adityasirohi in forum New To JavaReplies: 2Last Post: 02-18-2010, 04:10 PM -
I can only print string literals
By leeavital in forum New To JavaReplies: 1Last Post: 12-11-2009, 01:20 AM -
[SOLVED] how to print a class that extends another class
By alpdog14 in forum New To JavaReplies: 3Last Post: 03-19-2009, 05:00 PM -
Print the text file and print preview them
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:04 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks