In: Computer Science
IN JAVA ECLIPSE PLEASE
The xxx_Student class
A Student has a:
– Name - the name consists of the First and Last name separated by a space.
– Student Id – a whole number automatically assigned in the student class
– Student id numbers start at 100. The numbers are assigned using a static variable
in the Student class
• Include all instance variables
• Getters and setters for instance variables
• A static variable used to assign the student id starting at 100
• A toString method which returns a String containing the student name and id in the
format below:
Student: John Jones ID: 101
The xxx_Course class
A Course has the following information (modify your Course class):
– A name
– An Array of Students which contains an entry for each Student enrolled in the course
(allow for up to 10 students)
– An integer variable which indicates the number of students currently enrolled in the
course.
Write the constructor below which does the following:
Course (String name)
Sets courseName to name
Creates the students array of size 10
Sets number of Students to 0
Write the 3 getters
+getCourseName() : String
+getStudents() : Student []
+getNumberOfStudents() : int
Write the 2 setters
public void addStudent (Student student)
public void addStudent (String studentName)
Write toString
Return a String that contains the following information concatenated so that the
information prints on separate lines as shown in the sample output:
Course: xxxxx Number of Students: xx
The students in the class are:
Each of the Student objects in the students array followed by a \n so they print on
separate lines
Write a class xxx_TestCourse which will
Prompt the user for the name of a Course and create a Course object
In a loop, until the user enters a q or a Q,
Prompt the user to enter student names or Q to end
For each student entered,
create a Student object and add it to the Course using the addStudent method of
the Course class
At the end of the loop, print the Course object. Its toString method will format the
output as shown on the next slide
Sample Output
Enter the course name
CPS2231-04
Enter the name of a student or Q to quit
Jon
Enter the name of a student or Q to quit
Mary
Enter the name of a student or Q to quit
Tom
Enter the name of a student or Q to quit
q
Course: CPS2231-04 Number of Students: 3
The students in the class are:
Student: Jon ID: 100
Student: Mary ID: 101
Student: Tom ID: 102
Enhancement 1 (5 extra points)
Change the Course class so that a Course can have an unlimited number of students.
Start out with an Array of 10 Student objects
Once the Array is full,
Create a new Array of Student of twice the size as the original
Copy the elements from the full array to the new array
Re-assign the references
code
Student.java student class
public class Student
{
private String Name;//instance varibale Name that
holds
private int id;//instace variable id that store the id
of the studnt
static int idStart=100;//static variable whosw scope
is limeted to this class
//perameterize cunstructor that will assing name to Name and
idStart value to id
public Student(String name)
{
Name=name;
id=idStart;
idStart++;//it will increament by 1
when every time object of this class is created
}
public String getName()//accessor method that will
return the name of the student
{
return Name;
}
public int getId()//accessor method that will return
the id of the student
{
return id;
}
@Override
//override the toString method that will return formatted
string
public String toString() {
return "Student: " + Name + " Id: " + id ;
}
}
Course.java course class
public class Course
{
private String courseName;//instance varibale named coueseName that
is hold the name of the course
private Student []students;//declare the array of student
objects
private int numberOfStudents;//and instance variable
numberOfStudents that will hold the current number of studenr in
the array
//perameterize constructor that recived the parameter type
string
public Course(String courseName) {
this.courseName = courseName;//assing the name of the course to the
instance variable courseNmae
this.students=new Student[10];//initialize the students array with
size 10
numberOfStudents=0;//and assign numberOfStudents to 0
}
public String getCourseName() {//accessor method that will
return the course name of the Course
return courseName;
}
public Student[] getStudents() {//accessor method that will
return the array of the student object name of the Course
return students;
}
public int getNumberOfStudents() {//accessor method that will
return the current number of student in the array
return numberOfStudents;
}
public void addStudent (Student student)//add Student method has
perameter type student
{
if(numberOfStudents==students.length)//fisrt check if there is
space in array means the number student is same as size of the
student
{
increaseArraySize();//if so then it will call the
increaseArraySize;
}
students[numberOfStudents]=student;//add the student object to
students array at numberOfStudents position in the array
numberOfStudents++;//increamt student count by 1
}
public void addStudent (String studentName)//its another method
that will recive the string parameter that is tha name of the
student
{
if(numberOfStudents==students.length)//fisrt check if there is
space in array means the number student is same as size of the
student
{
increaseArraySize();//if so then it will call the
increaseArraySize;
}
//it will create the new student object and add to the array
students[numberOfStudents]=new Student(studentName);
numberOfStudents++;//increamt student count by 1
}
@Override
//this is override method that will return the formatted string
that contains the name of the couese and all the
//student that enrolled in that perticular course
public String toString()
{
String str="";//declare an empty string
str+="Course: "+getCourseName()+" Number of studenrs
:"+numberOfStudents;
str+="\nThe students in the class are:";
for(int i=0;i<numberOfStudents;i++)
{
str+="\n"+students[i].toString();
}
return str;
}
//private method increaseArraySize that will return nothing
private void increaseArraySize() {
Student[] newArray = new Student[students.length * 2];//it will
create an array of type of student with doubled the size of the
students array
//copy all the student object to the new array one by one
for(int i = 0; i < students.length; i++)
{
newArray[i] = students[i];
}
//assing new array to students instance variable
students=newArray;
}
}
TestCourse.java with main method
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
// this is test class with main method
public class TestCourse {
public static void main(String[] args) throws IOException
{
//crate the object of the BufferedReader for user input from the
console
BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
String courseName;
//ask from the user to enter the name of the course
System.out.println("Enter the course name");
courseName=reader.readLine();//read the name with reader object and
assing to courseName varible
Course crcs=new Course(courseName);//creates the object of the
course Class with perameter courseName
String sName;
while(true)//infinite while loop untill user will enter q as
student name
{
//promot user to enter the name of the student
System.out.println("Enter the name of a student or Q to
quit:");
//read then name and assign to variable sName
sName=reader.readLine();
//check if sName is q or Q
if(sName.equalsIgnoreCase("q"))
break;//then it will break the loop
crcs.addStudent(sName);//other wise call addStudent method of
couese class with string perameter
}
System.out.println(crcs);//this will print the course object crcs
.. means it will call toString method of course class
}
}
output
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.