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
41 lines
1.1 KiB
import java.util.*;
|
|
public class MathProblems
|
|
{
|
|
|
|
// Implement this method, which prompts the user for a and b, then divides a by b
|
|
// and then calculates a % b. If the user enters an invalid type of input, or a
|
|
// zero for the divisor, an Exception will be thrown, both of which need to be caught
|
|
public void divide()
|
|
{
|
|
Scanner input = new Scanner(System.in);
|
|
int a=1;
|
|
int b=1;
|
|
try
|
|
{
|
|
System.out.print("Enter the numerator: ");
|
|
a = input.nextInt();
|
|
System.out.print("Enter the divisor: ");
|
|
b = input.nextInt();
|
|
int div = a/b;
|
|
int mod = a%b;
|
|
System.out.println(a+" / "+b+" is "+div+" with a remainder of "+mod);
|
|
}
|
|
catch(InputMismatchException ime)
|
|
{
|
|
System.out.println("Invalid input type");
|
|
}
|
|
catch(ArithmeticException ae)
|
|
{
|
|
System.out.println("You can't divide "+a+" by "+b);
|
|
}
|
|
|
|
}
|
|
|
|
public static void main( String[] args )
|
|
{
|
|
// Test it here in main, when ready
|
|
MathProblems functions = new MathProblems();
|
|
functions.divide();
|
|
}
|
|
}
|
|
|