A Grading program using parallel arrays – Java

What it does: 

A simple program using parallel arrays to store the data which the user enters. It will prompt the user to enter their name and their mark which will then be converted to a grade. The size of the class has been set to 10 using a constant. At the end of the program it will give a print out in in categories using the grades which how many got which grade and also there names.

 

Code:

</pre>
public class MarksWithGrades

{

static final int CLASS_SIZE =10;
 public static void main(String[] args)

{
 Scanner keyboard = new Scanner(System.in);

//PARALLEL ARRAYS
 int [] marks = new int [CLASS_SIZE];
 char [] grade = new char[CLASS_SIZE];
 String [] names = new String[CLASS_SIZE];
 //reading in marks and names
 for (int count =0; count<CLASS_SIZE; count++)
 {
 System.out.println("Please enter your name \t" + (count+1));
 names[count] = keyboard.next();

System.out.println("Please enter you mark \t" + names[count]);
 marks[count]=keyboard.nextInt();

}//for

//convert marks to grades
 for(int count =0; count<CLASS_SIZE; count++)
 {
 if (marks[count] >=70)
 {
 grade[count] = 'A';
 }//if

else if (marks[count] >=60)
 {
 grade[count] = 'B';
 }//else if

else if (marks[count] >=50)
 {
 grade[count] = 'C';
 }//else if

else if (marks[count] >=40)
 {
 grade[count] = 'D';
 }//else if

else if(marks[count] >=30)
 {
 grade[count] = 'F';
 }//else if
 }//for
 //Print out the list of name with their grades

System.out.println("The following passed with A");
 for (int count =0; count<CLASS_SIZE; count++)
 {
 if (grade[count] =='A')
 {
 System.out.println(names[count]);
 }//if

}//for

System.out.println("The following passed with B");
 for(int count=0; count<CLASS_SIZE; count++)
 {
 if (grade[count] == 'B')
 {
 System.out.println(names[count]);
 }//if

}//for

System.out.println("The following passed with C");
 for(int count =0; count<CLASS_SIZE; count++)
 {
 if(grade[count] == 'C')
 {
 System.out.println(names[count]);
 }//if

}//for

System.out.println("The following passed with a D");
 for(int count =0; count<CLASS_SIZE; count++)
 {
 if(grade[count] == 'D')
 {
 System.out.println(names[count]);
 }//if

}//for

System.out.println("The following failed");
 for(int count=0; count<CLASS_SIZE; count++)
 {
 if (grade[count] == 'F')
 {
 System.out.println(names[count]);
 }//if
 }//for

}//main
}//class
<pre>

One thought on “A Grading program using parallel arrays – Java

  1. Hi, I’m new to java and was having issues understanding methods and passing variables this post really helped me. Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s