Hi! Can someone please help me with this JAVA problem? They are asking me; 1- To
ID: 3769823 • Letter: H
Question
Hi! Can someone please help me with this JAVA problem?
They are asking me; 1- To write the output of the following JAVA programs or segments (About exception handling and text IO) and 2- To write down what does EACH line mean.
The programs or segments are:
public class QuotientWithMethod {
public static int quotient(int number1, int number2) {
if (number2 == 0){
System.out.println("Divisor cannot be zero");
System.exit(1);
}
return number1 / number2;
}
}
======================================================================
public static void main(String[] args){
Scanner input = new Scanner(System.in);
//prompt the user to enter two integers
System.out.print("Enter two integers: ");
int number1 = input.nextInt();
int number2 = input.nextInt();
int result = quotient(number1, number2);
System.out.println(number1 + " / " + number2 + "is" + result);
}
}
I would really appreciate the help and will rate! Thanks!
Explanation / Answer
public class QuotientWithMethod {
//This method is to find quotient when number1 is divided by number 2.
public static int quotient(int number1, int number2) { //quotient is a static function means it can be used without creating an object.
if (number2 == 0){// If the denominator is 0 then we can't divide the two numbers. Ex:10/0
//So we output error message.
System.out.println("Divisor cannot be zero");
System.exit(1);
}
// if denominator is not 0 then return quotient
return number1 / number2;
}
public static void main(String[] args){// This is the main method from where Java execution starts.
Scanner input = new Scanner(System.in);// Create a scanner to read from System.in
//prompt the user to enter two integers
System.out.print("Enter two integers: ");
int number1 = input.nextInt();//read the next integer from scanner
int number2 = input.nextInt();// read another integer from scanner
int result = quotient(number1, number2);// call the above quotient method to find quotient when number1 is divided by number2.
System.out.println(number1 + " / " + number2 + "is" + result);//outputs quotient to console.
}
}
On a whole this program is to take two numbers from standard input and find quotient using quotient method and output it.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.