presence presence - 1 month ago 4
Java Question

Printing the lowest and second lowest values in an array using methods (Java)

I am new to Java and have been given an assignment that asks:

Description

A group of friends decide to run the Boston Marathon. Their names and times (in minutes) are below:

Name       Time(minutes)

Elena       341
Thomas      273
Hamilton    278
Suzie       329
Phil       445
Matt       402
Alex       388
Emma       275
John       243
James       334
Jane       412
Emily       393
Daniel     299
Neda       343
Aaron       317
Kate       265


Find the fastest runner. Print the name and his/her time (in minutes).

Find the second fastest runner. Print the name and his/her time (in minutes).

Instructions

Write a method that takes as input an array of integers and returns the index corresponding to the
person with the lowest time. Run this method on the array of times. Print out the name and time
corresponding to the returned index.
Write a second method to find the second‐best runner. The second method should use the first method
to determine the best runner, and then loop through all values to find the second‐best (second lowest)
time.
Here is a program skeleton to get started:

class Marathon {
public static void main (String[] arguments){
String[] names ={
"Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", "Alex",
"Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda",
"Aaron", "Kate"
};
int[] times ={
341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299,
343, 317, 265
};
int i1 = fastestIndex(times);
int i2 = secondFastestIndex(times);
//
// your output based on the index i1 and i2
//
}
// your two Java methods:
// fastestIndex() and secondFastestIndex()
}


I wrote a program that prints the first and second place times and names but now I am starting to think I didn't follow the instructions correctly. Could anyone help me figure out what I should do differently?

Here is my amateur code:

public class Lab
{
public static void main(String[] args)
{
String[] names = {
"Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt",
"Alex", "Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda", "Aaron", "Kate"
};

int[] times = {
341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299, 343, 317, 265
};

int firstplace = times[0];
String firstname = names[0];

int secondplace = times[0];
String secondname = names[0];

for (int counter = 0; counter < times.length; counter++)
{
if (times[counter] < firstplace) {
firstplace = times[counter];
firstname = names[counter]; }
}

for (int counter = 0; counter < times.length; counter++)
{
if (times[counter] > firstplace)
if (times[counter] < secondplace) {
secondplace = times[counter];
secondname = names[counter]; }
}
System.out.printf("The fastest runner is: %s (%d Minutes)%n", firstname, firstplace);
System.out.printf("The second fastest runner is: %s (%d Minutes)%n", secondname, secondplace);
} // end main
} // end class Lab


This is what it prints:

The fastest runner is: John (243 Minutes)
The second fastest runner is: Kate (265 Minutes)

Answer

Alright so the complete answer you want is this:

Your teacher gives you specific pseudo-code on how to solve this problem and you should follow it.

Write a method that takes as input an array of integers and returns the index corresponding to the person with the lowest time.

this is it: public static int fastestIndex(int[] times){...} it is static because it is not an instance method (meaning you don't have to create an instance of an object to call it)

and the second one also has the same signature

public static int secondFastestIndex(int[] times)

and inside it calls the first method to help find the second fastest index

then you print both.

Here is the full code

public class Lab
{
    public static void main(String[] args)
    {
            String[] names = {
                "Elena", "Thomas", "Hamilton", "Suzie", "Phil", "Matt", 
                "Alex", "Emma", "John", "James", "Jane", "Emily", "Daniel", "Neda", "Aaron", "Kate"
            };

            int[] times = {
                341, 273, 278, 329, 445, 402, 388, 275, 243, 334, 412, 393, 299, 343, 317, 265
            };



            int win = fastestIndex(times);
            int second = secondFastestIndex(times);
            System.out.printf("The fastest runner is: %s (%d Minutes)%n", names[win], times[win]);
            System.out.printf("The second fastest runner is: %s (%d Minutes)%n", names[second], times[second]);
    } // end main



    public static int fastestIndex(int[] times) 
    {

        int fastestIndex = 0;

        for (int counter = 0; counter < times.length; counter++)
        {

            if (times[counter] < times[fastestIndex]) 
            {
                fastestIndex = counter;
            }
        }

        return fastestIndex;

    }

    public static int secondFastestIndex(int[] times)
    {
        int fastestIdx = fastestIndex(times);
        int secondFastestIdx = 0;

        for (int counter = 0; counter < times.length; counter++)
        {
            //you search for the fastest, but you ignore it if it is
            // equal to the fastest index (so you are really finding the 2nd)
            if (times[counter] < times[secondFastestIdx] && counter != fastestIdx) 
            {
                secondFastestIdx = counter;
            }
        }

        return secondFastestIdx;
    }

} // end class Lab

hope this helps

Comments