Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Number tudents in course1: 3 10.5 Case Study: Desi ook provides a peter Jones, Kim Smith, Anne Kennedy, Number of students in course2: 2 section
Number tudents in course1: 3 10.5 Case Study: Desi ook provides a peter Jones, Kim Smith, Anne Kennedy, Number of students in course2: 2 section and the nd has students n use a class to The Course class is implemented in Listing 10.6. It uses an array to store the studer course. For simplicity, assume the maximum course enrollment is 100. The array using new String [100] in line 3. The addStudent method (line 10) adds a stude array. Whenever a new student is added to the course, numberOf Students is increa as an exercise. 12). The getStudents method returns the array. The dropStudent method (line he course. LISTING 10.6 Course . java public class Course { name. private String courseName; AWN - private String students = new String [100] ; private int numberOfStudents; 5 6 public Course (String courseName) { 7 this . courseName = courseName; the course. 8 9 10 public void addStudent (String student) { 11 students [ numberOf Students ] = student; 12 numberOfStudents++; by passing 13 student) 14 15 method, and public String getStudents ( ) { 16 return students; the Course 17 ents to them. 18 19 public int getNumberOfStudents ( ) { 20 return numberOfStudents; 21 22 23 24 public String getCourseName ( ) { 25 return courseName ; 26 27 28 public void dropStudent (String student) { 29 / / Left as an exercise in Programming Exercise 10.9 30 the array size is fixed to be 100 (line 3), so you cannot have more than 100 studer purse. You can improve the class by automatically increasing the array size in Prog Exercise 10.9 Whdent ( student: String ) : void Adds a new student to the cour +dropStudent (student: String) : void Drops a student from the course +getStudents ( ) : String[ ] Returns the students for the cou +get NumberOfStudents ( ) : int Returns the number of students FIGURE 10.10 The Course class models the courses. A Course object can be created using the constructor Course (String a course name. You can add students to the course using the addStudent (Str method, drop a student from the course using the dropStudent (String studer return all the students in the course using the getStudents ( ) method. Suppose class is available; Listing 10.5 gives a test class that creates two courses and adds s LISTING 10.5 TestCourse . java public class TestCourse { public static void main (String args) { A WN- Course course1 = new Course ("Data Structures") ; Course course2 = new Course ("Database Systems") ; course1 . addStudent ("Peter Jones" ) ; course1 . addStudent ("Kim Smith") ; 8 course1 . addStudent ("Anne Kennedy" ) ; 9 10 course2 . addStudent ("Peter Jones" ) ; 11 course2 . addStudent ("Steve Smith") ; 12 13 System . out . printin ("Number of students in course1: 14 + course1 . getNumberOfStudents ( ) ) ; 15 String students = course1 . getStudents() ; 16 for (int i = 0; i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started