Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Console IO Practice Exercise The purpose of this exercise is to practice console

ID: 3782021 • Letter: C

Question

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.

Explanation / Answer

import java.util.*;
import java.lang.*;
import java.io.*;

public class ConsolePractice
{
   private static Scanner scanner = new Scanner(System.in);
   public static void main (String[] args)
   {
       System.out.println("Enter two integers as numerator and denominator for division");
       int num = scanner.nextInt();     //input numerator and denominator
       int den = scanner.nextInt();
       divide(num,den);                //call to divide method
      
       System.out.println("Enter two integers to compare");
       int first = scanner.nextInt();                 //input two integers
       int second = scanner.nextInt();
       int result =compare(first,second);          //call to compare method passing integers
       System.out.println("Result "+result);
      
      
       System.out.println("Enter three integers to find the greatest");
       int a = scanner.nextInt();     //input integers a,b and c
       int b = scanner.nextInt();
       int c = scanner.nextInt();
       max(a,b,c);    //call to max() method to find the maximum of three numbers
      
       double sumNumbers = sum();    //call to sum method
       System.out.println("Sum = "+sumNumbers);
   }
   public static void divide(int num,int den)
   {
       int quo = num / den;
       int rem = num % den;
       System.out.println("Numerator = "+ num + " Denominator = "+ den+ " Quotient = "+ quo + " Remainder = "+rem);
      
   }
  
   public static int compare(int first, int second)
   {
       if(first == second)
       return 0;
       else if(first < second)
       return -1;
       else if(first > second)
       return 1;
       else
       return 2;
   }
   public static void max(int a,int b,int c)
   {
       int largest = (a>b)?((a>c)?a:c):((b>c)?b:c);
       System.out.println("largest = "+largest);
   }
   public static double sum()
   {
       System.out.println("Enter the number of values to sum");
       int n = scanner.nextInt();
       double value,sum;
       sum =0;
       System.out.println("Enter "+n + " values");
       for(int i=1;i<=n;i++)
       {
           value = scanner.nextDouble();          //input n double values
           sum = sum +value;
       }
       return sum;
   }
}

output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote