The Artima Developer Community
Sponsored Link

Java Answers Forum
need help on java program!

3 replies on 1 page. Most recent reply: Feb 19, 2006 10:50 PM by Matthias Neumair

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
Michelle Loh

Posts: 10
Nickname: modernage
Registered: Feb, 2006

need help on java program! Posted: Feb 19, 2006 8:57 PM
Reply to this message Reply
Advertisement
i need help on how to write this java program:
In many Olympic sports, an athlete is given scores by a panel of judges. Scores can range from 0.0 (awful) to 10.0 (perfect). To ensure fairness, the highest and lowest scores are dropped, and the remaining scores are averaged to produce the athlete's overall score.

Ask the user for the following information, in this order:


The number of judges on the panel.
The score given by each judge, in order. Use the number of judges to decide how many scores to read - do not look for a sentinel value (such as -1 or -999) to stop.
Compute the average of all scores except the highest and lowest. If any score is invalid, report bad input and then ask for that judge's score again. If the number of judges is invalid, do not ask for the number again - simply report bad input and stop.

thanks!


Michelle Loh

Posts: 10
Nickname: modernage
Registered: Feb, 2006

Re: need help on java program! Posted: Feb 19, 2006 9:45 PM
Reply to this message Reply
to be more specific, i am having trouble finding the smallest and largest numbers and then subtracting them in a loop..i dont know how to write code to do that

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: need help on java program! Posted: Feb 19, 2006 10:49 PM
Reply to this message Reply
There are 2 ways o doing it.

1. Sort all results using bullesort or whatever sort algorithm you want. Then only add the scores with index from 1 to n-2

2. Create 2 variables called minScore and maxScore.
Set minScore to a very high value and maxScore to a low value.
Loop through all scores. Compare the actual score with minSum and with maxSum. If you want, use the Math.Min method to set the minScore variable and Math.Max to set the maxScore variable. Also add the current score to the sum.
When you exit the loop, subtract minScore and maxScore from the sum.



I assume that you have all numbers in an double[] array or in a ArrayList<double> Object called scores.
    double minScore = Double.POSITIVE_INFINITY;
    double maxScore = Double.NEGATIVE_INFINITY;
    double sum = 0;
    for (double score : scores) {
        sum+=score;
        if (minScore > score)
            minScore = score;
        if (maxScore < score)
            maxScore = score;
    }
    sum-=(maxScore+minScore);

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: need help on java program! Posted: Feb 19, 2006 10:50 PM
Reply to this message Reply
Bah! Didn't want to post the actual code. Forgot to delete it.

Try to understand it instead of just copying it into your program.

Flat View: This topic has 3 replies on 1 page
Topic: Programming K-map Previous Topic   Next Topic Topic: chat server

Sponsored Links



Google
  Web Artima.com   

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