For Java, in the box below is the start of a definition for a Course class. Each
ID: 3832155 • Letter: F
Question
For Java, in the box below is the start of a definition for a Course class. Each Course stores its name,
current enrollment, and maximum enrollment. Provide a complete class definition, including a Course constructor and methods so that the following code will compile:
Course introJava = new Course("Java Programming", 15, 25);
System.out.println("Course currently has " + introJava.getCurrentEnrollment() + "
students"); System.out.println("How many students to add?");
Scanner input = new
Scanner(System.in); int moreStudents =
input.nextInt();
introJava.addStudents(moreStudents);
System.out.println("Course currently has " + introJava.getCurrentEnrollment() + "
students"); if (introJava.overFull()){
System.out.println("too many students!");
}
When the code fragment above is run, the output might look like:
Course currently has 15 students
How many students to add?
18
Course currently has 33 students
too many students!
public class Course
{
private String name;
private int enrollment;
private int max_enrollment;
// fill in the rest...
}
Explanation / Answer
import java.util.Scanner;
public class Course {
private String name;
private int enrollment;
private int max_enrollment;
Course(String course_name, int course_enrollment, int course_max_enrollment){
name = course_name;
enrollment = course_enrollment;
max_enrollment = course_max_enrollment;
}
/* getter method to get current enrollment */
public int getCurrentEnrollment(){
return enrollment;
}
// method to add students in the course
public void addStudents(int num_of_students){
enrollment += num_of_students;
}
// method to check if enrollment succeed maximum enrollment
public boolean overFull(){
if(enrollment > max_enrollment){
return true;
}
return false;
}
// main method to test the program
public static void main(String args[]){
Course introJava = new Course("Java Programming", 15, 25);
System.out.println("Course currently has " + introJava.getCurrentEnrollment() + "students");
System.out.println("How many students to add?");
Scanner input = new Scanner(System.in); int moreStudents = input.nextInt();
introJava.addStudents(moreStudents);
System.out.println("Course currently has " + introJava.getCurrentEnrollment() + "students");
if (introJava.overFull()){
System.out.println("too many students!");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.