#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Manila, Philippines
    Posts
    64
    Rep Power
    5

    Calling a method


    Code:
    import java.util.Scanner;
    import java.util.Arrays;
    
    class iacFunRun2 {
    	
    	public static double[] getRunTime() {
    		Scanner keyIn = new Scanner(System.in);
    		double[] runTimeArr = new double[5];
    		
    		System.out.println("\n***** STUDENT RUNTIMES *****");
    		for(int i = 0; i < runTimeArr.length; i++) {
    			System.out.print("Enter runtime of student " + (i + 1) + ": ");
    			runTimeArr[i] = keyIn.nextDouble();
    		}
    		
    		return (runTimeArr);
    	}
    	
    	public static void main(String[] args) {
    		double[] userInput = getRunTime();
    		
    		Arrays.sort(getRunTime(runTimeArr)); // causes error
    		
    		System.out.println("\n***** SORTED RUNTIME ARRAY *****");
    		for(double runTime : runTimeArr) {
    			System.out.println(runTime);
    		}
    		
    		System.out.println("\n***** TOP 3 RUNTIMES *****");
    		System.out.println(runTimeArr[0]);
    		System.out.println(runTimeArr[1]);
    		System.out.println(runTimeArr[2]);
    	}
    }
    Hi guys,

    I need to sort the return value of the getRunTime method, which is the array runTimeArr. However, I am having trouble calling it, particularly in this line:
    Code:
    Arrays.sort(getRunTime(runTimeArr));
    It is returning a "cannot find symbol error".
    Last edited by kathyrollo; July 5th, 2013 at 02:25 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    1. getRunTime method does not expect any argument. So, it's wrong when you invoke the method by sending it an argument. The following is wrong:
    Code:
    getRunTime(runTimeArr)
    2. The variable "runTimeArr" is defined in the scope of the method "getRunTime". But you're trying to access it in the main method, which is not possible.

    Here is the corrected program:


    Code:
    import java.util.Scanner;
    import java.util.Arrays;
    
    public class TestClass {
    
      public static double[] getRunTime() {
        Scanner keyIn = new Scanner(System.in);
        double[] runTimeArr = new double[5];
    
        System.out.println("\n***** STUDENT RUNTIMES *****");
        for (int i = 0; i < runTimeArr.length; i++) {
          System.out.print("Enter runtime of student " + (i + 1) + ": ");
          runTimeArr[i] = keyIn.nextDouble();
        }
    
        return (runTimeArr);
      }
    
      public static void main(String[] args) {
        double[] userInput = getRunTime();
    
        Arrays.sort(userInput);
        for (double x: userInput) {
          System.out.println(x);
        }
      }
    }
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Manila, Philippines
    Posts
    64
    Rep Power
    5
    [ SEE POST BELOW ]
    Last edited by kathyrollo; July 5th, 2013 at 02:22 AM.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Manila, Philippines
    Posts
    64
    Rep Power
    5
    Exercise:
    - Enter 5 student names who joined the fun run and their corresponding runtimes.
    - Display the top 3 runners (name and time).

    Here is what I have so far:
    Code:
    // With separate methods
    // With student names
    
    import java.util.Scanner;
    import java.util.Arrays;
    
    class iacFunRunNew {
    	static Scanner keyIn = new Scanner(System.in);
    	static String[] studentNameArr = new String[5];
    	static double[] runTimeArr = new double[5];
    		
    	public static String[] getStudentName() {
    		System.out.println("\n***** STUDENT NAMES *****");
    		for(int i = 0; i < studentNameArr.length; i++) {
    			System.out.print("Enter name of student " +
    				(i + 1) + ": ");
    			studentNameArr[i] = keyIn.nextLine();
    		}
    
    		return (studentNameArr);
    	}
    	
    	public static double[] getRunTime() {
    		System.out.println("\n***** STUDENT RUNTIMES *****");
    		for(int i = 0; i < runTimeArr.length; i++) {
    			System.out.print("Enter runtime of student " +
    				(i + 1) + ": ");
    			runTimeArr[i] = keyIn.nextDouble();
    		}
    
    		return (runTimeArr);
    	}
    	
    	public static void sortRunTime() {
    		Arrays.sort(runTimeArr);
    	}
    
    	public static void printOutput() {
    		System.out.println("\n***** TOP 3 RUNTIMES *****");
    		System.out.println("1st Place: " + runTimeArr[0]);
    		System.out.println("2nd Place: " + runTimeArr[1]);
    		System.out.println("3rd Place: " + runTimeArr[2]);
    	}
    
    	public static void main(String[] args) {
    		getStudentName();
    		getRunTime();
    		sortRunTime();
    		printOutput();
    	}
    }
    I can't get the runtimes to display with their corresponding names.
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    I can't get the runtimes to display with their corresponding names.
    can you post the program's current output and add some comments to show what you want the output to look like?
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Manila, Philippines
    Posts
    32
    Rep Power
    2
    Hi NormR,

    I can no longer access my old account, so I had to make a new one.

    Going back, here is the current output of the program:
    Code:
    http://imgur.com/5oinGAE
    I have placed the runners' names in an array.
    I have placed the runtimes in another array.
    I have sorted and displayed the top 3 runtimes.

    But I do not know how to "link" the names to their runtimes. I need to display it like this:
    Code:
    ***** TOP 3 RUNTIMES *****
    1st Place: 30.25 by El Dorado
    2nd Place: 42.51 by Carrie Oakey
    3rd Place: 43.23 by American Idiot
    Help please?
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    If you have parallel arrays holding the data, you will have to write your own sort routine to keep the rows in the arrays in synch. Sorting one array by itself will remove the association of the rows.
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Manila, Philippines
    Posts
    32
    Rep Power
    2
    So I cannot use the API here...

    We have been taught the Bubble Sort, Selection Sort, and Insertion Sort in C, but mostly theoretically since we no longer had the time last term. This would be my first time trying to implement, in a different language at that. :'(

    Which sorting algorithm would you recommend NormR?
  16. #9
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Use whatever sort is the simplest or easiest for you.
    When changing the locations of two elements in one array, be sure to make the same changes to the elements in the other parallel arrays so that the corresponding rows stay parallel.
  18. #10
  19. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Manila, Philippines
    Posts
    32
    Rep Power
    2
    Came up with this:
    Code:
    // With separate methods
    // Sort parallel arrays with bubble sort
    
    import java.util.Scanner;
    
    class iacFunRun2 {
    	static Scanner keyIn = new Scanner(System.in);
    	static String[] studentNameArr = new String[5];
    	static double[] runTimeArr = new double[5];
    		
    	public static String[] getStudentName() {
    		System.out.println("\n***** STUDENT NAMES *****");
    		for(int i = 0; i < studentNameArr.length; i++) {
    			System.out.print("Enter name of student " +
    				(i + 1) + ": ");
    			studentNameArr[i] = keyIn.nextLine();
    		}
    
    		return (studentNameArr);
    	}
    	
    	public static double[] getRunTime() {
    		System.out.println("\n***** STUDENT RUNTIMES *****");
    		for(int i = 0; i < runTimeArr.length; i++) {
    			System.out.print("Enter runtime of student " +
    				(i + 1) + ": ");
    			runTimeArr[i] = keyIn.nextDouble();
    		}
    
    		return (runTimeArr);
    	}
    	
    	public static void bubbleSort() {
    		for(int i = 0; i < runTimeArr.length; i++) {
    			for(int j = i; j < runTimeArr.length; j++) {
    				if(runTimeArr[i] > runTimeArr[j]) {
    					swapRunTime(i, j);
    					swapStudentName(i, j);
    				}
    			}
    		}
    	}
    	
    	public static void swapRunTime(int i, int j) {
    		double temp = runTimeArr[i];
    		runTimeArr[i] = runTimeArr[j];
    		runTimeArr[j] = temp;
    	}
    	
    	public static void swapStudentName(int i, int j) {
    		String temp = studentNameArr[i];
    		studentNameArr[i] = studentNameArr[j];
    		studentNameArr[j] = temp;
    	}
    	
    	public static void printOutput() {
    		System.out.println("\n***** SORTED RUNTIMES *****");
    		for(int i = 0; i < runTimeArr.length; i++) {
    			System.out.println("[" + (i + 1) + "]" +
    				runTimeArr[i] + " mins. by " + studentNameArr[i]);
    		}
    	
    		System.out.println("\n***** TOP 3 RUNTIMES *****");
    		for(int i = 0; i < 3; i++) {
    			System.out.println("#" + (i + 1) + " Place: " +
    				runTimeArr[i] + " mins. by " + studentNameArr[i]);
    		}
    	}
    		
    	public static void main(String[] args) {
    		getStudentName();
    		getRunTime();
    		bubbleSort();
    		printOutput();
    	}
    }
    With the bubble sort code fragment from this site:
    Code:
    http://www.java-forums.org/new-java/28677-sorting-multiple-parallel-arrays.html
    I have some clarifications regarding parameters.
    Most of my methods do not include them, but the swapRunTime and the swapStudentName methods do.
    When do I include the parameters?
  20. #11
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    You could include references to the arrays as parameters to the methods instead of accessing them as class variables.
  22. #12
  23. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Manila, Philippines
    Posts
    32
    Rep Power
    2
    Why would including the parameters be preferable NormR?
  24. #13
  25. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    It makes the method more general. It would be able to be used in other programs if it is not connected to class variables.

    In a small, simple program it won't make much difference. In a larger program it will be useful.

    For example if the parallel arrays in this program were all String then one method could be used to do the swap for all the arrays instead of a swap method for each array.
  26. #14
  27. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Manila, Philippines
    Posts
    32
    Rep Power
    2
    Guess I'd get on with that programming habit then.

    Thanks, I didn't know they were called "parallel arrays". Made searching a lot easier haha.
  28. #15
  29. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Manila, Philippines
    Posts
    32
    Rep Power
    2
    NormR, here is the output of my code now:
    Code:
    http://imgur.com/m3v2wJ6
    I am happy.

    I also changed to the more efficient Insertion Sort:
    Code:
    // With separate methods
    // Sort parallel arrays with insertion sort
    
    import java.util.Scanner;
    import java.text.DecimalFormat;
    
    class funRunInsSort {
    	static Scanner keyIn = new Scanner(System.in);
    	static String[] studentNameArr = new String[5]; // Set value
    	static double[] runTimeArr = new double[5]; // Set value
    		
    	public static String[] getStudentName(String[] studentNameArr) {
    		System.out.println("\n***** STUDENT NAMES *****");
    		for(int i = 0; i < studentNameArr.length; i++) {
    			System.out.print("Enter name of student " +
    				(i + 1) + ": ");
    			studentNameArr[i] = keyIn.nextLine();
    		}
    
    		return (studentNameArr);
    	}
    	
    	public static double[] getRunTime(double[] runTimeArr) {
    		System.out.println("\n***** STUDENT RUNTIMES *****");
    		for(int i = 0; i < runTimeArr.length; i++) {
    			System.out.print("Enter runtime of student " +
    				(i + 1) + ": ");
    			runTimeArr[i] = keyIn.nextDouble();
    		}
    
    		return (runTimeArr);
    	}
    	
    	public static void insertionSort(double[] runTimeArr,
    		String[] studentNameArr) {
    		for(int i = 0; i < runTimeArr.length; i++) {
    			double tempDbl = runTimeArr[i];
    			String tempStr = studentNameArr[i];
    			
    			int j = i;
    			while(j > 0 && runTimeArr[j - 1] > tempDbl) {
    				runTimeArr[j] = runTimeArr[j - 1];
    				studentNameArr[j] = studentNameArr[j - 1];
    				j--;
    			
    			runTimeArr[j] = tempDbl;
    			studentNameArr[j] = tempStr;
    			}
    		}
    	}
    	
    	public static void printOutput(double[] runTimeArr,
    		String[] studentNameArr) {
    		DecimalFormat df = new DecimalFormat("##.00");
    		
    		System.out.println("\n***** SORTED RUNTIMES *****");
    		for(int i = 0; i < runTimeArr.length; i++) {
    			System.out.println("[" + (i + 1) + "] " +
    				df.format(runTimeArr[i]) + " mins. by " +
    				studentNameArr[i]);
    		}
    	
    		System.out.println("\n***** TOP 3 RUNTIMES *****");
    		for(int i = 0; i < 3; i++) {
    			System.out.println("#" + (i + 1) + " Place: " +
    				df.format(runTimeArr[i]) + " mins. by " +
    				studentNameArr[i]);
    		}
    	}
    		
    	public static void main(String[] args) {
    		getStudentName(studentNameArr);
    		getRunTime(runTimeArr);
    		insertionSort(runTimeArr, studentNameArr);
    		printOutput(runTimeArr, studentNameArr);
    	}
    }

IMN logo majestic logo threadwatch logo seochat tools logo