Access over 20 million homework & study documents
search

Complete a function that does the following Prompts the user to inp

Content type

User Generated

Rating

Showing Page:
1/4
Complete a function that does the following: Prompts the
user to input both a numerator and denominator. Uses a
try/catch block, that catches the following issues: A
denominator of 0 will throw an exception. A negative value
of the division of the two terms will throw an exception The
catch block will then write an error message and tell the
user as such. Repeat this prompt until valid data inputted.
Returns the positive divided result of the numerator and
denominator. double UnsignedDivideFunction() {
Solution
Please follow the code and comments for description :
CODE :
import java.io.*;
import java.util.*;
public class Testtrycatch {
public static void main(String args[]) {
double a, b;
do {
Scanner sc = new Scanner(System.in);
System.out.println(\"Please enter the numerator :
\");
a = sc.nextInt();
System.out.println(\"Please enter the denominator

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/4
: \");
b = sc.nextInt();
UnsignedDivideFunction(a, b);
} while ((a <= 0 && b <= 0) || (a <= 0 && b >= 0) || (a
>= 0 && b <= 0));
}
static double UnsignedDivideFunction(double x, double
y) {
double result;
try {
result = x / y;
// Catch arithmetic exception
} catch (ArithmeticException e) {
System.out.println(\"Error is : \" + e.toString() + \"
caught\");
}
try {
if (x < 0 || y < 0) {
throw new NegativeNumberException(\"Input
must be a positive number\");
}
// Divide first by second and output result
System.out.println(\"Division = \" + x / y);
} // Catch block
catch (NegativeNumberException error) {

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/4

Sign up to view the full document!

lock_open Sign Up
End of Preview - Want to read all 4 pages?
Access Now

Unformatted Attachment Preview

Complete a function that does the following: Prompts the user to input both a numerator and denominator. Uses a try/catch block, that catches the following issues: A denominator of 0 will throw an exception. A negative value of the division of the two terms will throw an exception The catch block will then write an error message and tell the user as such. Repeat this prompt until valid data inputted. Returns the positive divided result of the numerator and denominator. double UnsignedDivideFunction( ) { Solution Please follow the code and comments for description : CODE : import java.io.*; import java.util.*; public class Testtrycatch { public static void main(String args[]) { double a, b; do { Scanner sc = new Scanner(System.in); System.out.println(\"Please enter the numerator : \"); a = sc.nextInt(); System.out.println(\"Please enter the denominator : \"); b = sc.nextInt(); UnsignedDivideFunction(a, b); } while ((a ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Anonymous
Super useful! Studypool never disappoints.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4