You are required to do the followings: 1. Draw UML represents the class Course 2
ID: 3702148 • Letter: Y
Question
You are required to do the followings:
1. Draw UML represents the class Course
2. The array size is fixed with 5 elements only. Improve it to automatically increase the array size by creating a new larger array and copying the contents of the current array to it
3. Implement the method toString to enable the program to write the object directly 4. Implement the method dropStudent that enable to drop student from the course.
5. Add new method named clear() that remove all students from the course. 6. Write a test program that creates a course, add three students, removes one, and display the students in the course
The code:
public class Course {
private String courseID;
private String courseName;
private String[] students = new String[5];
private int numberOfStudents = 0;
public Course () {}
public Course(String courseID, String courseName)
{
this.courseID = courseID;
this.courseName = courseName;
}
public String getCourseID() {
return courseID;
}
public void setCourseID(String courseID) {
this.courseID = courseID;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String[] getStudents() {
return students;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public void setNumberOfStudents(int numberOfStudents) {
this.numberOfStudents = numberOfStudents;
}
public void addStudent(String student) {
students[numberOfStudents] = student;
numberOfStudents++;
}
public void dropStudent(String student) {
}
}
public class TestCourse {
public static void main(String[] args) {
Course c1 = new Course("ITE-401","IT Project Management");
Course c2 = new Course();
c1.addStudent("Ali");
c1.addStudent("Luay");
c1.addStudent("Hussam");
c1.addStudent("Naser");
c2.setCourseID("cen");
c2.setCourseName("cen444");
c2.addStudent(" Ali");
c2.addStudent("Luay");
System.out.println("Number of students in course : "
+c1.getCourseName() +" is " + c1.getNumberOfStudents());
String[] s1 = c1.getStudents();
for (int i = 0; i < c1.getNumberOfStudents(); i++)
System.out.println(s1[i]);
System.out.println(" =========================================");
System.out.println("Number of students in course: "
+c2.getCourseName() +" is "+ c2.getNumberOfStudents());
String[] s2 = c2.getStudents();
for (int i = 0; i < c2.getNumberOfStudents(); i++)
System.out.println(s2[i]);
}}
Explanation / Answer
public class Course {
private String courseID;
private String courseName;
private String[] students;
private int numberOfStudents = 0;
int currentSize = 5;
public Course() {
students = new String[currentSize];
}
public Course(String courseID, String courseName) {
this.courseID = courseID;
this.courseName = courseName;
students = new String[currentSize];
}
public String getCourseID() {
return courseID;
}
public void setCourseID(String courseID) {
this.courseID = courseID;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String[] getStudents() {
return students;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public void setNumberOfStudents(int numberOfStudents) {
this.numberOfStudents = numberOfStudents;
}
public void addStudent(String student) {
if(numberOfStudents == (currentSize-1)) {
String[] tempSdudent = new String[currentSize * 2];
currentSize = currentSize * 2;
int count = 0;
for(String s: students) {
tempSdudent[count++] = s;
}
tempSdudent[count-1] = student;
students = tempSdudent;
numberOfStudents = count;
}else {
students[numberOfStudents] = student;
numberOfStudents++;
}
}
public void dropStudent(String student) {
String[] tempSdudent = new String[numberOfStudents-1];
int count = 0;
for(String s: students) {
if(s!=null)
if(s.equals(student)) {
//Skip dont add
}else {
tempSdudent[count++] = s;
}
}
students = tempSdudent;
numberOfStudents = numberOfStudents - 1;
}
public void clrear() {
students = new String[5];
currentSize = 5;
numberOfStudents = 0;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(" Course ID " + courseID);
sb.append(" Course Name " + courseName);
sb.append(" Total Students " + numberOfStudents);
for(String s: students) {
if(s!=null)
sb.append(" Student " + s );
}
return sb.toString();
}
}
=============
public class TestCourse {
public static void main(String[] args) {
Course c1 = new Course("ITE-401", "IT Project Management");
c1.addStudent("Ali");
c1.addStudent("Luay");
c1.addStudent("Hussam");
c1.addStudent("Naser");
c1.addStudent("Mustafi");
c1.addStudent("Abbas");
c1.addStudent("Mohamad");
Course c2 = new Course();
c2.setCourseID("cen");
c2.setCourseName("cen444");
c2.addStudent(" Ali");
c2.addStudent("Luay");
System.out.println("Number of students in course : " + c1.getCourseName() + " is " + c1.getNumberOfStudents());
String[] s1 = c1.getStudents();
for (int i = 0; i < c1.getNumberOfStudents(); i++)
System.out.println(s1[i]);
System.out.println(" =========================================");
System.out.println("Number of students in course: " + c2.getCourseName() + " is " + c2.getNumberOfStudents());
String[] s2 = c2.getStudents();
for (int i = 0; i < c2.getNumberOfStudents(); i++)
System.out.println(s2[i]);
System.out.println("Print using toString");
System.out.println(c1.toString());
c1.dropStudent("Ali");
System.out.println("Print using toString after removing student");
System.out.println(c1.toString());
c1.clrear();
System.out.println("Print using toString after removing student");
System.out.println(c1.toString());
}
}
===============
Output
Number of students in course : IT Project Management is 7
Ali
Luay
Hussam
Naser
Mustafi
Abbas
Mohamad
=========================================
Number of students in course: cen444 is 2
Ali
Luay
Print using toString
Course ID ITE-401
Course Name IT Project Management
Total Students 7
Student Ali
Student Luay
Student Hussam
Student Naser
Student Mustafi
Student Abbas
Student Mohamad
Print using toString after removing student
Course ID ITE-401
Course Name IT Project Management
Total Students 6
Student Luay
Student Hussam
Student Naser
Student Mustafi
Student Abbas
Student Mohamad
Print using toString after removing student
Course ID ITE-401
Course Name IT Project Management
Total Students 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.