2.99 See Answer

Question: Create a CourseException class that extends the

Create a CourseException class that extends the Java Exception class and whose constructor receives a String that holds a college course’s department (for example, CIS), a course number (for example, 101), and a number of credits (for example, 3). Save the file as CourseException.java.
b. Create a Course class with the same fields as those included in the CourseException class and whose constructor requires values for each field. Upon construction, throw a CourseException if the department does not consist of three letters, if the course number does not consist of three digits between 100 and 499 inclusive, or if the credits are less than 0.5 or more than 6. Each CourseException object should include an appropriate message that explains the reason the construction failed. Include a toString() method that returns a String that consists of an object’s department, course number, and credits. Save the class as Course.java.
c. Write an application that establishes an array of six Course objects. Create an array of six Strings to hold department names, six ints to hold course numbers, and six doubles to hold credits. In a loop, prompt the user for values for each field. Accept any value for the department name. If the user does not enter an int for the course number, continually reprompt until an int is entered. If the user does not enter a numeric value for the credits, reprompt until a number is entered. In a new loop, try to construct six Course objects, each with values taken from the three arrays of field values. If a Course Exceptionis thrown when constructing any of the objects, create the object using the default constructor. Then, in a third loop, display all the stored Course values using the Course toString() method. Save the file as ThrowCourseException.java.


See Answer