The Artima Developer Community
Sponsored Link

Java Answers Forum
Please please help me with a assignment!!!

3 replies on 1 page. Most recent reply: May 10, 2004 12:58 PM by SRV001

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Fiona

Posts: 2
Nickname: fiona
Registered: May, 2004

Please please help me with a assignment!!! Posted: May 7, 2004 2:39 AM
Reply to this message Reply
Advertisement
I have to write a java program that will dispaly a ist of athletes and their associated times in each event in columns with the headings:
Name
100 metres
200 metres
300 metres

Please could some one help cos i havent a clue what to do!!! :(


SRV001

Posts: 13
Nickname: srv001
Registered: May, 2004

Re: Please please help me with a assignment!!! Posted: May 7, 2004 5:58 AM
Reply to this message Reply
Post your whole assignment here and I will help you.

Fiona

Posts: 2
Nickname: fiona
Registered: May, 2004

Re: Please please help me with a assignment!!! Posted: May 10, 2004 5:11 AM
Reply to this message Reply
Thank you so much!!
I gotta design a program that processes the times achevievd by a set of 25 athletes. It has to use a array of strings to hold the athletes names, and three arrays of double values to hold the times they acheived in the 100metres, 200metres, and 300metres.

(a)Declare a constant to hold number of athlets. Using this constant declare arrays to hold their names and their times for each of the three events.

(b) Invite the user to enter easch athlets name and the time in seconds for each of the three events, and then it will store this information in the appropiate array.

(c) Write a section of java code which will display the list of athlets and their associated times in each event in colums with the headings shown below:
Name 100metres 200metres 300metres

(d) Write a section of java code which finds the fastest time acheived (the smallest time recorded) in the 100 metres, and prints out the name of the person along with the time achevied.

Its a pretty long question, but i would appreciate any help you could give me with ne of it thanks!!

SRV001

Posts: 13
Nickname: srv001
Registered: May, 2004

Re: Please please help me with a assignment!!! Posted: May 10, 2004 12:58 PM
Reply to this message Reply
Fiona,

Here is one solution to your assignment. I didn't do this because I like doing
your homework, but because I think that a lot of people in a first CIS course
don't know where to start when they are faced with writing a program to solve
a problem. I really think that it helps to see a problem that you are familiar
with and some working code.

The instructions that you were given were pretty accurate. You can see how I have
broken the problem down (see comments in the code) and solved each little problem
one at a time until the code was done.

The most important thing is not to try and solve the whole problem all at
once, but to break it down into smaller more manageable problems that are
easily solved. Get one piece of code woking and then continue on from there.

I hope that this helps you see how you would go about this for next time. Read
each comment in the code and then look at the code directly under it until you
understand how each little piece works and then you will understand the overall
problem.

If a problem seems tough then try to do it on paper first. If I asked you to
write three scores for three runners on a piece of paper and then tell me who had
the lowest score in the 100 metre race you would (hopefully) have no trouble
doing this.

I hope that this helps! Please try to post some working code on the website for
the next time.

Good Luck

import java.io.*;
 
public class Times {
    private static final int numberAthletes = 3;
 
    public static void main(String[] args) {
 
      //Declare a constant to hold number of athletes.
        String[] names = new String[numberAthletes];
 
      //Using this constant declare arrays to hold their
      //names and their times for each of the three events.
        int[] time100 = new int[numberAthletes];
        int[] time200 = new int[numberAthletes];
        int[] time300 = new int[numberAthletes];
 
      //Use these variables to keep track of the winner of the 100 metre
        String winner = ""; //nobody yet
        int time = 1000;    //some big number
 
      //System.in reads bytes
      //Use InputStreamReader to change bytes into chars
      //Use BufferedReader to read the whole line
        InputStreamReader reader = new InputStreamReader(System.in);
        BufferedReader console = new BufferedReader(reader);
 
      //Invite the user to enter each athletes name and the time
      //in seconds for each of the three events, and then it will
      //store this information in the appropiate array
        try {
            for (int j = 0; j < numberAthletes; j++) {
                System.out.print("Please enter Athletes Name: ");
                String input = console.readLine();
                names[j] = input;
                for (int k = 0; k < 3; k++) {
                    if (k == 0) {
                        System.out.print("Please enter " + names[j] + "\'s time for 100 metre race: ");
                        input = console.readLine();
                        time100[j] = Integer.parseInt(input);
                      //Store winner of 100 metre race
                        if (time100[j] < time) {
                            winner = names[j];
                            time = time100[j];
                        }
                    } else if (k == 1) {
                        System.out.print("Please enter " + names[j] + "\'s time for 200 metre race: ");
                        input = console.readLine();
                        time200[j] = Integer.parseInt(input);
                    } else if (k == 2) {
                        System.out.print("Please enter " + names[j] + "\'s time for 300 metre race: ");
                        input = console.readLine();
                        time300[j] = Integer.parseInt(input);
                    }
                } //for
                System.out.println();
            } //for
        } catch(Exception e) {
            System.out.println("\n\nError occurred while reading input...terminating program\n\n");
            System.exit(0);
        } //try
 
      //Write a section of java code which will display the list of
      //athletes and their associated times in each event in colums
      //with the headings shown below:
      //Name 100metres 200metres 300metres
        System.out.println();
        System.out.println("Name         100metres       200metres       300metres");
        System.out.println("------       ---------       ---------       ---------");
 
        for (int k = 0; k < numberAthletes; k++) {
            System.out.println(names[k] + "\t\t" + time100[k] + "\t\t" + time200[k] + "\t\t" + time300[k]);
        }
 
        System.out.println();
        System.out.println("The winner of the 100 metre race is "
              + winner
              + " with a time of "
              + time
              + " secs\n");
    } //main()
 
} //Times

Flat View: This topic has 3 replies on 1 page
Topic: Garbage Collection... Previous Topic   Next Topic Topic: reallocation of memory in Java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use