This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
I am dealing with aggregates and i have created a program that will ask for user input to determine a student average. I ask the user to enter test score 1,2 and 3 for student number one, and then ask teh same info for student number 2. the problem is that i call teh variable testScore1,testScore2,testScore3. As a result, student 2 will output the same grade values as student one. I believe teh problem is that Java has no way to know that once student one's average is calculated taht it must set the counter back to zero and now use new values to calculate the average for student 2. I'm not sure how to do this wehn i've used a constructor to initialize the testScores to zero.
Here is my code.
Being a scholar of life does not require a student loan.
Aside from some minor formatting and style issues, your code looks correct.
What's happening when you execute it and put in different values? Your testScore variables are independent for each student object that you create (inside the student object), so there shouldn't be any problem with that. As far as the average, there is no "counter" it just returns a computation based on the variables that are, again, independent from other students within your student object. I don't see any showstoppers on first pass... ???
Joined: Apr 03, 2010
i don't have any opportunity to enter a value for the second student (marsha) because i get compile errors. The program is not recognizing the difference between
John's testScore1 and Marsha's testScore1. When i try to compile it's saying that its already defined most likely because we already referenced these score values when we got John's
StudentBody.java:54: testScore1 is already defined in main(java.lang.String)
int testScore1 = scan.nextInt();
StudentBody.java:56: testScore2 is already defined in main(java.lang.String)
int testScore2 = scan.nextInt();
StudentBody.java:58: testScore3 is already defined in main(java.lang.String)
int testScore3 = scan.nextInt();
these compile errors are happening at the point where i request input for the second students scores
You can only have uniquely named variables. However, you can reassign values to a variables once its declared.
Similarly for testScore2 and testScore3.
Joined: Oct 28, 2009
oops. Missed that!
Got carried away with the claims of bad output when the code doesn't even compile....harrumph.
Joined: Apr 03, 2010
hoo haa!! that worked.......
but just so i'm clear on how this is working............if i added on to this program to allow averages for 5 students isntead of 2 , would this line of code " testScore1 = scan.nextInt(); "
allow me to pass a new value to the test score?