Question: Lab 4.1: Input Using a Scanner Let's first write a program so that it uses basic (Scanner-based) input, and basic output. Create a new Eclipse

Lab 4.1: Input Using a Scanner

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:

  1. 12 and 23, and
  2. 23 and 12.


Lab 4.2: Input Using an Input Dialog

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:

import javax.swing.JOptionPane; public class Main { public static void main(String[] args) { String Input; // TODO: declare input1 and input2 as integers, each initialized to 0 // TODO: declare minInput and maxInput as integers, each initialized to 0 // get the value of input1 as user input, using an input dialog Input = JOptionPane.showInputDialog("Enter the first integer"); input1 = Integer.parseInt(Input); // TODO: get the value of input2 as user input, using an input dialog (use the previous step as a guide) // 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("smaller number: " + minInput); // TODO: output the larger integer using a message dialog (use the previous step as a guide) } // end main method } // end Main class

Then. test the program using the inputs:

  1. 12 and 23, and
  2. 23 and 12.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!