You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. import java.util.*;
  2. public class MathProblems
  3. {
  4. // Implement this method, which prompts the user for a and b, then divides a by b
  5. // and then calculates a % b. If the user enters an invalid type of input, or a
  6. // zero for the divisor, an Exception will be thrown, both of which need to be caught
  7. public void divide()
  8. {
  9. Scanner input = new Scanner(System.in);
  10. int a=1;
  11. int b=1;
  12. try
  13. {
  14. System.out.print("Enter the numerator: ");
  15. a = input.nextInt();
  16. System.out.print("Enter the divisor: ");
  17. b = input.nextInt();
  18. int div = a/b;
  19. int mod = a%b;
  20. System.out.println(a+" / "+b+" is "+div+" with a remainder of "+mod);
  21. }
  22. catch(InputMismatchException ime)
  23. {
  24. System.out.println("Invalid input type");
  25. }
  26. catch(ArithmeticException ae)
  27. {
  28. System.out.println("You can't divide "+a+" by "+b);
  29. }
  30. }
  31. public static void main( String[] args )
  32. {
  33. // Test it here in main, when ready
  34. MathProblems functions = new MathProblems();
  35. functions.divide();
  36. }
  37. }