1. Member Join Date
Feb 2009
Posts
3
Rep Power
0

## What's up guys

Hi,

I am totally new to programming and my local university has us starting with java right off the bat, for my degree. I have heard a lot of programmers say that this isn't the greatest language to be learning in. Luckily, I found these forums and I see that you help each other.

I am caught with this. I have to take 5 test scores and average them. Now, I have to be able to take an infinite amount of scores and average them.

Do I need to start off by reassigning my variables? right now they're; double test1, test2, test3, test4, test5
double average

and then from that put it through a counter control loop?
Last edited by zuriick; 02-05-2009 at 11:21 PM.  Reply With Quote

2. Member Join Date
Feb 2009
Location
Raleigh, NC
Posts
5
Rep Power
0

## This type of problem is language agnostic. The solution you are looking for involves taking a set of data with an unknown upper bound.

So, when you started with a known number of tests you took the path of least resistance and what seemed most obvious: you created a variable for each test score. However, if you don't know how many tests you are going to have (or even if you do, but you suspect that number might change in the future), it is best to use an array.

If you were writing this in function form, you could use:

Java Code:
```    public double averageTestScore(double[] testScores) {
/** Add all the test scores */
double sumOfScores = 0;
for (int x = testScores.length; x < testScores.length; x++) {
sumOfScores += testScores[x];
}

/** Compute the average */
double averageScore = sumOfScores / testScores.length;
return averageScore;
}```
Then, in order to use that function, you might do something like:

Java Code:
```double[] testScores = new double;
testScores = 95.4;
testScores = 86.2;
testScores = 99.4;
testScores = 100;
testScores = 75.5;

double averageScore = averageTestScore(testScores);```
The concept your teacher is trying to convey is that you normally are not going to know how many items you will be working with, so it is best to create functions that can take any number of elements and perform its operation on them. That's why the averaging function takes an array of unknown length in this example.

Like I said, this is language agnostic. It's a way of thinking that translates to any programming language.

(Note: I am not new to programming, but I am new to Java. So, there could potentially be some syntactical errors with the code posted above.)  Reply With Quote

3. ## Example

Do you mean you can add the numbers to an array in the program and it will still calculate the average? or are the numbers the command line arguments? more details pls.
if test# variables are the numbers, you should keep all the numbers in an array (int[]), not in seperate variables.
To get the average of the array, this would work:
(you can replace the int's with doubles if you want decimal #'s)
Java Code:
```public class AverageCalculator {
private static int[] nums = { 1, 5, 44, 34, 45, 6, 11 }; // you can change this
public static void getAverage() {
int sum, average;
sum = 0;
String numbers = "";
for(int i = 0; i < nums.length; i++) { // This loop gets the sum of nums
sum += nums[i];          // and also puts nums in a string
String += Integer.toString(nums[i]) + ", ";
}
average = sum / nums.length; // calculate average
System.out.println("The Average of:\n" + numbers); //display results
System.out.println("is " + average + "!");
}
public static void main(String[] args) {
getAverage();
}
}```  Reply With Quote

4. Member Join Date
Feb 2009
Location
Raleigh, NC
Posts
5
Rep Power
0

## What I was presenting was a basic answer to the problem without full implementation details. One of the major tenants of programming is to separate logic out and make it implementation agnostic if at all possible. That's the idea behind the average function. In reality, the function should simply be named "average" and it could be overloaded to support many data types (int, floats, etc.). However, the original poster required doubles.

You could implement this as a command line script in several ways. One simple way might be to first ask the user how many tests they would like to average. Then, once you have that number you know how big to make your array. After creating your array, you can then create a loop which asks the user for each score one at a time, filling the array as you go. Once the array is filled, you can pass it to the average function and display the returned result.

That's code you'll have to figure out on your own, however. That's half the fun of learning. :)  Reply With Quote

5. Senior Member Join Date
Jun 2008
Posts
2,568
Rep Power
14

##  Originally Posted by zuriick What's up
Two letters put together to form a preposition indicating movement in a vertical direction away from the ground.  Reply With Quote

6. ## I have heard a lot of programmers say that this isn't the greatest language to be learning in
Interesting statement... what would be the greatest language to be learning? I find Java rather fun and fresh to program, but unfortunately it wasn't my first programming language that I learned. It probably doesn't make any difference what your first programming language is, so long as you learn very well (the why's).... all prog languages are bascially the same in structure.
Nough said on that...
I have to take 5 test scores and average them. Now, I have to be able to take an infinite amount of scores and average them.
I'm not sure if you have do the 5 or the infinite amount of scores. I'll assume that you have already done the 5 and now have to do the infinite amount. My first question would be:
• How are you planning on getting the inifinite amount of scores? User input or manually (hardcoded in the program like y0y and MK12 show)?

If this manually, than MK12 example is OK (using an array).
If it's through user input, then you would probably would want to use an arraylist instead of an array.

Luck,
CJSL  Reply With Quote

7. Member Join Date
Feb 2009
Posts
3
Rep Power
0

## ^^ I have already done the 5 and now i have to find the average of any given number of inputs. i am not hard coding them into the the program it has to be entered in two different ways: one is through inFile.nextDouble();, and the other is straight from system.  Reply With Quote

8. Member Join Date
Feb 2009
Posts
3
Rep Power
0

## here is what i have so far

import java.io.*;
import java.util.*;

{
public static void main(String[] args) throws IOException,
FileNotFoundException
{

//declare and initialize the variables
double test1, test2, test3, test4, test5;
double average;
String firstName;
String lastName;

Scanner inFile =

PrintWriter outFile = new
PrintWriter("testavg.out");

firstName = inFile.next();
lastName = inFile.next();

outFile.println("Student Name: "
+ firstName + " " + lastName);

//Step 6 - retrieve the five test scores
test1 = inFile.nextDouble();
test2 = inFile.nextDouble();
test3 = inFile.nextDouble();
test4 = inFile.nextDouble();
test5 = inFile.nextDouble();

outFile.printf("Test scores: %5.2f %5.2f %5.2f "
+ "%5.2f %5.2f %n", test1, test2,
test3, test4, test5);

average = (test1 + test2 + test3 + test4
+ test5) / 5.0;
outFile.printf("Average test score: %5.2f %n",
average);

if (average >=90) outFile.printf("A");
if (average >=80 && average <90) outFile.printf("B");
if (average >=70 && average <80) outFile.printf("C");
if (average >=60 && average <70) outFile.printf("D");
if (average <60) outFile.printf("F");

inFile.close(); //Step 10
outFile.close(); //Step 10
}
}
Last edited by zuriick; 02-06-2009 at 05:35 AM.  Reply With Quote

9. ## nice code it is.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•