I am a TA for a java class at my university and a student confronted me with a very odd problem today in lab. I looked over it for about an hour and had the other TA in lab do the same, yet we could not find a problem.
Effectively what we are doing here is creating 3 arrays, passing them into a new method. Modifying the value of those array in the new method and returning back to the original method. We are not using the return statement to return any of the arrays to the original method. Instead we are levering, what I can only describe being from a C background as pass-by-reference. However upon returning to the original method that values have changed to some incorrect values.
In this specific example we have three arrays called: "exams", "quizzes" and "labs." Each of these arrays are of size 1,000 and are initialized to -1. Inside the first method "calcGrade" we create these arrays and initialize them. Then we pass all three arrays to the second method which captures the amount of exams, quizzes and labs the user has and then stores the actual exam, quiz and lab grade values to the arrays.
METHOD 1 (calcGrade)
exams quizzes labs
-1 -1 -1
-1 -1 -1
-1 -1 -1
-1 -1 -1
. . .
. . .
. . .
METHOD 2 (getScores)
exams quizzes labs
90 80 90
-1 80 90
-1 -1 -1
-1 -1 -1
. . .
. . .
. . .
BACK to METHOD 1 (calcGrades)
exams quizzes labs
80 90 90
-1 -1 90
-1 -1 -1
-1 -1 -1
. . .
. . .
. . .
Can anyone think of any reason this could be happening? I am honestly stumped and I don't want him to lose credit for something that doesn't seem to be wrong...
Here is the code (please note that there are several println statements in there for debugging purposes):
import java.util.Scanner;
public class CSE1341Grade{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
System.out.println("What is your first name? ");
String first = input.nextLine();
System.out.println("What is your last name? ");
String last = input.nextLine();
calcGrade(first, last);
}
public static void calcGrade(String first, String last){
int base = 1000;
int[] quizzes = new int [base];
int[] exams = new int [base];
int[] labs = new int [base];
for(int x = 0; x < base; x++)
{
quizzes[x] = -1;
exams[x] = -1;
labs[x] = -1;
}
int[] countarr = getScores(quizzes, exams, labs);
System.out.println("EXAMS:");
for(int x = 0; x < countarr[0]; x++)
System.out.println(exams[x]);
System.out.println("QUIZ:");
for(int x = 0; x < countarr[1]; x++)
System.out.println(quizzes[x]);
System.out.println("LABS:");
for(int x = 0; x < countarr[2]; x++)
System.out.println(labs[x]);
for(int x = 0; x < countarr.length; x++)
System.out.println(countarr[x]);
//System.out.println("----");
double examAvg =0.0;
for(int i=0;i<countarr[0];i++){ //adding together scores
examAvg+=exams[i];
//System.out.println(examAvg);
}
//System.out.println("----");
double quizAvg=0.0;
for(int i=0;i<countarr[1];i++){ //adding together scores
quizAvg+=quizzes[i];
//System.out.println(quizAvg);
}
//System.out.println("----");
double labAvg=0.0;
for(int i=0;i<countarr[2];i++){ //adding together scores
labAvg+=labs[i];
//System.out.println(labAvg);
}
examAvg = examAvg/countarr[0];
quizAvg = quizAvg/countarr[1];
labAvg = labAvg/countarr[2];
double totalAverage = (.5 * examAvg) + (.35 * quizAvg) + (.1 *labAvg) + 5.0;
System.out.println("Total Score: " +totalAverage);//display average
String grade = "";
if (totalAverage >= 90)
grade = "A";
else if (totalAverage >= 80)
grade ="B";
else if (totalAverage >= 70)
grade = "C";
else
grade = "F";
System.out.println(first + " " + last + " your grade is a: " + grade); //letter grade
}
public static int [] getScores(int [] exams, int [] quizzes, int [] labs){
Scanner input = new Scanner(System.in);
int [] countArray = new int[3]; //holding numbers of exams quizzes labs
System.out.println("How many exam grades do you have? ");
countArray[0] = input.nextInt();
System.out.println("How many quiz grades do you have? ");
countArray[1] = input.nextInt();
System.out.println("How many lab grades do you have?" );
countArray[2] = input.nextInt();
System.out.println(countArray[0] + ", " + countArray[1] + ", " + countArray[2]);
for(int counter = 0; counter < countArray[0]; counter++){ //every exam score
System.out.printf("Enter Exam" + " " + (counter + 1) + " " + "score: ");
exams[counter]=input.nextInt();
System.out.println(exams[counter]);
}
System.out.println("----");
for(int counter = 0; counter < countArray[1]; counter++){ //every quiz score
System.out.printf("Enter Quiz" + " " + (counter + 1) + " " + "score: ");
quizzes[counter]=input.nextInt();
System.out.println(quizzes[counter]);
}
System.out.println("----");
for(int counter = 0; counter < countArray[2]; counter++){ //every lab score
System.out.printf("Enter Lab" + " " + (counter + 1) + " " + "score: ");
labs[counter]=input.nextInt();
System.out.println(labs[counter]);
}
System.out.println("----");
System.out.println("EXAMS:");
for(int x = 0; x < countArray[0]; x++)
System.out.println(exams[x]);
System.out.println("QUIZ:");
for(int x = 0; x < countArray[1]; x++)
System.out.println(quizzes[x]);
System.out.println("LABS:");
for(int x = 0; x < countArray[2]; x++)
System.out.println(labs[x]);
System.out.println("************************");
return countArray; //return back to calc grade
}
}
String x = "hello";, then the value ofxis not theStringobject, but a reference to it. The object itself is, in fact, passed by reference because the value ofxis the reference to the object. What it does not do is pass a reference to the variable (that itself is just a reference to the object), which is why you can't change a variable in the calling code by passing it to a method. I guess that my main point is simply that "pass by value of the reference" is not standard terminology.