Page 629 - Introduction to Programming with Java: A Problem Solving Approach
P. 629
System.out.println("done");
} // end main
} // end class FantasyFootball
2. Given the above program. What is the output if the user enters 1 in response to the prompt?
3. [after §14.4] Add a try-catch structure to the following program to make it compile and execute
correctly, even when the divisor is zero. Note that division by zero throws an ArithmeticException. import java.util.Scanner;
public class Division
{
}
// end Division class
*
public static void main(String[] args)
{
}
Scanner stdIn = new Scanner(System.in);
int n, d, q;
System.out.print("Enter numerator: ");
n = stdIn.nextInt();
System.out.print("Enter divisor: ");
d = stdIn.nextInt();
q = n / d;
System.out.println(q);
// end main
To help you out, we’veApropviadedgthoe catPchDbFlock,Ebelnowh: ancer catch (ArithmeticException e)
{
}
System.out.println("Error, but keep going anyway.");
There’s no need to check for correct input; you may assume that the user enters two properly formatted int values for input.
4. [after §14.5] Program Improvement:
The following program performs division and does not throw an exception when you input a zero for the denominator. It also does not detect input number format exceptions. Minimize the total lines of code required to meet the requirements.
/*************************************************************
Division2.java
Dean & Dean
This attempts to prevent division by zero.
*************************************************************/
*
*
*
import java.util.Scanner;
public class Division2
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
Exercises 595