Question: Java Program using relationships between classes --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Write The Student class A Student has a: Name - the name consists of the First and Last

Java Program using relationships between classes

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Write The 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

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Write Course class

A Course has the following information:

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 3 instance variables:

courseName: String

students: Student []

numberOfStudents: int

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:

addStudent (student : Student) : void public void addStudent (Student student)

addStudent (studentName: String): void public void addStudent (String studentName)

Return a String that contains the following information concatenated so that the information prints on separate lines as shown in the sample output:

Course: Calculus; Number Of Students: 4

The students in the class are:

Student: Mary Smith; ID: 100

Student: John Jones; ID: 101

Student: Susan Thompson; ID: 102

Student: George Johnson; ID: 103

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Write a class CourseTester 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

Enter the name of a student or Q to quit

Mary Smith

Enter the name of a student or Q to quit

John Jones

Enter the name of a student or Q to quit

Susan Thompson

Enter the name of a student or Q to quit

George Johnson

Enter the name of a student or Q to quit

Q

Sample Output

Course: CPS2231; Number Of Students: 4

The students in the class are:

Student: Mary Smith; ID: 100

Student: John Jones; ID: 101

Student: Susan Thompson; ID: 102

Student: George Johnson; ID: 103

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Enhancement

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!