JAVA QUESTION Part 2: Course Registration (50 points) Write a Student and Course
ID: 3585967 • Letter: J
Question
JAVA QUESTION
Part 2: Course Registration (50 points) Write a Student and Course class that might be part of a course registration system. A student is described by: first name last name student ID (as text) whether or not tuition is paid A course is described by: a name the maximum number of students that can be enrolled in the class a roster of students enrolled in the course (stored as a Student[]) Student Class (10 points) In your class, include: instance data variables a constructor getters and setters a toString method Course Class (40 points) In your class, include instance data variables a constructor a course is initially created with no students on the roster getters and setters think about which variables should have setters include validity checks where appropriate a toString method include the name, number of students enrolled in the course, maximum number that can be enrolled, and a printed roster as part of the text representation make sure that the text representation does not include any "nulls" an addStudent method method header: public boolean addStudent(Student student) if there is room for a student and the student has paid their tuition, add the student to the roster a dropStudent method method header: public boolean dropStudent(Student student) if the student is on the roster, remove them
Explanation / Answer
public class Student {
private String firstname;
private String lastname;
private String Id;
public Student(String firstname, String lastname, String id) {
super();
this.firstname = firstname;
this.lastname = lastname;
Id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
@Override
public String toString() {
return "Student [firstname=" + firstname + ", lastname=" + lastname + ", Id=" + Id + "]";
}
}
======================================================================================
import java.util.Arrays;
public class Course {
private String name;
private Student[] student;
public Course(String name) {
// TODO Auto-generated constructor stub
this.name = name;
}
public Course(String name, Student[] student) {
super();
this.name = name;
this.student = student;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student[] getStudent() {
return student;
}
public void setStudent(Student[] student) {
this.student = student;
}
public boolean addStudent(Student stud){
for(int i=0;i<student.length;++i){
if(student[i]==null)
student[i] = stud;
return true;
}
return false;
}
public boolean dropStudent(Student stud){
for(int i=0;i<student.length;++i){
if(student[i].getFirstname().equals(stud.getFirstname()))
student[i] = null;
return true;
}
return false;
}
@Override
public String toString() {
return "Course [name=" + name + ", student=" + Arrays.toString(student) + "]";
}
}
=========================================================================================
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.