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

J

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
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));
}

}
 
T

Taria

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
 
L

Lionel

Taria said:
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 :).
 
K

Knute Johnson

Lionel said:
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 :).

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

Lionel

Knute said:
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 . . .
 
P

Patricia Shanahan

Lionel said:
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 :).

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
 
T

Taria

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
 
T

Taria

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
 
J

John W. Kennedy

Knute said:
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.
 
J

John W. Kennedy

Patricia said:
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.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,979
Messages
2,570,185
Members
46,723
Latest member
TwilaTarde

Latest Threads

Top