Results 1 to 7 of 7
- 11-01-2010, 01:51 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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:
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: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; } }Last edited by noobForever; 11-01-2010 at 02:18 PM.
-
- 11-01-2010, 03:49 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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?
- 11-01-2010, 06:04 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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; } }
- 11-02-2010, 09:15 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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()).
- 11-02-2010, 11:49 AM #6
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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..).
- 11-02-2010, 12:07 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Result set not returning values
By karthikeyan_raju in forum Advanced JavaReplies: 5Last Post: 04-14-2011, 05:24 AM -
Returning Multiple Value's
By Hollywood_99 in forum New To JavaReplies: 8Last Post: 10-22-2010, 09:09 AM -
java method with multiple return values
By Ms.Ranjan in forum New To JavaReplies: 1Last Post: 06-18-2009, 06:08 PM -
problem on returning values..
By kulangotski in forum Advanced JavaReplies: 11Last Post: 03-07-2009, 02:13 AM -
beginner grade 11-need help with a math code involving returning values from a method
By bobmasta5 in forum New To JavaReplies: 3Last Post: 12-10-2008, 01:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks