In: Computer Science
Create a DataEntryException class whose getMessage() method returns information about invalid integer data. Write a program named GetIDAndAge that continually prompts the user for an ID number and an age until a terminal 0 is entered for both. If the ID and age are both valid, display the message ID and Age OK.
Throw a DataEntryException if the ID is not in the range of valid ID numbers (0 through 999), or if the age is not in the range of valid ages (0 through 119). Catch any DataEntryException or InputMismatchException that is thrown, and display the message Invalid age or ID - DataEntryException - \, where \ is the value of the invalid input. For example:
Enter ID 1000 Enter age 40 Invalid age or ID - DataEntryException
=============================================
public class DataEntryException extends Exception {
public DataEntryException(int num) {
}
}
==================================================
import java.util.*;
public class GetIDAndAge {
public static void main(String[] args) {
int id;
int age;
final int QUIT = 0;
int returnVal = QUIT + 1;
Scanner keyboard = new Scanner(System.in);
while (returnVal != QUIT) {
// Write your code here
}
}
public static int check(int idNum, int ageNum) throws
DataEntryException {
// Write your code here
}
public static void showStatus(String msg) {
// Write your code here
}
}
Thanks for the question. Below is the code you wll be needing Let me know if you have any doubts or if you need anything to change. Thank You !! =========================================================================== public class DataEntryException extends Exception { public DataEntryException(int num) { super("Invalid Age or ID - Data Entry Exception : "+num); } }
=====================================================
import java.util.*; public class GetIDAndAge { public static void main(String[] args) { int id; int age; final int QUIT = 0; int returnVal = QUIT + 1; Scanner keyboard = new Scanner(System.in); while (returnVal != QUIT) { // Write your code here System.out.print("Enter ID "); id = keyboard.nextInt(); System.out.print("Enter age "); age = keyboard.nextInt(); try { returnVal = check(id, age); if (returnVal == -1) { System.out.println("ID and Age OK."); } } catch (DataEntryException e) { showStatus(e.getMessage()); } } } public static int check(int idNum, int ageNum) throws DataEntryException { // Write your code here if (0 <= idNum && idNum <= 999) { if (0 <= ageNum && ageNum <= 119) { if (idNum == 0 && ageNum == 0) return 0; else return -1; } else { throw new DataEntryException(ageNum); } } else { throw new DataEntryException(idNum); } } public static void showStatus(String msg) { // Write your code here System.out.println(msg); } }
=====================================================