Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

create a class within a project that throws exceptions for blank or empty inputs. test.java import java.util.Scanner; public class test { public static void

create a class within a project that throws exceptions for blank or empty inputs. 

 


test.java

import java.util.Scanner; public class test { public static void main(String[] args) { //accepting the required values from user using Scanner class String name2,nameS,name1,title; double salF,salS; Scanner sc=new Scanner(System.in); System.out.print(" Enter the faculty name: "); name2=sc.nextLine(); //name2 System.out.print("Enter the salary: "); salF=sc.nextDouble(); //salF System.out.print("Enter the department name: "); name1=sc.next(); //name1 //Working Exception **fix illegalstaffnameargumentexception, test2 to catch invalid arguments if (name2 == null || name2.isBlank() || name2.isEmpty()) throw new IllegalArgumentException("Im Sorry, a faculty name has not been entered into the system."); sc.nextLine(); System.out.print(" Enter the staff name: "); nameS=sc.next(); //nameS System.out.print("Enter the salary: "); salS=sc.nextDouble(); //salS System.out.print("Enter the Job title: "); title=sc.next(); //title //Instantiation of Faculty class Faculty fac=new Faculty(name2,salF,name1); System.out.println(" Details of Faculty:"); System.out.println("-*-*-*-*-*"); System.out.println(fac); //displays details of faculty //constructor for Exceptions //Instantiation of staff class Staff i =new Staff(title, salS, title); System.out.println(" Details of Staff:"); System.out.println("-*-*-*-*-*"); System.out.println(i); //prints job title System.out.println(nameS); //prints name System.out.println(" "); // gives space System.out.println("Alvin Sarkadi, Discussion 3, CMIS 242 - 6384 1/29/2022"); } } 

IllegalStaffNameArgumentException

public class IllegalStaffNameArgumentException extends IllegalArgumentException { public String noStaff; public IllegalStaffNameArgumentException(String nameS) { if (nameS == null) noStaff = "Staff cannot be named null"; else if (nameS.isBlank()) noStaff = "Staff name cannot be blank."; else if (nameS.isEmpty()) noStaff = "Staff name cannot be empty."; } public String toString() { return this.getClass().getSimpleName() + " : " + noStaff; } }

test2.java

 public class test2 { public static void main(String[] args) { try { test2 IllegalStaffNameArgumentException= new test2(); } catch (IllegalStaffNameArgumentException i) { System.out.println(i.toString()); }catch (IllegalArgumentException i2) { System.out.println("Exception has been caught: IllegalArgumentException"); } } }

Step by Step Solution

3.44 Rating (144 Votes )

There are 3 Steps involved in it

Step: 1

Heres the corrected code for your requirements IllegalStaffNameArgumentExceptionjava public class Il... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Programming Language Pragmatics

Authors: Michael L. Scott

4th Edition

0124104096, 978-0124104099

Students also viewed these Programming questions