Help with JAVA Create a presentation class that will ask the user for a student
ID: 3919405 • Letter: H
Question
Help with JAVA
Create a presentation class that will ask the user for a student ID, a student name and the number of hours the student will be taking in the upcoming quarter. Create and use three custom exception classes; one for each of the fields listed below:
Student ID: 11111 – 99999
Student name: 1 – 30
characters Student hours: 2.5 - 22
Display an appropriate message if the above criteria is not met. Once the user has entered valid data, display the information back to them.
Explanation / Answer
import java.util.Scanner; public class Presentation { public static void main(String[] args) { Scanner in = new Scanner(System.in); try { System.out.print("Enter student's id: "); int id = in.nextInt(); if(id < 11111 || id > 99999) { throw new BadStudentID(); } System.out.print("Enter student's name: "); String name = in.nextLine(); if(name.length() < 1 || name.length() > 30) { throw new BadStudentName(); } System.out.print("Enter number of hours taken in next quarter: "); double hours = in.nextDouble(); if(hours < 2.5 || hours > 22) { throw new BadStudentHours(); } System.out.println("Id: " + id); System.out.println("Name: " + name); System.out.println("Hours: " + hours); } catch (BadStudentID e) { System.out.println("Invalid student id"); } catch (BadStudentName e) { System.out.println("Invalid student name"); } catch (BadStudentHours e) { System.out.println("Invalid number of hours"); } } } class BadStudentID extends Exception { } class BadStudentName extends Exception { } class BadStudentHours extends Exception { }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.