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
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...
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...
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...
is there anything wrong with the solution. the question are from java course Write a main...
is there anything wrong with the solution. the question are from java course Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”.       Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit. Answer: import javax.swing.*; public class IsAllLetters {     public static void main(String[] args) {         String input;         int count =...
IN JAVA Complete the following program. Add codes in the main method to:1) call method average(double[]...
IN JAVA Complete the following program. Add codes in the main method to:1) call method average(double[] gpaarr ) to calculate the average of gpaArray; 2) add codes in the for loop to calculate sum 3) print the average . public class Test { public static void main (String args[]) { double[ ] gpaArray = { 2.5, 4.0, 3.8, 3.2, 2.9, 4.0 } ;   //add your codes here (you can put your codes in the Answer area with/without copying the original...
Download the ProductUpTo3.java file, and open it in jGrasp (or a text editor of your choice)....
Download the ProductUpTo3.java file, and open it in jGrasp (or a text editor of your choice). This program will read in three integers with Scanner, put the values in an array of int, and then print the product of the three values. Example output of the program is shown below, with user input shown in bold: Enter first integer: 3 Enter second integer: 4 Enter third integer: 5 Product: 60 More details about the method you need to write are...
Questions: 1. (5 marks) Create a VB.NET Console Application that defines a function Smallest and calls...
Questions: 1. Create a VB.NET Console Application that defines a function Smallest and calls this function from the main program. The function Smallest takes three parameters, all of the Integer data type, and returns the value of the smallest among the three parameters. The main program should (1) Prompt a message (using Console.WriteLine) to ask the user to input three integers. (2) Call the built-in function Console.ReadLine() three times to get the user’s input. (3) Convert the user’s input from...
IN JAVA!! You may be working with a programming language that has arrays, but not nodes....
IN JAVA!! You may be working with a programming language that has arrays, but not nodes. In this case you will need to save your BST in a two dimensional array. In this lab you will write a program to create a BST modelled as a two-dimensional array. The output from your program will be a two-dimensional array.   THEN: practice creating another array-based BST using integers of your choice. Once you have figured out your algorithm you will be able...
I need this before the end of the day please :) In Java 10.13 Lab 10...
I need this before the end of the day please :) In Java 10.13 Lab 10 Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you...
Write the following problem in Java Create a class Dog, add name, breed, age, color as...
Write the following problem in Java Create a class Dog, add name, breed, age, color as the attributes. You need to choose the right types for those attributes. Create a constructor that requires no arguments. In this constructor, initialize name, breed, age, and color as you wish. Define a getter and a setter for each attribute. Define a method toString to return a String type, the returned string should have this information: “Hi, my name is Lucky. I am a...