Question

In: Computer Science

Create a DataEntryException class whose getMessage() method returns information about invalid integer data. Write a program...

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

Solutions

Expert Solution

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

=====================================================


Related Solutions

Write a Java program/method that takes a LinkedList and returns a new LinkedList with the integer...
Write a Java program/method that takes a LinkedList and returns a new LinkedList with the integer values squared and reversed. Example: if the LinkedList has (9, 5,4,6), then the returned list will have (36, 16,25,81). What is the time-complexity of your code? You must use listIterator for full credit. public LinkedList getReverseSquaredList (LinkedList list) { }
Create a method that takes a HashMap<Integer, Integer> and returns the sum of the keys of...
Create a method that takes a HashMap<Integer, Integer> and returns the sum of the keys of the HashMap.
Create a class named RemoveDuplicates and write code: a. for a method that returns a new...
Create a class named RemoveDuplicates and write code: a. for a method that returns a new ArrayList, which contains the nonduplicate elements from the original list public static ArrayList removeDuplicates(ArrayList list) b. for a sentinel-controlled loop to input a varying amount of integers into the original array (input ends when user enters 0) c. to output the original array that displays all integers entered d. to output the new array that displays the list with duplicates removed Use this TEST...
CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns...
CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns a sorted copy of that ArrayList with no duplicates. Sample Input: {5, 7, 4, 6, 5, 6, 9, 7} Sample Output: {4, 5, 6, 7, 9}
Create a program with the Calculator.java code RUN the program with the invalid data arguments 1,...
Create a program with the Calculator.java code RUN the program with the invalid data arguments 1, 3, 5x Take a screenshot of the output and describe what you think happened. MODIFY the code as in listing 14.2, NewCalculator.java , RUN it again with the same invalid input Take a screenshot of the new output and describe in your word document why you think the exception handling is a 'better' programming technique than just letting the program crash. __________________________________________________________________________ The Calculator.java...
Create a Java program with a method that searches an integer array for a specified integer...
Create a Java program with a method that searches an integer array for a specified integer value **(see help with starting the method header below). If the array contains the specified integer, the method should return its index in the array. If not, the method should throw an Exception stating "Element not found in array" and end gracefully. Test the method in main with an array that you make and with user input for the "needle". starting header ** public...
- Write a method sum that expects a List<Integer> as a parameter. The method returns an...
- Write a method sum that expects a List<Integer> as a parameter. The method returns an int representing the sum of the integers in the list. - Write an index-based loop that prints the contents of a list.
Write a program whose input is two integers, and whose output is the first integer and...
Write a program whose input is two integers, and whose output is the first integer and subsequent increments of 10 as long as the value is less than or equal to the second integer. Ex: If the input is: -15 30 the output is: -15 -5 5 15 25 Ex: If the second integer is less than the first as in: 20 5 the output is: Second integer can't be less than the first. import java.util.Scanner; public class LabProgram {...
write in c++ Create a program that uses EXCEPTION HANDLING to deal with an invalid input...
write in c++ Create a program that uses EXCEPTION HANDLING to deal with an invalid input entry by a user. a. Write a program that prompts a user to enter a length in feet and inches. The length values must be positive integers. b. Calculate and output the equivalent measurement in centimeters 1 inch = 2.54 centimeters c. Write the code to handle the following exceptions: If the user enters a negative number, throw and catch an error that gives...
Write a C program whose input is two integers, and whose output is the first integer...
Write a C program whose input is two integers, and whose output is the first integer and subsequent increments of 10 as long as the value is less than or equal to the second integer. Ex: If the input is: -15 30 the output is: -15 -5 5 15 25 Ex: If the second integer is less than the first as in: 20 5 the output is: Second integer can't be less than the first. For coding simplicity, output a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT