Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

I got my program with ArrayList to work except for one problem it won't add the same score twice

Thread view: 
judith - 17 Nov 2006 00:57 GMT
I got my program to run except for one problem if you enter the same
score twice it won't add it into the sum of the scores. The divers
final score is supposed to be 67.5 and i'm getting 54.90 here's the
program again and the output. Sorry if i'm being repetive i'm just
wondering how to add the same score in twice. I would appreciate any
help or suggestions thanks Judith I won't post again on this one

output

C:\>java program5JS
Enter the degree of difficulty for the dive (1.2-3.8).
3.0
Enter score for judge 1 (0-10).
5
Enter score for judge 2 (0-10).
7
Enter score for judge 3 (0-10).
9
Enter score for judge 4 (0-10).
7
Enter score for judge 5 (0-10).
6.5
Enter score for judge 6 (0-10).
10
Enter score for judge 7 (0-10).
8
The diver's final score is 54.90

C:\>

Program

//Author:     Judith Spurlock
//Course:       ITSE 2437
//Program No:   5
//Due Date:
//Program Name: program5JS.java

import java.util.ArrayList;
import java.util.Scanner;
import java.text.DecimalFormat;

//fill in code

public class program5JS
{
    public static void main(String[]args)
    {
    ArrayList<Double> scores = new ArrayList<Double>();
    DecimalFormat pattern0dot00 = new DecimalFormat("0.00");
    int posMinScore, posMaxScore;
    double sum = 0;
    double difficulty;
    double finalScore;
    Integer i;
    Scanner keyboard = new Scanner(System.in);

    //Input data values

    System.out.println("Enter the degree of difficulty for the dive
(1.2-3.8).");
    difficulty = keyboard.nextDouble();

    //Input judges scores

    double next = 0;
    for (i = 1; i <= 7; i++)
    {
    System.out.println("Enter score for judge " + i + " (0-10).");
    next = keyboard.nextDouble();
    scores.add(next);
    }

    //Find position of min score

    posMinScore = 0;
    for(i = 1; i < scores.size(); i++)
    {
    if(scores.get(i) < scores.get(posMinScore))
    posMinScore = i;
    }
    scores.remove(posMinScore);

    //Find position of max score

    posMaxScore = 0;
    for(i = 1; i < scores.size(); i++)
    {
    if(scores.get(i) > scores.get(posMaxScore))
    posMaxScore = i;
    }
    scores.remove(posMaxScore);

    //sum scores

    for(i = 1; i < scores.size(); i++)
    {
    sum = sum + scores.get(i);
    }

    //Calculate total score

    finalScore = difficulty * sum * 0.6;
    System.out.println("The diver's final score is "+
pattern0dot00.format(finalScore));
    }
   
}
Taria - 17 Nov 2006 03:51 GMT
>         for(i = 1; i < scores.size(); i++)
>         {
>         sum = sum + scores.get(i);
>         }

The problem here is a common mistake that I do often.  If you've done a
lot of programming with older programming languages, array positions
start with 1, however, in Java, the position starts with 0.

Thus, the current for loop while summing the scores only adds the array
scores from 1 to scores.size() instead of 0 to scores.size().

It really should read:
         for(i=0;i < scores.size();i++)

Taria
Lionel - 17 Nov 2006 04:00 GMT
> If you've done a
> lot of programming with older programming languages, array positions
> start with 1, however, in Java, the position starts with 0.

How old are you talking? C and C++ start at 0! The year C++ was created
was in the early 70s wasn't it? C was obviously before C++. Perhaps
"older programming languages" and "in Java" are just bad choices of
words - either way you solved the OP's problem :).
Knute Johnson - 17 Nov 2006 04:26 GMT
>> If you've done a
>> lot of programming with older programming languages, array positions
[quoted text clipped - 4 lines]
> "older programming languages" and "in Java" are just bad choices of
> words - either way you solved the OP's problem :).

Basic and I think APL did too.  Can't remember for sure, it's been too
long :-).

Signature

Knute Johnson
email s/nospam/knute/

Lionel - 17 Nov 2006 04:35 GMT
>>> If you've done a
>>> lot of programming with older programming languages, array positions
[quoted text clipped - 7 lines]
> Basic and I think APL did too.  Can't remember for sure, it's been too
> long :-).

So is it a function of age or a function of the language :)? I don't
know that much about the history of languages so I wouldn't know . . .
Taria - 17 Nov 2006 09:06 GMT
> > Basic and I think APL did too.  Can't remember for sure, it's been too
> > long :-).So is it a function of age or a function of the language :)? I don't
> know that much about the history of languages so I wouldn't know . . .

Eek!  Don't start that math talk with me!  I'm so full of math theory
and formulas, I'm going to blow up.  Math midterm tomorrow, I really
should be studying and not here.

If I were to guess between 1(a function of age, 2) a function of a
language or 3) a function of neither, intuitively I will have to go
with, I hate to admit this but shh don't tell, it's a function of age.
bahaha

Taria
John W. Kennedy - 21 Nov 2006 01:42 GMT
>>> If you've done a
>>> lot of programming with older programming languages, array positions
[quoted text clipped - 7 lines]
> Basic and I think APL did too.  Can't remember for sure, it's been too
> long :-).

BASIC depends on the dialect, and is sometimes an option. In APL, it's
an option.

Fortran and COBOL start at 1. PL/I starts at 1 if not otherwise specified.

Signature

John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
  -- Charles Williams.  "Taliessin through Logres: Prelude"

Patricia Shanahan - 17 Nov 2006 06:23 GMT
>> If you've done a
>> lot of programming with older programming languages, array positions
[quoted text clipped - 4 lines]
> "older programming languages" and "in Java" are just bad choices of
> words - either way you solved the OP's problem :).

My early programming experience was in Fortran. Although it allows the
programmer to specify a starting index when declaring an array, the
default is 1.

Patricia
John W. Kennedy - 21 Nov 2006 01:43 GMT
>>> If you've done a
>>> lot of programming with older programming languages, array positions
[quoted text clipped - 8 lines]
> programmer to specify a starting index when declaring an array, the
> default is 1.

Allowing the starting index is a modern improvement to the original
language.

Signature

John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
  -- Charles Williams.  "Taliessin through Logres: Prelude"

Taria - 17 Nov 2006 08:57 GMT
> > If you've done a
> > lot of programming with older programming languages, array positions
> > start with 1, however, in Java, the position starts with 0.How old are you talking? C and C++ start at 0! The year C++ was created
> was in the early 70s wasn't it? C was obviously before C++. Perhaps
> "older programming languages" and "in Java" are just bad choices of
> words - either way you solved the OP's problem :).

Lol, yah I don't think C was taught in some universities till about mid
80's :)  Pascal was the hot new language then C took over.  They used a
Unix environment instead of the PC environment.  I remember my PC was a
C64 then. Lol, what memories.

Older languages, like Cobol (gasp, yes folks it still exists), HP
Basic, PL-1, and Fortran which is still commonly used by Mathlab/Octave
folk, today.

Anyway, bugs in your programs are the best way to learn and can be the
most fustrating.

Note:  The Java Debugger is mighty handy, I must admit instead of
putting in manually debug statements.   Kids are so spoilt these days.
(I'm kidding!)  :)

Taria
judith - 17 Nov 2006 05:01 GMT
Tanks eveyone my program works now. I really appreciate it Judith
> I got my program to run except for one problem if you enter the same
> score twice it won't add it into the sum of the scores. The divers
[quoted text clipped - 104 lines]
>    
> }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.