Reputation: 95
after spending number of hours and trying different things, i can't figure out what is wrong with my code, it's a simple program : `
public class AssignGrades {
private int ntotal=0;
private int []y;
//constructor to initialize class instances
AssignGrades(int t)
{
ntotal = t;
//y = num1;
}
AssignGrades( int []num1)
{
y=num1;
for (int i=0;i<y.length;i++)
y[i] = num1[i];
}
//method to sort grades int []num1
void setGrades()
{
int [] y = new int[ntotal];
for (int i=0;i<y.length;i++)
{
//assign grades
if
(y[i]<80){
System.out.println("grade is A" +y[i]);}
else if (y[i]<70)
System.out.println("grade is B" +y[i]);
else if (y[i]<60)
System.out.println("grade is c" +y[i]);
else
System.out.println("FAIL" +y[i]);
}
}
//show student grades - to print array[] values
void showGrades()
{
for (int u: y)
System.out.println(u);
}
}`
my client program
`import java.util.Scanner;
public class AssignGradesDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
int t=0;
System.out.println("enter no of students" );
Scanner input = new Scanner(System.in);
{
t=input.nextInt();
// input.close();
}
int [] num1 = new int[t];
System.out.println("enter grades");
Scanner input1 = new Scanner(System.in);
for (int i=0;i<num1.length;i++)
{
num1[i]=input1.nextInt();
}
input1.close();
AssignGrades ag = new AssignGrades(t);
AssignGrades ag1 = new AssignGrades( num1);
ag.setGrades();
ag1.showGrades();
}
}
output is:
enter no of students
2
enter grades
78
98
grade is A0
grade is A0
78
98
Question: now in the output 'A' and '0' -> where the problem is, it looks like array is not initialized, all the values appear to be zero: whereas when I print them separately, they are initialized.?!
Please let me know if more clarification is required. thanks
Upvotes: 0
Views: 74
Reputation: 39457
You have a local variable called y and a class variable with the same name y. That seems to be the problem. You are using the local y but you meant to use the class y, I think.
OK, your code had lots of problems. Here is the fixed version.
public class AssignGrades {
private int[] y;
public AssignGrades(int[] num1) {
y = num1;
}
// method to set grades
void setGrades() {
for (int i = 0; i < y.length; i++)
{
// assign grades
if (y[i] < 50)
System.out.println("FAIL" + y[i]);
else if (y[i] < 60)
System.out.println("grade is C" + y[i]);
else if (y[i] < 70)
System.out.println("grade is B" + y[i]);
else if (y[i] < 80) {
System.out.println("grade is A" + y[i]);
}
}
}
// method to show student grades
void showGrades() {
for (int u : y){
System.out.println(u);
}
}
}
import java.util.Scanner;
public class AssignGradesDemo {
public static void main(String[] args) {
int t = 0;
System.out.println("enter no of students");
Scanner input = new Scanner(System.in);
t = input.nextInt();
int[] num1 = new int[t];
System.out.println("enter grades");
for (int i = 0; i < num1.length; i++) {
num1[i] = input.nextInt();
}
input.close();
AssignGrades ag = new AssignGrades(num1);
ag.setGrades();
ag.showGrades();
}
}
Upvotes: 3