Write a Java application that asks a user to enter two integers (A and B) and th
ID: 3552342 • 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
package com.Project.com.Project.Chegg;
import java.util.Scanner;
public class Yournamehw2a {
public static void main(String[] args) {
// TODO Auto-generated method stub
while(true){ // TO re-prompt the user to enter the valid values
System.out.println("Please enter the two integers"); //to get the user inputs
Scanner Sc = new Scanner(System.in);
int A = Sc.nextInt();
int B = Sc.nextInt();
try{ //try block actual calculation
System.out.println(A/B);
break; // to break from the while loop if there is no exception
}
catch(ArithmeticException e){ //catch block to handle exception
System.out.println("Invalid operation :" + e.getMessage());//print the message
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.