Modif the following program to check for your last name and age. Assign your las
ID: 3653727 • Letter: M
Question
Modif the following program to check for your last name and age. Assign your last name and age for the constant values in check() function. Make sure to remove the id and all the related code for id.
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)
{
try
{
System.out.print("Enter ID ");
id = keyboard.nextInt();
System.out.print("Enter age ");
age = keyboard.nextInt();
returnVal = check(id, age);
}
catch(DataEntryException ex)
{
showStatus("Invalid age or ID - " + ex.getMessage());
}
catch(InputMismatchException ex)
{
showStatus("Invalid data type");
keyboard.nextLine();
}
}
}
public static int check(int idNum, int ageNum) throws DataEntryException
{
final int HIGHID = 999;
final int HIGHAGE = 119;
final int QUIT = 0;
int returnVal = 1;
if(idNum < QUIT || idNum > HIGHID)
throw (new DataEntryException(idNum));
if(ageNum < QUIT || ageNum > HIGHAGE)
throw (new DataEntryException(ageNum));
if(idNum == QUIT && ageNum == QUIT)
returnVal = QUIT;
else
{
showStatus("ID and Age OK");
}
return returnVal;
}
public static void showStatus(String msg)
{
System.out.println(msg);
}
}
// DataEntryException
public class DataEntryException extends Exception
{
public DataEntryException(int num)
{
super("DataEntryException - " + num);
}
}
Explanation / Answer
Please rate...
Modified Program:
==================================================
import java.util.*;
public class GetIDAndAge
{
public static void main(String[] args)
{
String ln;
int age;
final int QUIT = 0;
int returnVal = QUIT + 1;
Scanner keyboard = new Scanner(System.in);
Scanner keyboard1 = new Scanner(System.in);
while(returnVal != QUIT)
{
try
{
System.out.print("Enter Last Name ");
ln = keyboard1.nextLine();
System.out.print("Enter age ");
age = keyboard.nextInt();
returnVal = check(ln, age);
}
catch(DataEntryException ex)
{
showStatus("Invalid age or ID - " + ex.getMessage());
}
catch(InputMismatchException ex)
{
showStatus("Invalid data type");
keyboard.nextLine();
}
}
}
public static int check(String lastName, int ageNum) throws DataEntryException
{
final String LASTNAME = "Potter";
final int HIGHAGE = 119;
final int QUIT = 0;
int returnVal = 1;
if(!lastName.equalsIgnoreCase(LASTNAME))
throw (new InputMismatchException(lastName));
if(ageNum < QUIT || ageNum > HIGHAGE)
throw (new DataEntryException(ageNum));
if(lastName.equalsIgnoreCase(LASTNAME) && ageNum == QUIT)
returnVal = QUIT;
else
{
showStatus("Last name and Age OK");
}
return returnVal;
}
public static void showStatus(String msg)
{
System.out.println(msg);
}
}
// DataEntryException
class DataEntryException extends Exception
{
public DataEntryException(int num)
{
super("DataEntryException - " + num);
}
}
========================================================
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.