Answered step by step
Verified Expert Solution
Question
1 Approved Answer
i need code for this project ( java). LAB 04: OBJECT-ORIENTED PROGRAMMING EXERCISE 1 1. Run the code below and enter an integer between 1
i need code for this project ( java).
LAB 04: OBJECT-ORIENTED PROGRAMMING EXERCISE 1 1. Run the code below and enter an integer between 1 and 10. 2. Run the program again and enter 5.5. Although this number is between 1 and 10 , the program will abort. Examine the error message. You should see the word Exception, the method where the exception occurred (main), the class name of the exception (InputMismatchException), as well as the call stack listing the method calls. 3. Add a try/catch block to catch and handle the InputMismatchException exception. Identify the statements that cause the error as well as the portions of the program that depend upon these statements. Enclose these statements within the try block. Follow the try block with the catch block given below. Note, the InputMismatchException class is defined in java.util and must be imported. Also, when the Scanner throws an InputMismatchException, the input token will remain in the buffer so that it can be examined by the program. In our case, we will not be examining the token, but will simply clear out of the buffer to start over. catch (InputmismatchException e) \{ System.out.println("Enter integers only, with no spaces or other characters"); scan.next(); / / clear the scanner buffer \} 4. Compile and run the program again, testing with a variety of input (integers, floats, characters) The program should not abort when floats or character data is given. import java.util. Scanner; public class Lab04 \{ public static void main(String[] args) \{ Scanner scan = new Scanner(System. in); int num =0; do \{ System.out.println("Enter a number between 1 and 10); num = scan. nextInt (); if (num 10 ) System.out.println (" Illegal value, " + num + " entered. Please try again."); \} while (num 10); system.out. println(" Value correctly entered! Thank you."); \}Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started