Results 1 to 7 of 7
  1. #1
    noobForever is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default Method Arrays's: Returning multiple values

    Hi all, just embarked on a Java course so very much learning the basics; i'm keen to work this one out myself, however any assistance on how to achieve would be greatly appreciated, as i've hit a bit of a brick wall.

    The concept is simple (i thought..); i have a string of names and two arrays of times (minutes\seconds). I need to pass time values to a method, which runs through each time and finds the quickest time. The method then returns an index number which i can then use to select the fastest time. I've managed that version OK, however i'm having difficulty with duplicate times. So i figured if i returned an array that would allow me to handle duplicate times, that would return all matches. This is what i've come up with:

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package isdcoursework1;
    
    import java.util.Scanner;
    import java.util.Arrays;
    import java.util.Collections;
    
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
    
            [COLOR="SeaGreen"]/*set the names, minutes and seconds data values*/[/COLOR]
    
            String[] names = {"Elena", "Demetris", "Rocky", "Lesley", "Richard", "Gavin", "Max"};
            int[] timesMin = {100, 150, 403, 201, 100, 150, 100};
            int[] timesSec = {29, 28, 15, 55, 27, 29, 27};
    
            int[] quickestTime1 = fastestTime1(timesMin, timesSec);
            for (int i = 0; i < quickestTime1.length; i++){
                System.out.println(names[i]);
            }
        }
    
        public static int[] fastestTime1(int[] minutes, int[] seconds) {
    
            [COLOR="SeaGreen"]/*setup minutes and seconds variable and set to array position 0*/[/COLOR]
    
            int minValue = minutes[0];
            int secValue = seconds[0];
    
            [COLOR="SeaGreen"]/*setup a new index array to hold the minimum and any duplicate values*/[/COLOR]
    
            int[] index = new int[10];
    
            [COLOR="SeaGreen"]/*loop through each minute array value and set the minimum values to the current array index*/[/COLOR]
    
            for (int i = 1; i < minutes.length; i++) {
                System.out.println(i);
                if (minutes[i] < minValue) {
                    System.out.println("minValue set at index: " + i);
                    minValue = minutes[i];
                    secValue = seconds[i];
                    index[i] = minutes[i];
    
            [COLOR="SeaGreen"]/*if we find a duplicate minute value check the associated seconds value of the array, then set the array index to the current array position*/[/COLOR]
    
                } else if (minutes[i] == minValue) {
                    if (seconds[i] < secValue) {
                        System.out.println("minValue set at index: " + i);
                        minValue = minutes[i];
                        secValue = seconds[i];
                        index[i] = minutes[i];
    
            [COLOR="SeaGreen"]/*if we find matching seconds at the array position, set a new index value in the array*/[/COLOR]
    
                    } else if (seconds[i] == secValue) {
                        System.out.println("Found a duplicate time at index: " + i);
                        index[i] = minutes[i];
                    }
                }
            }
            return index;
        }
    
    }
    This is part of a course i'm on, so pointers in the right direction opposed to outright answers would probably serve me better in the long-run :rolleyes:
    Last edited by noobForever; 11-01-2010 at 02:18 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by noobForever View Post
    The method then returns an index number which i can then use to select the fastest time.
    Note that the method is supposed to return a single int, not an array of int. The method internally does not need an index int array but rather an index int local variable that it will return.

  3. #3
    noobForever is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    But what if the method finds duplicate times? In the array example i've posted, positions 4 and 6 of the minutes\seconds array are duplicates. In this case, I would need to return both index number 4 and 6 so i can return both names for joint 1st place?

  4. #4
    noobForever is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    OK i'm nearly there; by re-setting the index array number to 0 if a new minimum is found OR adding a new value to the array if a duplicate is found, i can then check for != 0 and print the correct results, including duplicate times. However, if the quickest time is at position 0, i'm buggered.

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package isdcoursework1;
    
    /**
     *
     */
    import java.util.Scanner;
    import java.util.Arrays;
    import java.util.Collections;
    
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
    
            /*Define variables to hold data (single string and two arrays) */
            String[] names = {"A", "B", "C", "D", "E", "F"};
            int[] timesMin = {10, 40, 30, 40, 10, 10};
            int[] timesSec = {3, 4, 4, 3, 1, 1};
    
            int[] quickestTime1 = fastestTime1(timesMin, timesSec);
            for (int i = 0; i < quickestTime1.length; i++) {
                if (quickestTime1[i] != 0) {
                    System.out.println(quickestTime1[i]);
                    System.out.println(names[quickestTime1[i]]);
                }
            }
        }
    
         public static int[] fastestTime1(int[] minutes, int[] seconds) {
            int minValue = minutes[0];
            int secValue = seconds[0];
            int[] index = new int[6];
    
            for (int i = 1; i < minutes.length; i++) {
                System.out.println(i);
                if (minutes[i] < minValue) {
                    minValue = minutes[i];
                    secValue = seconds[i];
                    index[i - 1] = 0;
                    index[i] = i;
                } else if (minutes[i] > minValue) {
                    index[i - 1] = 0;
                } else if (minutes[i] == minValue) {
                    if (seconds[i] < secValue) {
                        minValue = minutes[i];
                        secValue = seconds[i];
                        index[i - 1] = 0;
                        index[i] = i;
                    } else if (seconds[i] > secValue) {
                        index[i - 1] = 0;
                    } else if (seconds[i] == secValue) {
                        index[i] = i;
                    }
                }
            }
            return index;
        }
    }

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    What happens when the quickestTime is at postion 0?

    By the way, have you done Classes yet? Objects?
    Just thinking that your name and times should be in their own class, but if you haven't done that stuff yet then ignore me.

    ETA: Ah, why are you starting i at 1 in the fastestTime1() method?

    ETA2: And your methods should be actions (so findFastestTimes(), rather than fastestTime1()).

  6. #6
    noobForever is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Cool thanks Tolls; made the ETA1/2 modifications :-) no we haven't covered classes and objects yet although i have read up on them. Trouble is, the assignment specifically asks to only use methods\arrays to return the results, but using only those methods, i can't see a way of returning two duplicate values if the method finds them in the array (apart from the messy version i've put together so far..).

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    No, that's fine.
    I suspected it was a basic coding/algorithm thing, so I can understand not being allowed to use classes.

Similar Threads

  1. Result set not returning values
    By karthikeyan_raju in forum Advanced Java
    Replies: 5
    Last Post: 04-14-2011, 05:24 AM
  2. Returning Multiple Value's
    By Hollywood_99 in forum New To Java
    Replies: 8
    Last Post: 10-22-2010, 09:09 AM
  3. java method with multiple return values
    By Ms.Ranjan in forum New To Java
    Replies: 1
    Last Post: 06-18-2009, 06:08 PM
  4. problem on returning values..
    By kulangotski in forum Advanced Java
    Replies: 11
    Last Post: 03-07-2009, 02:13 AM
  5. Replies: 3
    Last Post: 12-10-2008, 01:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •