Question

In: Computer Science

An exceptions lab. Create a new class named EnterInfo Request user enter name and age Accept...

An exceptions lab.

  • Create a new class named EnterInfo
  • Request user enter name and age
  • Accept a string and an int
  • Use custom Exception to warn user when name is blank
  • Use custom Exception to warn user when age is invalid
    • 0 > age > 120
  • Use general Exception to warn user when age is not an int
  • Loop through until user enters all valid data

Solutions

Expert Solution

Program:

import java.util.*;


//custom exception for blank name
class BlankException extends Exception
{
BlankException(String str)
{
super(str);
}
}

//custom exception for invalid age
class InvalidException extends Exception
{
InvalidException(String str)
{
super(str);
}
}


class EnterInfo
{
public static void main(String args[])
{

String name,age;
Scanner sc=new Scanner(System.in);
while(true)
{
try
{
System.out.print("Enter your name: ");
name=sc.nextLine();

if(name.equals(""))
{
throw new BlankException("Name should not be blank!!");
}

System.out.print("Enter your age(0 > age > 120): ");
age=sc.next();
Integer.parseInt(age);
if(Integer.parseInt(age)<0 || Integer.parseInt(age)>120)
{
throw new InvalidException("Age has to be greater than 0 and less than 120");
}

break;

}
catch(BlankException be)
{
System.out.println(be);
}
catch(InvalidException ie)
{
System.out.println(ie);
sc.nextLine();
}
catch(NumberFormatException nf)
{
System.out.println(nf);
sc.nextLine();
}
}
}
}

Output:


Related Solutions

An exception with finally block lab. Create a new class named ReadArray Create simple array and...
An exception with finally block lab. Create a new class named ReadArray Create simple array and read an element using a try block Catch any exception Add a finally block and print a message that the operation if complete
Create a PHP class named "User" with the following private fields: name, birthdate in yyyy/mm/dd format,...
Create a PHP class named "User" with the following private fields: name, birthdate in yyyy/mm/dd format, age, and department. In the class, include getter and setter methods to get and set the values of those variables. Author a data entry webform using HTML text input boxes and a submit button. When the user clicks on the submit button, a "User" class is instantiated and the new User object's fields are populated. The HTML input boxes correspond to the field in...
Write a class to accept a sentence from the user and prints a new word in...
Write a class to accept a sentence from the user and prints a new word in a terminal formed out of the third letter of each word. For example, the input line “Mangoes are delivered after midnight” would produce “neltd”. Program Style : Basic Java Programming
Lab 7   - Rectangle class-   (Lec. 7) 1.) Create a new project and name it:    Rectangle...
Lab 7   - Rectangle class-   (Lec. 7) 1.) Create a new project and name it:    Rectangle /* OUTPUT: Enter the width of the court: 60 Enter the length of the court: 120 The width of the court is 60 feet. The length of the court is 120 feet. The area of the court is 7200 square feet. Press any key to continue . . . */ 2.) Create the following 3 files: Rectangle.h Rectangle.cpp Source.cpp 3.) Write a program that...
2. Create a new project named named lab5_2. You will prompt the user for an amount...
2. Create a new project named named lab5_2. You will prompt the user for an amount of names to enter, and then ask for that amount of names. You’ll store these names in a vector of strings. Then you’ll sort the vector. Finally, you’ll print the results. How many names?: 4 Enter a name: Leonardo Enter a name: Donatello Enter a name: Michelangelo Enter a name: Raphael ==================== Alphabetized ==================== Donatello                  Leonardo Michelangelo                      Raphael
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
Create a program that when run, prompts the user to enter a city name, a date,...
Create a program that when run, prompts the user to enter a city name, a date, the minimum temperature and the maximum temperature. Calculate the difference between the maximum temperature and the minimum temperature. Calculate the average temperature based on the minimum and maximum temperature. Print out the following: City name today's Date minimum temperature maximum temperature average temperature difference between minimum and maximum The following is a sample run, user input is shown in bold underline. Enter City Name:...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT