Question: Java 1 Write a program with similar variations to the sections of code below that follow the specs listed below. Terminal or GUI style ,

Java 1

Write a program with similar variations to the sections of code below that follow the specs listed below. Terminal or GUI style , either is okay. Just something simple that will use the try-catch and type mismatch which will prevent the program from crashing

The program must use OOP style and have more than one class containing methods.

The program must at some point ask the user for the input of numbers.

There must be methods to handle the input of the numbers to insure that type mismatches should not crash the program.

The program must do at least one calculation. If it is possible to have a division by zero, there should be a try-catch to catch it and prevent the program from crashing.

There must be at least one try-catch statement in your program.

---------------------------------------

import java.util.*; public class StuLaTryCatch3 { public static void main(String[] args) { watchThis(); } public static void watchThis() { try { int firstNum = 5; int secondNum = 0; int answer = firstNum / secondNum; return; } catch(Exception e) { System.out.println("Dividing by zero causes an error. "); System.out.println("Using the try-catch helps to catch this error and prevent the program from shutting down"); } } }

-----------------------------------

import java.util.*; public class StuLaTryCatch1 { static Scanner keyboard=new Scanner(System.in); public static void main(String[] args) { getInt(); } public static int getInt() { int intgr; while(true) { try { System.out.println("Enter an Integer."); intgr=keyboard.nextInt(); System.out.println("The value you entered is an Integer."); return intgr; } catch(InputMismatchException e) { System.out.println("The value you entered is incorrect."); String other=keyboard.next(); System.out.print("It is not and integer. Please try again. "); } } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!