Please help with this computer science homework on Classes in JAVA i\'d really a
ID: 3555378 • Letter: P
Question
Please help with this computer science homework on Classes in JAVA i'd really appreciate it! You task is to create a Student class and then develop a main program that will create instances of students to represent a set of students in CS 110 in the Summer of 2014, their grades and semester average.
Part 1: Your main program will prompt the user for the name of a data file containing the basic information for students. The first line of the file indicates how many students are in the file. Using this information the main program will allocate an array of students. It will then read the data file one at a time, creating student objects and storing them into the array.
Part 2:
Your main program will implement 3 static methods which will manipulate the student array:
Explanation / Answer
import java.util.StringTokenizer;
// creating class
class Student{
// for two parameter as name and grade
String name;
int grade;
public Student(String name, int g){
this.name= name;
this.grade=g;
}
public Student(){
this("",0);
}
public String getName(){
return this.name;
}
public int getGrade(){
return this.grade;
}
}
// creating the list of students and getting input
public class StudentList {
Student[] students;
public StudentList(){
students= new Student[0];
}
// processing user input and separating the name with grades
public void getData(String input){
StringTokenizer st = new StringTokenizer(input);
students= new Student[st.countTokens()/2];
for(int i =0;st.hasMoreTokens();i++){
String name = st.nextToken();
int grade= Integer.parseInt(st.nextToken());
students[i]= new Student(name,grade);
}
}
public void getAdddata(String input){
StringTokenizer st = new StringTokenizer(input);
Student[]oldlist = students;
students= new Student[oldlist.length+st.countTokens()/2];
for(int i= 0;i<oldlist.length;i++)
students[i]=oldlist[i];
for(int i=oldlist.length;st.hasMoreTokens();i++){
String name = st.nextToken();
int grade= Integer.parseInt(st.nextToken());
students[i]= new Student(name,grade);
}
}
public float getAverage(){
// assuming the total grade to be zero
float total=0;
// creating loop to go through each and every grade in array
for (int i=0;i<length();i++)
total+=students[i].grade;
return total/students.length;
}
}
public static void main(String[] args)
{
System.out.println("Reading File from Java code");
//Name of the file
String fileName="info.txt";
try{
//Create object of FileReader
FileReader inputFile = new FileReader(fileName);
//Instantiate the BufferedReader Class
BufferedReader bufferReader = new BufferedReader(inputFile);
//Variable to hold the one line data
String line,line1;
line1 = bufferReader.readLine();
Integer i = Integer.valueOf(line1);
Student[] students;
students= new Student[i];
// Read file line by line and print on the console
while ((line = bufferReader.readLine()) != null) {
students.getAdddata(line);
}
//Close the buffer reader
bufferReader.close();
}catch(Exception e){
System.out.println("Error while reading file line by line:"
+ e.getMessage());
}
}