Setup: Create a class called ConsolePractice with a main method. Create a static
ID: 3781966 • Letter: S
Question
Setup: Create a class called ConsolePractice with a main method. Create a static field in your class that stores a scanner object. You will use this scanner for all user input. o private static Scanner scanner new Scanner(System.in. Part A: Create a static method called "divide" Your method should do the following: Accept two integers as parameters, a numerator and denominator Compute the quotient and remainder of dividing the numerator by the denominator. Print the quotient and remainder to the Java console. Inside your main method, request two integers from the Java console and call your "divide' method. Part B: Create a static method called "compare'. Your method should do the following: Accept two arguments of type int. Use the following method header: o public static int comparelint first, int second) Your method should return the following values: o Return 0, when "first" and "second" are equal o Return -1, when "first" is less than "second" o Return 1, when "first is greater than "second" Inside your main method, request two integers from the Java console and pass them to your compare" method. Store the result of your method call and print it to the Java consoleExplanation / Answer
ConsolePractice.java
import java.util.Scanner;
public class ConsolePractice {
//Static scanner field
private static Scanner scanner = new Scanner(System.in);
//Static sum field to store sum of numbers
private static int sum;
/**
* Static divide method
* @param numerator
* @param denominator
*
* Prints the quotient and remainder on the Java Console
*/
static void divide(int numerator, int denominator){
if(denominator==0){
throw new ArithmeticException("Division Not Allowed By 0");
}
int quotient = numerator/denominator;
int remainder = numerator%denominator;
System.out.println("Quotient = "+quotient+" Remainder = "+remainder);
}
/**
* Static Compare Method
* @param num1
* @param num2
* @return 0 when num1=num2, 1 when num1>num2, -1 when num1<num2
*/
static int compare(int num1, int num2){
if(num1>num2){
return 1;
}
else if(num1<num2){
return -1;
}
else{
return 0;
}
}
/**
* Static Max method
* @param num1
* @param num2
* @param num3
* @return the maximum of the 3 parameters
*/
static int max(int num1, int num2, int num3){
int max = num1;
if(num2>max){
max=num2;
}
if(num3>max){
max=num3;
}
return max;
}
/**
* Static sum method
* @return returns the sum of the numbers input by user
*/
static int sum(){
//Input number
int number = scanner.nextInt();
//Add number to sum
sum+=number;
//Return sum
return sum;
}
/**
* Main method
* @param args
*/
public static void main(String args[]){
//For Division
System.out.println("Input two numbers for division");
int numerator = scanner.nextInt();
int denominator = scanner.nextInt();
divide(numerator,denominator);
//For Comparison
System.out.println("Input two numbers for comparison.");
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
int result = compare(num1, num2);
System.out.println("Result: "+result);
//For Finding Max
System.out.println("Input 3 numbers for finding maximum among them.");
int maxNum1 = scanner.nextInt();
int maxNum2= scanner.nextInt();
int maxNum3= scanner.nextInt();
System.out.println("Maximum Number = "+max(maxNum1, maxNum2, maxNum3));
//For finding Sum
System.out.println("Enter total numbers to be summed");
int n = scanner.nextInt();
System.out.println("Enter "+n+" numbers");
int sumResult = 0;
for(int i=0;i<n;i++){
sumResult = sum();
}
System.out.println("Total Sum = "+sumResult);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.