Question

Write a program that prompts the user for two inputs: a divisor and a dividend that...

Write a program that prompts the user for two inputs: a divisor and a dividend that can handle the exceptions of input failure (non-integer input for both dividend and divisor) and dividing by 0 (divisor only).

Use try-catch statements and throw statements.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package card;

import java.util.InputMismatchException;

import java.util.Scanner;

public class Divide {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

try {

// gets input divident from keyboard

System.out.println("Enter divident: ");

int divident=sc.nextInt();

sc.nextLine();

// gets input divisor from keyboard

System.out.println("Enter divisor: ");

int divisor=sc.nextInt();

sc.nextLine();

//prints the integer part of divident/ divisor

System.out.println(divident+"/"+divisor+" = "+(divident/divisor));

// handles exception if any non integer value is entered

}catch(InputMismatchException e) {

System.out.println("Only Integer Input Allowed");

// handles divide by zero exception

}catch(ArithmeticException e) {

System.out.println("Divisor should not be 0");

}

}

}

Add a comment
Know the answer?
Add Answer to:
Write a program that prompts the user for two inputs: a divisor and a dividend that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program which: - prompts the user for 2 numerical inputs - stores their two...

    Write a program which: - prompts the user for 2 numerical inputs - stores their two inputs in variables as floats - Use a while in loop to ask a user for a valid operation: (if the user's input operation isn't one of those listed above, instead print a message telling them so, and continue asking for the valid operation) - prompts the user for an arithmetic operation ( + - * / %) - stores the user's input as...

  • Write a program that prompts the user to enter a person's birth date in numeric form...

    Write a program that prompts the user to enter a person's birth date in numeric form (e.g. 8-27-1980) and outputs the date of birth in the format of month day, year (e.g. August 27, 1980). Your program must work for three types of exceptions - invalid day, invalid month and invalid year (year must be between 1915 and 2015). Make these as class driven exceptions. Use one try-catch block with separate catches for the exception classes to handle errors for...

  • In Java - Write a program that prompts the user to enter two integers and displays...

    In Java - Write a program that prompts the user to enter two integers and displays their sum. If the input is incorrect, prompt the user again. This means that you will have a try-catch inside a loop.

  • c++ Write a program that prompts the user to enter a length in feet and then...

    c++ Write a program that prompts the user to enter a length in feet and then enter a length in inches and outputs the equivalent length in centimeters. If the user enters a negative number or a non-digit number, throw and handle an appropriate exception and prompt the user to enter another set of numbers. Your error message should read A non positive number is entered and allow the user to try again.

  • Write program that prompts the user to enter a person's date of birth in numeric form...

    Write program that prompts the user to enter a person's date of birth in numeric form such as 8-27-1980. The program then outputs the date of birth in the form: August 27, 1980. Your program must contain at least two exception classes: invalidDay and invalidMonth. If the user enters an invalid value for day, then the program should throw and catch an invalidDay object. Similar conventions for the invalid values of month and year. (Note that your program must handle...

  • Wrote a program called ExceptionalDivide that asks the user for 2 integer values and displays the...

    Wrote a program called ExceptionalDivide that asks the user for 2 integer values and displays the quotient of the first value divided by the second value. Make sure the your program reads in the two input values as ints. Your program must catch either of two exceptions that might arise and crash your program java.util.InputMismatchException //wrong data type inputted java.lang.ArithmeticException //divide by zero error Once you catch these exceptions, your program must print out a message explaining what happened and...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • In C++, Write a program that it prompts to user for two integers, computes the quotient...

    In C++, Write a program that it prompts to user for two integers, computes the quotient of two integers, and then displays the result. You need to do the following things in this assignment: + The main function prompts for user input and reads the two integers that the user entered. + Write a quotient function that computes the quotient of two integers and return the results. (hint: avoid integer division and divide by zero)

  • Using SPIM, write and test a program that finds the Greatest Common Divisor of two integers...

    Using SPIM, write and test a program that finds the Greatest Common Divisor of two integers using a recursive function that implements Euclid's GCD algorithm as described below. Your program should greet the user "Euclid's GCD algorithm", prompt the user to input two integers, and then output the result "Euclid's Greatest Common Divisor Algorithm" GCD(M,N) = M                      (if N is 0) GCD(M,N) = GCD(N, M % N)   (if N > 0) you may assume that inputs are non-negative name your assembly...

  • Write a program that prompts the user to enter a length in feet and inches and...

    Write a program that prompts the user to enter a length in feet and inches and outputs the equivalent length in centimeters. If the user enters a negative number or a nondigit number, throw and handle an appropriate exception and prompt the user to enter another set of numbers. Using Python

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT