Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a full program that will have a user enter in a person\'s age and if age

ID: 645450 • Letter: C

Question

Create a full program that will have a user enter in a person's age and if age is negative it will catch the exception and display an error message Sorry age must be positive. It will test the age and if age is under 5 or greater than 55 it will display you pay 5.50 and if any other positive age it will say you pay 11.00. All interaction can be done on the console. create a method in the exception that will be giveSecondChance() that will allow user to re-enter a positive age if a negative one is entered.

JAVA pls help

Explanation / Answer

import java.util.Scanner;
public class Age {
   public static int giveSecondChance()
   {
       Scanner scan=new Scanner(System.in);
       System.out.println("Reenter your age");
       int x=scan.nextInt();
       return x;
   }

   public static void main(String[] args)
   {   int age;
       Scanner scan=new Scanner(System.in);
       System.out.println("Please enter your age:");
       try
       {
       age=scan.nextInt();
       if(age<0)
       }catch(IllegalArgumentException e)
       {
           System.out.println("The age you have entered is invalid");
           age=giveSecondChance();
       }
       if(age>0 && age<5){
           System.out.println("You pay 0");
       }
       else if(age>=5 && age<=55){
           System.out.println("You pay 5.50");
       }
       else{
           System.out.println("You pay 11.0");
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote