Results 1 to 9 of 9
Thread: What's up guys
- 02-05-2009, 10:15 PM #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 10:21 PM.
- 02-05-2009, 11:06 PM #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:
Then, in order to use that function, you might do something like: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; }
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.Java 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);
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-05-2009, 11:07 PM #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(); } }Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-05-2009, 11:17 PM #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. :)
- 02-05-2009, 11:52 PM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
- 02-06-2009, 12:42 AM #6
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.I have heard a lot of programmers say that this isn't the greatest language to be learning in
Nough said on that...
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: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.
- 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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-06-2009, 04:18 AM #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.
- 02-06-2009, 04:28 AM #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 class StudentGrade
{
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 =
new Scanner(new FileReader("test.txt"));
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 04:35 AM.
-
Similar Threads
-
Hello Guys!
By jeraldjamescapao in forum New To JavaReplies: 3Last Post: 11-23-2008, 07:39 AM -
Hello Guys!
By jeraldjamescapao in forum Advanced JavaReplies: 1Last Post: 11-22-2008, 07:42 PM -
hi guys!!
By durga in forum Advanced JavaReplies: 5Last Post: 11-15-2008, 12:29 AM -
Hi Guys!
By AnGuRuSO in forum IntroductionsReplies: 2Last Post: 10-29-2008, 09:30 AM -
Help Me Out Guys
By prince24 in forum New To JavaReplies: 10Last Post: 07-13-2007, 04:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks