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

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...
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...
Write a method sumTo that accepts an integer parameter n and returns the sum of the...
Write a method sumTo that accepts an integer parameter n and returns the sum of the first n reciprocals. In other words: sumTo(n) returns: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n For example, the call of sumTo(2) should return 1.5. The method should return 0.0 if passed the value 0 and should print an error message and return -1 if passed a value less than 0. Include a loop. Please help for Java programming.
Write a Java program to process the information for a bank customer.  Create a class to manage...
Write a Java program to process the information for a bank customer.  Create a class to manage an account, include the necessary data members and methods as necessary.  Develop a tester class to create an object and test all methods and print the info for 1 customer.  Your program must be able to read a record from keyboard, calculate the bonus and print the details to the monitor.  Bonus is 2% per year of deposit, if the amount is on deposit for 5 years...
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT