Download presentation
Presentation is loading. Please wait.
Published byAnne Barber Modified over 9 years ago
1
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer
2
Warm-Up: Monday, March 3 Keyboard Microphone Scanner Digital Camera Flash/Hard Drive Mouse Joystick/Game Controller Tablet Video Capture Equipment Barcode Readers Touch Screen Fax Machine Buttons
3
INPUT Pre-AP Computer Science Cycle 5
4
Using Class Scanner Class Scanner is used to scan the computer for signals from input devices, and record inputs from these devices on your computer –Must import java.util.* library For our programming, the input device we’re most concerned with is the keyboard –Scan for key presses –Record key presses as numbers, letters, strings, etc Java Programming: Guided Learning with Early Objects4
5
5 Input (Read) Statement The Scanner class puts data into variables from the standard input device –static Scanner console = new Scanner(System.in); Next input is: –Integer: console.nextInt() –Double: console.nextDouble() –String: console.next() or console.nextLine()
6
Java Programming: Guided Learning with Early Objects6 Variable Initialization Variable can be initialized with a literal value int feet = 35; Variable can be initialized with an input statement int feet = console.nextInt(); Variables initialized with a literal value cannot be changed without editing the source code Variables initialized with an input statement are more flexible
7
Example A import java.util.*; public class Example_A { static Scanner console = new Scanner(System.in); public static void main(String[ ] args) { System.out.println(“Enter two integers” + “separated by spaces.”); int feet = console.nextInt(); int inches = console.nextInt(); System.out.println(“feet = “ + feet); System.out.println(“inches = “ + inches); }
8
Sample Run: Example A Enter two integers separated by spaces. 23 7 feet = 23 inches = 7 Java Programming: Guided Learning with Early Objects8
9
import java.util.*; public class Example_B { static Scanner console = new Scanner(System.in); public static void main(String[ ] args) { System.out.println(“Enter first name.”; String firstname = console.next(); System.out.println(“Enter last name.”; String lastname = console.next(); System.out.println(“Enter age.”); int age = console.nextInt(); System.out.println(“Enter weight.”); double weight = console.nextDouble(); System.out.println(“\nName: “ + firstname + “ “ + lastname); System.out.println(“Age: “ + age); System.out.println(“Weight: “ + weight); }
10
Sample Run: Example B Enter first name. Monica Enter last name. Barrera Enter age. 22 Enter weight. 126.7 Name: Monica Barrera Age: 22 Weight: 126.7 10
11
Java Programming: Guided Learning with Early Objects11
12
Warm-Up What is the error in the following input statement? double miles = console.nextInt( ); Java Programming: Guided Learning with Early Objects12
13
Chapter 0-1 Exam Statistics Java Programming: Guided Learning with Early Objects13
14
Chapter 0-1 Exam Statistics Java Programming: Guided Learning with Early Objects14
15
Chapter 0-1 Exam Statistics Period 1 –Average: 85 –Range: 71 – 99 Period 2 –Average: 74 –Range: 38 – 98.5 Combined –Average: 79 –Std Dev: 14.6 Top 10% >= 94.5 Top 25% >= 89.5 15
16
Dialog Output Boxes Chapter 2
17
Input/Output Review Used class System.out to output –System.out.print( ) –System.out.println( ) Used class Scanner to use the keyboard to provide input –Had to import the java.util.* packages –Create a new Scanner to check for keypresses static Scanner console = new Scanner(System.in ) –Used the Scanner to accept input console.nextInt( )//for integers console.nextDouble( )//for doubles console.next( )//for Strings 17
18
Review: Typical Input/Output Structure import java.util.* public class example { static Scanner console = new Scanner(System.in); public static void main(String[ ] args) { // prompt the user for what to input // accept the input and store it in a variable // process/manipulate the inputted information //output the answer back to the user } 18
19
Review: Typical Input/Output Structure import java.util.*; public class example { static Scanner console = new Scanner(System.in); public static void main(String[ ] args) { System.out.println(“Enter a number.”); int number = console.nextInt( ); int newNum = number * 3 + 6; System.out.println(“New number = “ + newNum); } 19
20
Using Dialog Boxes Besides printing to the console, you can also use a generated user interface (GUI) to communicate with the user –Output AND Input GUI’s make the program look more user- friendly, and less like a computer program Closely resembles every-day computer use Java Programming: Guided Learning with Early Objects20
21
Java Programming: Guided Learning with Early Objects21 Figure 2-1 Input dialog box prompting the user to input name
22
Creating Dialog Boxes Dialog boxes are contained in the library package javax.swing.*, so it must be imported at the beginning of every code To create an output dialog box, use the following code: JOptionPane.showMessageDialog(null, “TEXT”, “TITLE”, JOptionPane.INFORMATION_MESSAGE); Java Programming: Guided Learning with Early Objects22
23
Example Code import javax.swing.*; import java.util.*; public class example { static Scanner console = new Scanner(System.in); public static void main(String[ ] args) { System.out.println(“Enter a message.”); String message = console.next( ); JOptionPane.showMessageDialog(null, message, “Your message”, JOptionPane.INFORMATION_MESSAGE); } 23
24
Example Code import javax.swing.*; import java.util.*; public class example { static Scanner console = new Scanner(System.in); public static void main(String[ ] args) { System.out.println(“Enter a message.”); String message = console.next( ); JOptionPane.showMessageDialog(null, message, “Your message”, JOptionPane.INFORMATION_MESSAGE); } 24
25
Types of Messages 25
26
Code Template – Dialog Boxes import javax.swing.*; import java.util.*; public class example { static Scanner console = new Scanner(System.in); public static void main(String[ ] args) { //write code here } JOptionPane.showMessageDialog(null, message, “Your message”, JOptionPane.PLAIN_MESSAGE); ERROR_MESSAGE QUESTION_MESSAGE INFORMATION_MESSAGE PLAIN_MESSAGEWARNING_MESSAGE 26
27
Warm-Up: Friday, March 7 What is a GUI? When using dialog boxes, why do we need to import the javax.swing.* package? When you are done, TURN IN YOUR WARMUPS Java Programming: Guided Learning with Early Objects27
28
Dialog Boxes - Input
29
Review Dialog boxes are pop-up boxes that can be used for input or output They are contained in the javax.swing.* package They belong to class JOptionPane Java Programming: Guided Learning with Early Objects29
30
Creating Dialog Output Boxes Dialog boxes are contained in the library package javax.swing.*, so it must be imported at the beginning of every code To create an output dialog box, use the following code: JOptionPane.showMessageDialog(null, “TEXT”, “TITLE”, JOptionPane.INFORMATION_MESSAGE); Java Programming: Guided Learning with Early Objects30
31
Creating Dialog Input Boxes The code for input boxes is much shorter Note: As it is input, you need to set a variable equal to the command in order to save what the user types in String str = JOptionPane.showInputDialog(“text”); Java Programming: Guided Learning with Early Objects31
32
Example String str; str = JOptionPane.showInputDialog(“Enter your name and press OK”); Java Programming: Guided Learning with Early Objects32
33
Dialog Input Returns Strings When using the dialog box for input, it always returns the input as a String, EVEN if what you’ve inputted is a number. That means, any number inputted into the dialog box must be converted to a number before you can use it to perform math Java Programming: Guided Learning with Early Objects33
34
Converting String to Int (Parsing) Integer.parseInt(stringName); int number = Integer.parseInt(“56”); 56 String number = “3678”; int Num = Integer.parseInt(number); 3678 Java Programming: Guided Learning with Early Objects34
35
Converting String to Float (Parsing) Float.parseFloat(stringName); float decimal = Float.parseFloat(“5.4”); 5.4 String number = “36.51”; float decimal = Float.parseFloat(number); 36.51 Java Programming: Guided Learning with Early Objects35
36
Review str = JOptionPane.showInputDialog(“Enter your name and press OK”); int Num = Integer.parseInt(number); float decimal = Float.parseFloat(number); Java Programming: Guided Learning with Early Objects36
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.