Question: Question 1 Open the Exercise project ( the Exercise folder ) . Complete the following by editing CourseCode.java ( a ) Write a method addCourseCode

Question 1
Open the Exercise project (the Exercise folder).
Complete the following by editing CourseCode.java
(a) Write a method addCourseCode(String courseCode) to add a new course code to the courseCodeSet attribute.
(b) In main() method, create a CourseCode object name courseCode. Add the following course code to the object.
COMP1030SEF
COMP2020SEF
COMP3120SEF
COMP
COMPSEF
(c) Write a method printCourseCodeLevel() to display the level of each course code stored in the courseCodeSet. The level is represented by an integer that located at the fifth character of the course code. For example, COMP1030SEF is a level 1 course.
Hint:
- String method substring can be used to extract the course level in String type from the course code.
o StringIndexOutOfBoundsException will be thrown if an index is either negative or greater than the size of the string.
- Method Integer.parseInt can be used to convert the extracted course level into an integer.
o NumberFormatException will be thrown if a string is converted to one of the numeric types, but it does not have the appropriate format.
- Implement an exception handler in the method to handle the StringIndexOutOfBoundsException and NumberFormatException with a try-catch statement. If StringIndexOutOfBoundsException is caught, displays a message Improper course code length: and the course code. If NumberFormatException is caught, displays a message Invalid course code format: and the course code.
(d) In main() method, test the method printCourseCodeLevel().
---------- Capture Output ----------
COMP1030SEF: Level 1
COMP2020SEF: Level 2
COMP3120SEF: Level 3
Improper course code length: COMP
Invalid course code format: COMPSEF

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 Programming Questions!