Object Arrays
String Arrays
Primitive Arrays
Write a program that will compute and display the final grades for students in a class. A student's final grade is determined from the scores obtained in the class exams. Each student in the class may be given a different number of exams as determined by the instructor. The final grade is determined by the average of the scores in all the exams given.
For each student, the program will input student id, name, the number of exams given and scores in each of the exams. The scores in each of the exams will vary from 0 to 100. Each exam will carry the same weight. The final grade will be computed by taking the average of the scores in all the exams given (i.e. by adding the scores in all the exams and dividing it with the number of exams given). The final grade will be assigned depending upon the percentage of total points in the exams given as follows:
A – 90 to 100%
B – 80 to 89%
C – 70 to 79%
D – 60 to 69%
F – 0 to 59%
Instructions
Use a String variable for keeping final grade. For comparing two strings use the method: equals( ) or equalsIgnoreCase ( ). For details about using these methods of String class, consult textbook.
For this assignment create the following classes.
Student class
Create a class Student that provides the following:
TestStudent class
Write a class TestStudent containing the main method.
Method main
The method main will do the following:
Method I.
Create five empty Strings, outA, outB, outC, outD, outF. Use the String outA for storing output relating to A students; use String outB for storing output relating to B students; etc. Use the object accessor methods to access object values as needed. At the end, prepare a String outAll that concatenates all the above five Strings. Then display the String outAll.
Method II.
Create a String array out of 5 Strings. Use out [0] for accumulating output relating to A students, out[1] for B students etc. Write a static method displayRestult to display output. Call the static method displayResult and pass it the array out.
displayResult
For method II above, write a static method displayResult. This method will receive a String array and display the contents of all elements of the String array one by one. Here is a proposed header for the method:
public static void displayResult (String [ ] s)
{
}
You may use either the method I or method II above. However, it is suggested that you use method II as it teaches you how to use String arrays.
The sample code given below uses method II for doing the output.
Input
Use the following data for test input:
1,John Adam,3,93,91,100
2,Raymond Woo,3,65,68,63
3,Rick Smith,3,50,58,53
4,Ray Bartlett,3,62,64,69
5,Mary Russell,3,93,90,98
6,Andy Wong,3,89,88,84
7,Jay Russell,3,71,73,78
8,Jimmie Wong,3,70,77,72
9,Jackie Chan,3,85,89,84
10,Susan Wu,3,80,88,84
In the above data, for the first entry, id is 1, name is John Adam, number of exams is 3, exam scores are 93, 91, 100 .
Output
1 John Adam (A)
5 Mary Russell (A)
6 Andy Wong (B)
9 Jackie Chan (B)
10 Susan Wu (B)
7 Jay Russell (C)
8 Jimmie Wong (C)
2 Raymond Woo (D)
4 Ray Bartlett (D)
3 Rick Smith (F)
//The sample code below use method II discussed above for doing the output
public class Student
{
private int id;
private String name;
private int [] exams;
public Student(int id, String n, int [] ex)
{
//Below id refers to local variable.
//this.id refers to the instance variable.
this.id = id;
name = n;
//create exams array of same length as ex array
exams = new int [ex.length];
//copy contents from array ex to exams
System.arraycopy(ex, 0, exams, 0, ex.length);
}
public String findGrade()
{
String grade;
//enter code here for finding the final grade.
return grade;
}
public int getId ( )
{
return id;
}
public String getName ( )
{
return name;
}
}
import javax.swing.*;
import java.util.*;
public class TestStudent
{
public static void main(String[] args)
{
String in, name, token;
int nStudents, id, nExams;
int [] scores;
in = JOptionPane.showInputDialog("Enter number of students");
nStudents = Integer.parseInt(in);
// Create an array of nStudents references
Student [] st = new Student [nStudents];
// Create nStudents objects
for (int i = 0; i < st.length; i++)
{
// Input one student data
in = JOptionPane.showInputDialog("Enter one student data");
// Tokenize student data using StringTokenizer
// Create Student object
st [i] = new Student (id, name, scores);
}
// Find student grades
// Create an array out of 5 string references
String [] out = new String [5];
//Create 5 String objects. Initialize each with “”
//and store their references in the above array of references.
for (int i=0; iout [i] = new String ( “” ); //alternate form: out [i] = “”;
//The above two parts can be done with a single statement as below:
//String [] out = new String [ ] {“”, “”, “”, “”, “”};
//find student grades and accumulate output for each type of student.
String grade;
for (int i = 0; i < st.length; i++)
{
grade = st[i].findGrade();
if (grade.equalsIgnoreCase("A"))
{
//accumulate output in out[0] for A students.
}
else if (grade.equalsIgnoreCase("B"))
{
//accumulate output in out[1] for B students.
}
else if (grade.equalsIgnoreCase("C"))
{
//accumulate output in out[2] for C students.
}
else if (grade.equalsIgnoreCase("D"))
{
//accumulate output in out[3] for D students.
}
else
{
//accumulate output in out[4] for F students.
}
}
// Call diaplayResults to display grades
displayResults (out);
}
public static void displayResults(String[] s)
{
//Create a String outAll initialized with an empty string.
String outAll = "";
//Accumulate elements of the received String array s into String outAll.
for (int i = 0; i < s.length; i++)
{
outAll = outAll + s[i];
}
JOptionPane.showMessageDialog(null, outAll);
}
}