Question

Java Code: Console IO Practice Exercise The purpose of this exercise is to practice console input...

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.

Homework Answers

Answer #1

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

Know the answer?
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
Similar Questions
Java Programing Exercise #3: Write a Java class that implements a static method – SortNumbers(int… numbers)...
Java Programing Exercise #3: Write a Java class that implements a static method – SortNumbers(int… numbers) with variable number of arguments. The method should be called with different numbers of parameters and does arrange the numbers in descending order. Call the method within main method of the driver classand display the results.
In this Java programming assignment, you will practice using selection statements to determine whether a given...
In this Java programming assignment, you will practice using selection statements to determine whether a given year in the past or future qualifies as a “Leap Year”. I. Design a class called LeapYear in a file called LeapYear.java. This class will hold the main method and the class method that we will write in this assignment. II. Write an empty public static void main(String[] args) method. This method should appear inside the curly braces of the LeapYear class. III. Write...
Code in Java Create a file called Factorial.java. This file should have the following method: public...
Code in Java Create a file called Factorial.java. This file should have the following method: public static long calculate(long n) Factorial.calculate should recursively calculate n!, where 0! = 1 and n! = n(n−1)!. The method should also print out an error and exit if n < 0 or n > 20, since factorial is not defined for negative numbers and will overflow Java’s long variable with larger numbers (if we used an int, it would overflow even sooner!). Inside Factorial.java,...
Here is the code I am supposed to Analyze: // If we want to use the...
Here is the code I am supposed to Analyze: // If we want to use the Scanner class (type) to get user input, we need // to import the following Java package that includes the Scanner class // definitions. We do not have to add an import statement to use the // print() or println() methods of the System object, because they // are built in. import java.util.Scanner; public class PRG420Week1_AnalyzeAssignment { /* The main() method you see below is...
Java Code import java.util.Scanner; /** Create two methods as instructed below and then call them appropriately....
Java Code import java.util.Scanner; /** Create two methods as instructed below and then call them appropriately. Hint: Only one of the methods you create needs to be called from the main method. */ public class LandCalculation { public static void main(String[] args) { final int FEET_PER_ACRE = 43560; // Number of feet per acre double tract = 0.0, acres = 0.0; Scanner keyboard = new Scanner(System.in); System.out.print("Enter the tract size: "); tract = keyboard.nextDouble(); // Validate the user's input. while(tract...
Complete the Java code. The code should print “x is in numbers” if the integer x...
Complete the Java code. The code should print “x is in numbers” if the integer x is one of the values stored in numbers. If x is not in numbers, your code should print “x is not in numbers” public static void main(String[] args){                         int[] numbers = <some array values>;                         int x = <some value>;             }
4) Write a java program where you will use while loop, where if condition is equal...
4) Write a java program where you will use while loop, where if condition is equal to 2 (or any other choice of your number) then it will break the while condition. Steps: 1) Declare variable int (any name) which is equal to zero 2) Declare while condition which variable int is less then equal to 10 3) Declare print out where the value of variable is the same name that you declared in step 1 4) Declare condition if...
5) Write a java program with scanner object to input first number and second number as...
5) Write a java program with scanner object to input first number and second number as variable with input statement and system out print statement. Steps: 1) Declare scanner object 2) Ask system out print for first number and declare variable first number 3) Ask system out print for second number and declare variable second number 4) Declare first for loop where variable int i has already a value (your choice), that i is less then equal to ? and...
IN JAVA In this problem, we will implement an nth root finder. Recall that the nth...
IN JAVA In this problem, we will implement an nth root finder. Recall that the nth root of x is the number when raised to the power n gives x. In particular, please fill in the method findNthRoot(int number, int n, int precision) within the Main class. The method should return a string representing the nth root of number, rounded to the nearest precision decimal places. If your answer is exact, you should still fill in the answer with decimal...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT