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

Java 1 Some help Please...... 1. Before You Begin Anticipate where things can go

ID: 3860616 • Letter: J

Question

Java 1

Some help Please......

1. Before You Begin

Anticipate where things can go wrong
Consider how to gracefully shut down the program and save the users data and close files properly.
Typically there are two scenarios to make a distinction between:

The first is one that which you have absolutely no control over; such as if all the files are where they should be, and that the user has not deleted one or accidentally moved it rather than copied it. In this case the program should not continue because the results cannot be trusted or even processed.

The second scenario is when, for instance, a user enters a double, when the input should be an int. In this situation you can, by good programming, maintain control of the program. In a poorly written program, the program will crash with an input mismatch. However, you can catch this type of error and ask the user to reenter a number of the proper type.
In both of these scenarios you want things to go smoothly without upsetting the user, and have the program either shut down smoothly and save all data, or continue with a reentering of a number, with little disruption to the user.

2. The Assignment

2.1. Specifications

2.2. General

Follow this with the name of the program: TryCatch.

Use good comments in the body of the program.

Think about the problem and decide what type each of the input numbers should be. Also, think about the calculations and decide what type the variables should be that will hold the results of the calculations.

Use camel casing for variable names, and use descriptive variable names. Do not use general variable names like "tax" or "total."

Take a look at the method below.
This method will cause the program to crash.

Example 1: A method that tries to perform an impossible task

Here is another example of a method that asks for an int and displays the number entered.
A good way to handle a situation like this is to have a method (think one task) that will return an int from the user, but will continue to ask for input until an int is received. This would prevent a type mismatch and cause the program to end.
You could use this method any time you needed an int from the user. You might also create a method for doubles as well.
You would not put the prompting in the method because the prompting would be different in each program.

Example 2: A method that will return an int.

2.3. The Problem

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.

Anticipate where things can go wrong
Consider how to gracefully shut down the program and save the users data and close files properly.
Typically there are two scenarios to make a distinction between:

The first is one that which you have absolutely no control over; such as if all the files are where they should be, and that the user has not deleted one or accidentally moved it rather than copied it. In this case the program should not continue because the results cannot be trusted or even processed.

The second scenario is when, for instance, a user enters a double, when the input should be an int. In this situation you can, by good programming, maintain control of the program. In a poorly written program, the program will crash with an input mismatch. However, you can catch this type of error and ask the user to reenter a number of the proper type.
In both of these scenarios you want things to go smoothly without upsetting the user, and have the program either shut down smoothly and save all data, or continue with a reentering of a number, with little disruption to the user.

Explanation / Answer

example one without try catch:
import java.util.*;
public class Cheggexample {
public static void main(String[] args) {
// TODO Auto-generated method stub
watchThis();
}
public static void watchThis()
{
int theFirstNumber = 5;
int theSecondNumber = 0;
int theAnswer = theFirstNumber / theSecondNumber;
return;
}
}
output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Cheggexample.watchThis(Cheggexample.java:11)
at Cheggexample.main(Cheggexample.java:5)



code with try catch:
import java.util.*;
public class Cheggexample {
public static void main(String[] args) {
// TODO Auto-generated method stub
watchThis();
}
public static void watchThis()
{
try{
int theFirstNumber = 5;
int theSecondNumber = 0;
int theAnswer = theFirstNumber / theSecondNumber;
return;
}catch(Exception e){
System.out.println("Divisible by zero causes error:");
System.out.println("try catch can help u out from sudden shut down of program:");
}
}
}
output:
Divisible by zero causes error:
try catch can help u out from sudden shut down of program:


example 2:
import java.util.*;
public class Cheggexample2{
static Scanner keyboard=new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
getInt();
}
public static int getInt()
{
while(true) //
{
try
{
return keyboard.nextInt();
}
catch(InputMismatchException e)
{
keyboard.next();   
System.out.print("That is not and integer. Please try again: ");
}
}
}
output:
when integer given:12
normal stop of excecuton:
output when non int given:
12.44
That is not and integer. Please try again: 22.3
That is not and integer. Please try again: 33.4
That is not and integer. Please try again: 22

import java.util.*;
public class Cheggexample {
static Scanner keyboard=new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
getInt();
}
public static int getInt()
{
int intgr;
while(true) //
{
try
{
System.out.println("enter integer:");
intgr=keyboard.nextInt();
System.out.println("it is integer:");
return intgr;

}
catch(InputMismatchException e)
{
System.out.println("input you have entered is not integer: plese enter again");
String other=keyboard.next();
System.out.print("That is not and integer. Please try again: ");
}
}
}
}

output:
enter integer:
12
it is integer:

output when non int
enter integer:
12.9
input you have entered is not integer: plese enter again
That is not and integer. Please try again: enter integer:
99.8
input you have entered is not integer: plese enter again
That is not and integer. Please try again: enter integer:
12
it is integer:

/*comments
while loop terminates when it return only integer
so try catch block is writen inside the while loop
if user enters integer then it loop ends
but when user enters non integer it contunusly iterates till integer..
without try catch the program causes or deviates or abnormal termination*/

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