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

Let\'s first write a program so that it uses basic (Scanner-based) input, and ba

ID: 3880852 • Letter: L

Question

Let's first write a program so that it uses basic (Scanner-based) input, and basic output.

Create a new Eclipse project named CPS_150_Lab4_1

Add a Main class (with a main method).

Type (or copy/paste) in the highlighted code and then complete and run the following program:

import java.util.Scanner;

/*
    Complete the following program that prompts the user to enter two integers,
    then outputs which integer is smaller, and which integer is larger.
*/

public class Main {

   public static void main(String[] args) {

      Scanner kbd = new Scanner( System.in );

      // TODO: declare input1 and input2 as integers, each initialized to 0


      // TODO: declare minInput and maxInput as integers, each initialized to 0


      // TODO: prompt the user for the first input value


      // TODO: get the value of input1 as user input, using the kbd Scanner


      // TODO: prompt the user for the second input value


      // TODO: get the value of input2 as user input, using the kbd Scanner


      // set minInput to the smaller of input1 and input2
      minInput = Math.min(input1, input2);

      // set maxInput to the larger of input1 and input2
      maxInput = Math.max(input1, input2);
   
    // TODO: output the smaller integer


      // TODO: output the larger integer


   } // end main method

} // end Main class

Then. test the program using the inputs:

Now, let's write the program that implements input using more graphical dialog boxes.

Create a new Eclipse project named CPS150_Lab4_2

Add a Main class (with a main method).

Type (or copy/paste) in the highlighted code and then complete and run the following program:


Then. test the program using the inputs:

Explanation / Answer

4.1: The code is as follows:

mport java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner kbd = new Scanner( System.in );

// declare input1 and input2 as integers, each initialized to 0

int input1=0;

int input2=0;

// declare minInput and maxInput as integers, each initialized to 0

int maxInput = 0;

int minInput = 0;

// prompt the user for the first input value

System.out.println("Enter first input value: ");

// get the value of input1 as user input, using the kbd Scanner

input1 = kbd.nextInt();

// prompt the user for the second input value

System.out.println("Enter second input value: ");

// get the value of input2 as user input, using the kbd Scanner

input2 = kbd.nextInt();

// set minInput to the smaller of input1 and input2

minInput = Math.min(input1, input2);

// set maxInput to the larger of input1 and input2

maxInput = Math.max(input1, input2);

// output the smaller integer

System.out.println("Smaller integer = "+minInput);

// output the larger integer

System.out.println("Larger integer = "+maxInput);

} // end main method

} // end Main class

4.2)

import javax.swing.JOptionPane;

public class Main {

public static void main(String[] args) {

String Input;

// declare input1 and input2 as integers, each initialized to 0

int input1=0;

int input2=0;

// declare minInput and maxInput as integers, each initialized to 0

int minInput=0;

int maxInput=0;

// get the value of input1 as user input, using an input dialog

Input = JOptionPane.showInputDialog("Enter the first integer");

input1 = Integer.parseInt(Input);

// get the value of input2 as user input, using an input dialog

Input = JOptionPane.showInputDialog("Enter the second integer");

input2 = Integer.parseInt(Input);

// set minInput to the smaller of input1 and input2

minInput = Math.min(input1, input2);

// set maxInput to the larger of input1 and input2

maxInput = Math.max(input1, input2);

// output the smaller integer using a message dialog

JOptionPane.showMessageDialog(null,"smaller number: " + minInput);

// output the larger integer using a message dialog

JOptionPane.showMessageDialog(null,"greater number: " + maxInput);

} // end main method

} // end Main class