In: Computer Science
In this exercise, you will create a basic data management system for students in the Department of Computer Science and Information Technology using the linked list.
You must write two classes – Student class attached, and StudentChart.
Student class consists of student data. StudentChart class contains a linked list of all students in the department.
For this purpose, you must fulfill the following interfaces (note - fields of the objects should be marked as private).
Class: Student chart
Class of Student Chart contains data for all students stored in a linked list. The way to add a student to the list by method addStudent only to the front of the list. At the beginning the student list is empty, the number of students enrolled in the Department is 0. Think what would be the fields of this class, so that you can know much information at any one time.
Constructors:
Creat the class chart according to the private |
|
StudentChart() |
.fields(variables). |
Methods:
Return type |
Method name and parameters |
operation |
Add student to the front of the linked list. |
||
void |
addStudent(Student student |
|
Delete student from the list. Do nothing if the student does not exist in the list. Returns false if student not found in the list |
||
boolean |
deleteStudent(Student student) |
|
int |
getNumOfStudents() |
Returns the number of students in the list. |
boolean |
isStudent(Student student) |
Examine if student exists in the list. |
Student |
getStudent(int studentNum) |
Returns students data for the students with studentNum. |
Returns the student average in the course with number |
||
double |
getAverage(int course) |
course |
Returns student average in all courses for all students. |
||
double |
getAverage() |
(You can use the Student class methods). |
Prints the students data for all students as follow: Student number 1: <Student String representation> Student number 2: <Student String representation> … Student number <n>: <Student String representation> If there are no students print: |
||
void |
printStudentChart() |
No students! |
It is recommended to write tester to present all the methods of the class and to check that they actually work.
The student class :
public class Student { private String name;// student name private long id; // student identity number private int [] grades = new int[5]; // list of student courses grades //costructor that initialize name and id public Student(String name, long id){ String n = name.toUpperCase(); name = name.replace(name.charAt(0), n.charAt(0)); for(int i=0 ; i<name. length(); i++) if(name.charAt(i)==' ') name = name.replace(name.charAt(i+1), n.charAt(i+1)); this.name = name; this.id=id; } //copy constructor public Student(Student st){ this.name= st.name; this.id = st.id; } //return the student name public String getName(){ return name; } //returns the student id number public long getId(){ return id; } // returns the course grade accoring to parameter course(course number) public int getGrade(int course){ if(grades[course]== 0) return -1; return grades[course]; } // set the name of student with parameter name public void setName(String name){ String n = name.toUpperCase(); name = name.replace(name.charAt(0), n.charAt(0)); for(int i=0 ; i<name. length(); i++) if(name.charAt(i)==' ') name = name.replace(name.charAt(i+1), n.charAt(i+1)); this.name = name; } //sets the student id with parameter id public void setId(long id){ this.id = id; } //set the course grade with parameter grade public void setGrade(int course, int grade){ grades[course]= grade; } // set all the grades of the student from user public void setGrades(int [] g){ for(int i=0; i< grades.length; i++) grades[i]=g[i]; } //returns the student average in his course public double getAverage(){ int sum= 0; int count =0; for(int i=0; i< grades.length; i++){ if(grades[i]!=-1) sum=+ grades[i]; count++; } return sum/count*1.0;// compute and return average } // takes a student object as a parameter and // return true if it is the same as student parameter public boolean equals(Student student){ if(student.id== this.id) return true; else return false; } //returns a string that represents the student object public String toString(){ String [] g = new String[5]; for(int i=0; i< grades.length; i++) if(grades[i]!= -1) g[i]= grades[i]+" "; else g[i]="no grade"; String n1= "Name:"+ name+"\n"+ "ID: "+ id +"\n"+ "Grades:"+"\n"+ "Introduction to Programming - "+g[0] +"\n"+"Introduction to Management – "+g[1]+"\n"+ "Data Structures – "+g[2]+"\n"+"Introduction to Economics – "+g[3]+"\n"+ "Introduction to Mathematics – "+g[4]; return n1; } }
please find the solution for the same:-
package studentdata;
import java.util.LinkedList;
public class StudentChat {
LinkedList<Student> list = new
LinkedList<Student>();;
public static void main(String[] args) {
StudentChat studentChat = new
StudentChat();
studentChat.printStudentChart();
// Adding student 1
Student student = new
Student("Rohan", 1);
student.setGrade(0, 9);
student.setGrade(1, 9);
student.setGrade(2, 8);
student.setGrade(3, 6);
student.setGrade(4, 7);
studentChat.addStudent(student);
Student student2 = new
Student("Raj", 2);
student2.setGrade(0, 9);
student2.setGrade(1, 9);
student2.setGrade(2, 8);
student2.setGrade(3, 6);
student2.setGrade(4, 7);
studentChat.addStudent(student2);
System.out.println("Total Number of
students created: " + studentChat.getNumOfSudents());
studentChat.printStudentChart();
System.out.println("####################");
//getaverage
double
average=student.getAverage();
System.out.println("total Average
for student "+student.getId()+" :-"+average);
System.out.println("####################");
double
averagecourse=student.getAverage(3);
System.out.println("total Average
for student "+student.getId()+" :-"+averagecourse);
System.out.println("####################");
//is valid student
boolean
isValidStudent=studentChat.isStudent(student2);
System.out.println("Student
exists:-"+isValidStudent);
System.out.println("####################");
// get the student
Student stut =
studentChat.getStudent(2);
System.out.println(stut.toString());
System.out.println("####################");
// delete
studentChat.deleteStudent(student2);
studentChat.printStudentChart();
System.out.println("####################");
}
public void addStudent(Student student) {
list.addFirst(student);
}
public boolean deleteStudent(Student student)
{
return list.remove(student);
}
public int getNumOfSudents() {
return list.size();
}
public boolean isStudent(Student student) {
return
list.contains(student);
}
public Student getStudent(int studentNum) {
for (int i = 0; i < list.size();
i++) {
if
(list.get(i).getId() == studentNum) {
return list.get(i);
}
}
return null;
}
public void printStudentChart() {
int count = list.size();
if (count > 0) {
for (int i = 0;
i < list.size(); i++) {
System.out.println("Student number " +
list.get(i).getId());
System.out.println(list.get(i).toString());
}
} else {
System.out.println("No Students");
}
}
}
package studentdata;
public class Student {
private String name;// student name
private long id; // student identity number
private int[] grades = new int[5]; // list of student
courses grades
// costructor that initialize name and id
public Student(String name, long id) {
String n =
name.toUpperCase();
name = name.replace(name.charAt(0),
n.charAt(0));
for (int i = 0; i <
name.length(); i++)
if
(name.charAt(i) == ' ')
name = name.replace(name.charAt(i + 1),
n.charAt(i + 1));
this.name = name;
this.id = id;
}
// copy constructor
public Student(Student st) {
this.name = st.name;
this.id = st.id;
}
// return the student name
public String getName() {
return name;
}
// returns the student id number
public long getId() {
return id;
}
// returns the course grade accoring to parameter
course(course number)
public int getGrade(int course) {
if (grades[course] == 0)
return -1;
return grades[course];
}
// set the name of student with parameter
name
public void setName(String name) {
String n =
name.toUpperCase();
name = name.replace(name.charAt(0),
n.charAt(0));
for (int i = 0; i <
name.length(); i++)
if
(name.charAt(i) == ' ')
name = name.replace(name.charAt(i + 1),
n.charAt(i + 1));
this.name = name;
}
// sets the student id with parameter id
public void setId(long id) {
this.id = id;
}
// set the course grade with parameter grade
public void setGrade(int course, int grade) {
grades[course] = grade;
}
// set all the grades of the student from
user
public void setGrades(int[] g) {
for (int i = 0; i <
grades.length; i++)
grades[i] =
g[i];
}
public double getAverage(int course) {
return grades[course]*1.0;
}
// returns the student average in his course
public double getAverage() {
double sum = 0.0;
int count = 0;
for (int i = 0; i <
grades.length; i++) {
if (grades[i] !=
-1)
sum +=grades[i];
count++;
}
return sum / count;
}
// takes a student object as a parameter and
// return true if it is the same as student
parameter
public boolean equals(Student student) {
if (student.id == this.id)
return
true;
else
return
false;
}
// returns a string that represents the student
object
public String toString() {
String[] g = new
String[5];
for (int i = 0; i <
grades.length; i++)
if (grades[i] !=
-1)
g[i] = grades[i] + " ";
else
g[i] = "no grade";
String n1 = "Name:" + name +
"\n" + "ID: " + id + "\n" + "Grades:" + "\n" + "Introduction to
Programming - "
+ g[0] + "\n" + "Introduction to Management – "
+ g[1] + "\n" + "Data Structures – " + g[2] + "\n"
+ "Introduction to Economics – " + g[3] + "\n" +
"Introduction to Mathematics – " + g[4];
return n1;
}
}