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...


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);

}

}




Jun 05, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here