Can i get solution for b a. Create a CourseException class that extends Exception 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. Create a Course class with the same fields 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. Save the class as Course.java. Write an application that establishes an array of at least six Course objects with valid and invalid values. Display an appropriate message when a Course object is created successfully and when one is not. Save the file as ThrowCourseException.java. 3.b. Modify the CourseException class to extend RuntimeException class and identify the differences.

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter12: Exception Handling
Section: Chapter Questions
Problem 6PE
icon
Related questions
Question

Can i get solution for b

a. Create a CourseException class that extends Exception 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. Create a Course class with the same fields 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. Save the class as Course.java. Write an application that establishes an array of at least six Course objects with valid and invalid values. Display an appropriate message when a Course object is created successfully and when one is not. Save the file as ThrowCourseException.java.

3.b. Modify the CourseException class to extend RuntimeException class and identify the differences.

My solution for a: 

public class Course
{
private String department;
privateint courseNumber;
privatedouble credits;

public Course(String department,int courseNumber,double credits)throws CourseException
{
if(department.length()!=3||(courseNumber<100|| courseNumber>499)||(credits<0.5|| credits>6))
{
try
{
thrownew CourseException(department,courseNumber,credits);
}
catch(CourseException ex)
{

}
}

else
{
System.out.println("Course is created successfully \nDepartment name: "+department+"\nCourse number: "+courseNumber+"\ncredits: "+credits+"\n" );
}

this.department = department;
this.courseNumber = courseNumber;
this.credits = credits;
}
}
 
public class CourseException extends Exception
{
public CourseException(String dept,int course,double cred)
{
System.out.println("Object is not created!! Invalid details!!"+"\nDepartment name: "+dept+"\nCourse number: "+course+"\nCredits: "+cred+"\n");
}
}
 
public class ThrowCourseException
{
publicstaticvoid main(String[] args)throws CourseException
{
Course course1 = new Course("CSE", 101, 1.0);
Course course2 = new Course("AI", 102, 2.1);
Course course3 = new Course("ECE", 103, 2.5);
Course course4 = new Course("CSE", 1004, 3.0);
Course course5 = new Course("CSE", 105, 4.1);
Course course6 =new Course("CSE",199,8.2);
}
}
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Exception Handling Keywords
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning