Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information...
-
Upload
anne-barber -
Category
Documents
-
view
212 -
download
0
Transcript of Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information...
Warm-Up: Monday, March 3
• List all devices you can think of that can be used to input information into the computer
Warm-Up: Monday, March 3
INPUT
Pre-AP Computer ScienceCycle 5
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 Objects 4
Java Programming: Guided Learning with Early Objects 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()
Java Programming: Guided Learning with Early Objects 6
Variable Initialization
• Variable can be initialized with a literal valueint 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
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);
}
}
Sample Run: Example A
Enter two integers separated by spaces.
23 7
feet = 23
inches = 7
Java Programming: Guided Learning with Early Objects 8
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);
}
}
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
Java Programming: Guided Learning with Early Objects 11
Warm-Up
• What is the error in the following input statement?
double miles = console.nextInt( );
Java Programming: Guided Learning with Early Objects 12
Chapter 0-1 Exam Statistics
Java Programming: Guided Learning with Early Objects 13
Chapter 0-1 Exam Statistics
Java Programming: Guided Learning with Early Objects 14
Chapter 0-1 Exam Statistics
15
Dialog Output Boxes
Chapter 2
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
Review: Typical Input/Output Structureimport 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
Review: Typical Input/Output Structureimport 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
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 Objects 20
Java Programming: Guided Learning with Early Objects 21
Figure 2-1 Input dialog box prompting the user to input name
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 Objects 22
Example Codeimport 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
Example Codeimport 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
Types of Messages
25
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_MESSAGE WARNING_MESSAGE 26
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 Objects 27
Dialog Boxes - Input
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 Objects 29
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 Objects 30
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 Objects 31
Example
String str;
str = JOptionPane.showInputDialog(“Enter your name and press OK”);
Java Programming: Guided Learning with Early Objects 32
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 Objects 33
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 Objects 34
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 Objects 35
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 Objects 36