Write a Java application that asks a user to enter two integers (A and B) and th
ID: 3639478 • Letter: W
Question
Write a Java application that asks a user to enter two integers (A and B) and then display the results of A/B. Catch any division by zero errors and prompt the user to reenter the value of B. Be sure your code compiles and runs as expected. Name your Java file Yournamehw2a.java.
Write a Java interface named Searchable with two abstract methods: one named Way2Search that returns a String and another named MaxTime that returns an Integer. Be sure your code compiles and runs as expected. Name your Java file Searchable.java.
Explanation / Answer
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Yournamehw2a { public static void main (String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String input1 = null; String input2 = null; int dividend = 0; int divisor = 0; System.out.println("Enter two integers (A and B) to divide by."); while (true){ try { System.out.print("A: "); input1 = br.readLine(); dividend = Integer.parseInt(input1); break; } catch (IOException ioe) { System.out.println("IO error!"); System.exit(1); } catch (NumberFormatException nfe) { System.out.println("Must be an integer!"); } } while (true){ try { System.out.print("B: "); input2 = br.readLine(); divisor = Integer.parseInt(input2); if (divisor == 0) { System.out.println("Cannot divide by zero!"); continue; } break; } catch (IOException ioe) { System.out.println("IO error!"); System.exit(1); } catch (NumberFormatException nfe) { System.out.println("Must be an integer!"); //continue; } } System.out.println("A/B = " + (double)dividend/divisor); } } ================================================================= public interface Searchable { String Way2Search(); int MaxTime(); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.