warm-up: monday, march 3 list all devices you can think of that can be used to input information...

36
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

Upload: anne-barber

Post on 25-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

• List all devices you can think of that can be used to input information into the computer

Page 2: 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

Page 3: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

INPUT

Pre-AP Computer ScienceCycle 5

Page 4: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

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

Page 5: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

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()

Page 6: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

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

Page 7: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

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);

}

}

Page 8: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

Sample Run: Example A

Enter two integers separated by spaces.

23 7

feet = 23

inches = 7

Java Programming: Guided Learning with Early Objects 8

Page 9: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

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);

}

}

Page 10: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

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

Page 11: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

Java Programming: Guided Learning with Early Objects 11

Page 12: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

Warm-Up

• What is the error in the following input statement?

double miles = console.nextInt( );

Java Programming: Guided Learning with Early Objects 12

Page 13: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

Chapter 0-1 Exam Statistics

Java Programming: Guided Learning with Early Objects 13

Page 14: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

Chapter 0-1 Exam Statistics

Java Programming: Guided Learning with Early Objects 14

Page 15: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

Chapter 0-1 Exam Statistics

15

Page 16: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

Dialog Output Boxes

Chapter 2

Page 17: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

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

Page 18: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

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

Page 19: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

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

Page 20: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

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

Page 21: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

Java Programming: Guided Learning with Early Objects 21

Figure 2-1 Input dialog box prompting the user to input name

Page 22: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

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

Page 23: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

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

Page 24: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

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

Page 25: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

Types of Messages

25

Page 26: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

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

Page 27: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

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

Page 28: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

Dialog Boxes - Input

Page 29: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

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

Page 30: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

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

Page 31: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

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

Page 32: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

Example

String str;

str = JOptionPane.showInputDialog(“Enter your name and press OK”);

Java Programming: Guided Learning with Early Objects 32

Page 33: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

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

Page 34: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

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

Page 35: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

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

Page 36: Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer

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