Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Homework question having to do with arrays...

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Homework question having to do with arrays...

    Hey guys, my name's Theo, or Thedore. I am currently doing the MIT open courseware java class, an am having a bit of trouble understanding how exactly to do this problem dealing with arrays. Any help would be appreciated!

    A group of MIT 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).

    Optional: Find the second fastest runner. Print the name and his/her time (in minutes).
    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
    };
    for(int i = 0; i < names.length; i++){ System.out.println(names[i] + ": " + times[i]);
    }
    }
    }
    Submit your file Marathon.java via Stellar.
    Good luck!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Homework question having to do with arrays...

    Where exactly are you stuck? You've posted the question and some code, but not an actual technical question.

    PS: When posting code, make sure you use the highlight tags to preserve formatting.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    TheoAnderson (June 3rd, 2013)

  4. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Homework question having to do with arrays...

    Kevin,

    I am pretty much brand new to Java and don't have hardly any experience.

    What do you mean highlight tags?

    My question is simply one of how do I go about finding out who has the lowest time and then print it out? Should I make another method to be called by the main method which gets this information? I am unsure how to figure out how I would go about finding the fastest time.

    Thanks,

    -Theo

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Homework question having to do with arrays...

    The highlight tags help preserve formatting. Check out another post that uses them or the link in my signature on using code tags for more information.

    When approaching this kind of problem, take a step back from the computer and think about how you'd do this with just a piece of paper and a pencil. Say you have a really long list of numbers. How would you figure out which one is the largest? Pretend you have a friend who is really dumb, and write out instructions (not code) that he could follow to find the largest number in a big list. Remember how dumb he is, so make sure you break the instructions down into the smallest steps you can.

    When you have those instructions written out, you'll have an algorithm that you can start thinking about coding.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    26
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Homework question having to do with arrays...

    Use Math.max(int x, int y) in a loop to figure out which is the ultimate highest of them all. By the way, if you don't have hardly any experience, this should be easy for you.

    UPDATE: Use Math.min(int x, int y) since the lowest number is the fastest.

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Homework question having to do with arrays...

    Please note that beansnbacon's solution is only one approach of many that you can take. I strongly suggest you try coming up with whatever solution makes the most sense to you before implementing somebody else's algorithm.

    Coding a given algorithm is easy. Coming up with an algorithm in the first place is the hard part, and it's a skill that's almost impossible to teach other than to have you think through problems on your own.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Predicting the Outcome without a compiler **Not a Homework Question**
    By sternfox in forum Java Theory & Questions
    Replies: 1
    Last Post: February 25th, 2013, 04:53 PM
  2. [SOLVED] Homework Question
    By EPC in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 24th, 2013, 01:33 PM
  3. Question about homework problem.
    By Rain_Maker in forum Java Theory & Questions
    Replies: 13
    Last Post: February 7th, 2013, 08:11 PM
  4. Question regarding 2d arrays
    By Demetrius82 in forum Java Theory & Questions
    Replies: 8
    Last Post: May 30th, 2011, 04:06 PM
  5. help with homework - simple arrays
    By sensoryctascale in forum Java Theory & Questions
    Replies: 3
    Last Post: April 5th, 2011, 01:39 AM

Tags for this Thread