User Input and Pseudo-Code February 2, 2015 1 User Input Programs interact with the user–they get input from the user. The user may provide input via keyboard, mouse or via touch screen. In this course, we will learn how to read input from the user via keyboard. Consider the following simple task. We want to write a program that prompts the user to enter an integer, stores the value entered in a variable, and then outputs the value typed by the user. Roughly, the program should be as follows. public class Input { public static void main(String[] args) { System.out.println("Please enter an integer"); int number = Whatever user types; System.out.println("You typed " + number); } } The second line of the main method is attempting to store the value typed by the user in the variable number. We will now learn how to do this in Java. Every program that attempts to read input, must do the following: • import java.util.Scanner • Declare a Scanner Variable • Use Scanner methods to read input Importing is done by adding the following at the beginning of the program. import java.util.Scanner; Often we write import java.util.*; Declaring a Scanner variable is done by Scanner stdin = new Scanner(System.in); We are declaring a variable named stdin whose type is Scanner, and System.in refers to the keyboard. Now, we can use the variable stdin to read input from the user via keyboard. To read integers, we use the method nextInt() and to read doubles we use the method nextDouble() Using these, we arrive at the following program: 1 import java.uti..*; public class Input { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); System.out.println("Please enter an integer"); int number = stdin.nextInt(); System.out.println("You typed " + number); } } 2 Pseudo Code Now, we are ready to write non-trivial programs. Let us write a program that gets temperature in Fahrenheit as input from the user and converse it into Centigrade equivalent. Program design consist of the following steps. • Problem Solving Strategy • Write pseudo-code • Convert pseudo-code into Java program • Develop Test-cases, and run the program on test-cases. • If the program is incorrect on test-cases, then fix and run again To arrive at the problem solving strategy, think the following loudly: “How would I solve this problem?” In this case, suppose someone gives us the temperature in Fahrenheit, how do we convert this into Centigrade? We know that this is done by applying the following formula C = 9/5 ∗ (F − 32). Now let us write the pseudo-code. Psudo-code is program in English. Below is pseudo-code for our problem. • Prompt the user to enter temperature in Fahrenheit • Read the value user types into a variable named tempF • Apply the formula 9/5 ∗ (tempF − 32) to compute temperature in centigrade and store the value in a variable named tempC • Output the value of the variable tempC 2 Now we are ready to convert the above psudo-code into a Java program. First, we have to choose a name for the program. Recall that the program (and variable) names should be descriptive. Let us choose the name TempConversion. Since our program deals with user input, it must import java.util.* and declare a scanner variable. It is always a good programming practice to describe the functionality of the program (as comments) at the beginning. We arrive at the following program skeleton: /* * A program that gets temperature in Fahrenheit as input from the user and outputs equivalent temperature in Centigrade. */ import java.util.*; public class TempConversion{ public static void main(String[] args) { Scanner stdin = new Scanner(System.in); } } Now, we look at each lien of the pseudo-code and convert into Java statements. Once we do this, we arrive at the following Program. /* * A program that gets temperature in Fahrenheit as input from the user and outputs equivalent * temperature in Centigrade. */ import java.util.*; public class TempConversion{ public static void main(String[] args) { Scanner stdin = new Scanner(System.in); System.out.println("Please enter temperature in Fahrenheit"); double tempF = stdin.nextDouble(); double tempC = 5.0/9*(tempF-32); System.out.println(tempF + " Fahrenheit is equivalent to " + tempC + " Centigrade"); } } Below are a few test-cases. • 0 • 212 • 32 • -40 • 48.5 • -70.6 3