Java Code:
Console IO Practice Exercise
The purpose of this exercise is to practice console input and output with the Java console.
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.
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:
public static int compare(int first, int second)
Your method should return the following values:
Return 0, when “first” and “second” are equal
Return -1, when “first” is less than “second”
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 console
Part C:
Create a static method called “max”. Your method should do the following:
Accept three arguments of type int.
Your method should print the largest of the three integers to the Java console.
Inside your main method, read in three integers from the Java console and pass them to your “max” method
Challenge:
Create a static method called “sum”. Your method should do the following:
Accept no arguments
Request an integer n from the Java console.
Use the value n to request n double values from the user (use a loop).
Sum each of the double values received and return the result.
Inside your main method, call your “sum” method and print the results to the Java console.
import java.util.Scanner;
//Class Console Practice definition
public class ConsolePractice
{
//Scanner class object created
private static Scanner scanner = new
Scanner(System.in);
//Main method definition
public static void main(String [] ss)
{
//Declaration of variables for user
input
int first, second, third,
result;
double res;
System.out.println("Enter two
numbers to show quotient and reminder: ");
//Accept two integer value from
console
first = scanner.nextInt();
second = scanner.nextInt();
//Calls method divide to display
quotient and remainder
divide(first, second);
System.out.println("Enter two
numbers to Compare: ");
//Accept two integer value from
console
first = scanner.nextInt();
second = scanner.nextInt();
//Calls method compare to display
comparison result
result = compare(first,
second);
System.out.println("Comparison
result = " + result);
System.out.println("Enter three
numbers to find biggest: ");
//Accept three integer value from
console
first = scanner.nextInt();
second = scanner.nextInt();
third = scanner.nextInt();
//Calls method max to display
largest number out of 3 numbers
max(first, second, third);
//Call the method sum to display
the total of n double numbers
res = sum();
System.out.println("Total of double
numbers = " + res);
}//End of method main
//Method divide to display quotient and
remainder
public static void divide(int numerator, int
denominator)
{
System.out.println("Quotient: " +
numerator / denominator);
System.out.println("Remainder: " +
numerator % denominator);
}//End of method divide
//Method compare to return the comparison result of
two numbers
public static int compare(int first, int second)
{
//Checks and returns 0 if both the
numbers are equal
if(first == second)
return 0;
//Checks and returns -1 if first
number is less than the second number
else if(first < second)
return -1;
//Returns 1 if first number is
greater than the second number
else
return 1;
}//End of method compare
//Method max to display the largest number out of 3
numbers
public static void max(int first, int second, int
third)
{
//Checks if first number is greater
than the second and third then first is the largest.
if(first > second &&
first > third)
System.out.println("Largest Number = " + first);
//Checks if second number is
greater than the third then second is the largest.
else if(second > third)
System.out.println("Largest Number = " + second);
//Otherwise third is the
largest
else
System.out.println("Largest Number = " + third);
}//End of method max
//Method sum returns the total of n double
numbers
public static double sum()
{
//variable n for how many
numbers
int n;
//variable total is initially
zero
double total = 0;
System.out.println("Enter how many
numbers you want to add?");
//Accepts how many numbers user
want
n = scanner.nextInt();
System.out.println("Enter " + n + "
Numbers: ");
//Accepts n double numbers and
calculates total
for(int c = 0; c < n; c++)
{
total +=
scanner.nextDouble();
}
//Returns the total of n double
numbers
return total;
}//End of method sum
}//End of class
Output:
Enter two numbers to show quotient and reminder:
20
3
Quotient: 6
Remainder: 2
Enter two numbers to Compare:
22
3
Comparison result = 1
Enter three numbers to find biggest:
12
56
5
Largest Number = 56
Enter how many numbers you want to add?
3
Enter 3 Numbers:
10.21
33.21
2.4
Total of double numbers = 45.82
Get Answers For Free
Most questions answered within 1 hours.