Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part I Write a class CourseListException that extends Exception with the followi

ID: 3685984 • Letter: P

Question

Part I

Write a class CourseListException that extends Exception with the following constructors:

----A default constructor that gives the error message "Too many courses."

----A constructor that takes a string and uses this for the error message

Part II

Write a class Student with the following attributes:

----Two instance variables (with corresponding getters and setters):

name - a String for the name of the student

courses - an array of Strings with length 5, holding the names of the courses the Student is taking.

----A default constructor that initializes name to be "NoName".

----A constructor that takes a String to set the name of the student

----Two methods:

assignCourses (no input parameters or return values) - This method should ask a user to input courses from the console window until the user enters an empty line, which indicates the end of the array. If the user enters more than 5 courses, throw a CourseListException

writeStudent (no input parameters or return values) - writes the name of the student and the names of the student's courses to the console window

Part III

Write a class PartTimeStudent, a subclass of Student, for students who can take at most 2 courses. The class should include the following:

----A default constructor that invokes the superclass constructor

----A constructor that takes a String to set the name of the Student

----An overridden assignCourses that throws a CourseListException when a user tries to assign more than 2 courses to a PartTimeStudent.

Part IV

Write a class TestStudent that does the following:

1. Creates a student with the name Alice

2. Assigns some courses to Alice

3. Writes the information about Alice to the console window

4. Creates a PartTimeStudent with the name Bob

5. Assigns courses to Bob

6. Writes the information about Bob to the console

Explanation / Answer

SOlution:

package com.nancy.chegg.qanda;

public class CourseListException extends Exception {

   private static final long serialVersionUID = 1L;
   String msg = null;
   public CourseListException() {
       super();
       msg = "Too many courses.";
       System.out.println("********************");
       System.out.println("ERROR : " + msg);
       System.out.println("********************");
   }

   public CourseListException(String msg) {
       super(msg);
       this.msg = msg;
       System.out.println("********************");
       System.out.println("ERROR : " + msg);
       System.out.println("********************");
   }
}

_________________________________________________________

package com.nancy.chegg.qanda;

import java.util.Scanner;

public class Student {
   String name;
   String[] courses = new String[5];

   Student() {
       this.name = "NoName";
   }

   Student(String name) {
       this.name = name;
   }

   public void assignCourses() throws CourseListException {

       System.out.println("Please assign some course to " + this.name);
       Scanner scan = new Scanner(System.in);
       int counter = 0;
       while (counter <= 5) {
           System.out.println("Enter course name : ");
           courses[counter] = scan.nextLine();
           if (courses[counter].equals("")) {
               break;
           }
           counter++;
           if (counter == 5) {
               throw new CourseListException();
           }
       }
   }

   public void writeStudent() {
       System.out.println("Name : " + name);
       System.out.println("Course Details : ");
       for (int i = 0; i < 5 && courses[i] != null
               && !courses[i].equals(""); i++) {
           System.out.println((i + 1) + ". " + courses[i]);
       }

   }
}

__________________________________________________________________-

package com.nancy.chegg.qanda;

import java.util.Scanner;

public class PartTimeStudent extends Student{
  
   PartTimeStudent(){
       super();
   }
  
   PartTimeStudent(String name){
       this.name = name;
   }
   @Override
   public void assignCourses() throws CourseListException {
       System.out.println("Please assign some course to " + this.name);
       Scanner scan = new Scanner(System.in);
       int counter = 0;
       while (counter <= 2) {
           System.out.println("Enter course name : ");
           courses[counter] = scan.nextLine();
           counter++;
           if(counter == 2) {
               throw new CourseListException();
           }
       }
   }

}

________________________________________________________________________

package com.nancy.chegg.qanda;

public class TestStudent {

   public static void main(String[] args) {
      
       Student stu1 = new Student("Alice");
       try {
           stu1.assignCourses();
       } catch (CourseListException e) {
       }
       stu1.writeStudent();
      
       PartTimeStudent stu2 = new PartTimeStudent("Bob");
       try {
           stu2.assignCourses();
       } catch (CourseListException e) {
       }
       stu2.writeStudent();

   }

}

___________________________________________________________

Sample run :

Please assign some course to Alice
Enter course name :
Math
Enter course name :
Science
Enter course name :

Name : Alice
Course Details :
1. Math
2. Science
Please assign some course to Bob
Enter course name :
math
Enter course name :
physics
********************
ERROR : Too many courses.
********************
Name : Bob
Course Details :
1. math
2. physics

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote