Module-2.pdf
Document Details
Uploaded by Deleted User
Full Transcript
Study Guide in FM-AA-CIA-15 Rev. 0 03-June-2020 OOP 101 – Object-Oriented Programming Module 2: Getting Input from the Keyboard Module No. 2...
Study Guide in FM-AA-CIA-15 Rev. 0 03-June-2020 OOP 101 – Object-Oriented Programming Module 2: Getting Input from the Keyboard Module No. 2 MODULE TITLE GETTING INPUT FROM THE KEYBOARD MODULE OVERVIEW Now that we've studied some basic concepts in Java and we've written some simple programs, let's make our programs more interactive by getting some input from the user. In this lesson, we'll be discussing two methods of getting input, the first one is by using the Scanner class, and the other one involves a graphical user interface by using JOptionPane. LEARNING OBJECTIVES At the end of the lesson, the student should be able to: Create an interactive Java program that gets input from the keyboard Use the Scanner class to read data from the keyboard using a console Use the JOptionPane class to get input from the keyboard using a graphical user interface LEARNING CONTENTS Using the Scanner Class to get input To put data into variables from the standard input device, Java provides the class Scanner. Using this class, first we create an input stream object and associate it with the standard input device. The following statement accomplishes this: Scanner inputDevice = new Scanner(System.in) The portion of the statement to the left of the assignment operator, Scanner inputDevice, declares an object of type Scanner with the programmer-chosen inputDevice. The portion of the statement to the right of the assignment operator, new Scanner(System.in), creates a Scanner object that is connected to the System.in property. In other words, the created Scanner object is connected to the default input device. Given the following code, import java.util.Scanner; public class GetUserInfo { public static void main( String[] args ) { String name; int age; Scanner inputDevice = new Scanner(System.in); System.out.print(“Please enter your name “); PANGASINAN STATE UNIVERSITY 3 Study Guide in FM-AA-CIA-15 Rev. 0 03-June-2020 OOP 101 – Object-Oriented Programming Module 2: Getting Input from the Keyboard name = inputDevice.nextLine(); System.out.print(“Please enter your age “); age = inputDevice.nextInt(); System.out.println(“Your name is “ + name + “ and you are “ + age +“ years old.”); } } The first statement, import java.util.Scanner; indicates that we want to import the class Scanner from the java.util package. This statement imports the package necessary to use the Scanner class. The statement, Scanner inputDevice = new Scanner(System.in); Declares a Scanner object named inputDevice. The name = inputDevice.nextLine()statement uses the nextLine()method to retrieve a line of text from the keyboard and store it in the name variable. The age = inputDevice.nextInt()statement uses the nextInt()method to retrieve an integer from the keyboard and store it in the age variable. Using JOptionPane to get input Another way to get input from the user is by using the JOptionPane class which is found in the javax.swing package. JOptionPane makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something. Given the following code, import javax.swing.JOptionPane; public class Welcome{ public static void main( String args[] ){ String name; name=JOptionPane.showInputDialog("Enter your name"); JOptionPane.showMessageDialog( null, "Hello "+name + "\nWelcome to Java Programming!" ); System.exit(0); } } PANGASINAN STATE UNIVERSITY 4 Study Guide in FM-AA-CIA-15 Rev. 0 03-June-2020 OOP 101 – Object-Oriented Programming Module 2: Getting Input from the Keyboard The first line, import javax.swing.JOptionPane; indicates that we want to import the class JOptionPane from the swing package inside the external library javax. The next line, public class Welcome{ indicates the name of the class which is Welcome. The statement, name=JOptionPane.showInputDialog("Enter your name"); creates a JOptionPane input dialog, which will display a dialog box with a message, a textfield with OK and Cancel button as shown in the figure 1. This returns a String which we will save in the name variable. The next statement, JOptionPane.showMessageDialog( null, "Hello "+name + "\nWelcome to Java Programming!" ); displays a dialog box which contains a message and an OK button as shown in figure 2. The output of the above program is shown below, Figure 1: Getting Input Using JOptionPane Figure 2: Showing Message Using JOptionPane PANGASINAN STATE UNIVERSITY 5 Study Guide in FM-AA-CIA-15 Rev. 0 03-June-2020 OOP 101 – Object-Oriented Programming Module 2: Getting Input from the Keyboard LEARNING POINTS The Scanner class is used to get user input, and is found in the java.util package. To use the Scanner class, create an object of the class and use any of the variable methods found in the Scanner class documentation. The JOptionPane class is used to provide standard dialog boxes such as message dialog box, confirm dialog box and input dialog box. These dialog boxes are used to display information or get input from the user. The JOptionPane class inherits JComponent class. LEARNING ACTIVITIES Hands-on Activity BufferedReader/Scanner version 1. Write a program that would input an integer number and then extract its one’s digit or right most digit. Scanner version 2. Write a program that would input four numbers for variables W, X, Y, and Z, respectively, and exchange their values such that X goes into W, Y into X, Z into Y and W into Z. JOptionPane version 1) Workers at a particular company were given a 17.75% salary increase. Moreover, the increase was retroactive for 2 months, that is, effective 2 months ago. Write a program that takes the employee’s old salary as input and then output the amount of retroactive pay (balance) due the employee and his new salary as well. JOptionPane version 2) XYZ’s Pizza Parlor charges 12% service charge and 7% sales tax on the gross bill of its customer. Make a program that would input the gross bill of the customer and the amount given by the customer to the waiter. Program must output the customer’s net bill and his change. REFERENCES 1. Farrel, Joyce, “Java Programming 9ed”, Course Technology, Cengage Learning 2. Java Education & Development Initiative Electronic Teachers Guide”, University of the Philippines Java Research and Development Center, UP-Diliman, Quezon City. PANGASINAN STATE UNIVERSITY 6