# What's up guys

• 02-05-2009, 11:15 PM
zuriick
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?
• 02-06-2009, 12:06 AM
y0y
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:

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:

Code:

```double[] testScores = new double[5]; testScores[0] = 95.4; testScores[1] = 86.2; testScores[2] = 99.4; testScores[3] = 100; testScores[4] = 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.)
• 02-06-2009, 12:07 AM
MK12
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)
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();     } }```
• 02-06-2009, 12:17 AM
y0y
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. :)
• 02-06-2009, 12:52 AM
Quote:

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.
• 02-06-2009, 01:42 AM
CJSLMAN
Quote:

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...
Quote:

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
• 02-06-2009, 05:18 AM
zuriick
^^ 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.
• 02-06-2009, 05:28 AM
zuriick
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
}
}
• 02-06-2009, 05:39 AM
Fubarable
nice code it is.