36
MIDTERM COVERAGE MIDTERM COVERAGE READING KEYBOARD OUTPUT

Reading Keyboard Output

Embed Size (px)

Citation preview

Page 1: Reading Keyboard Output

MIDTERM MIDTERM COVERAGECOVERAGE

READING KEYBOARD OUTPUT

Page 2: Reading Keyboard Output

SCANNER SCANNER CLASS(CLASS(REFRESHREFRESH))Ex. Scanner keyboard = new Scanner(System.in)

Declares a variable named keyboard

Creates a Scanner object in memory. Read input from System.in

Page 3: Reading Keyboard Output

SCANNER SCANNER CLASS(CLASS(MethodsMethods))STRINGBytesIntegersLong integersShort integersFloatDoubles

Page 4: Reading Keyboard Output

SCANNER SCANNER CLASS(CLASS(MethodsMethods))For example: int number; Scanner keyboard= new

Scanner(System.in);System.out.print(“Enter an integer value”); number= keyboard.nextInt();

Therefore, this statement formats the input that was entered @ the keyboard as an int.

Page 5: Reading Keyboard Output

SCANNER SCANNER CLASS(CLASS(MethodsMethods)) nextByte – returns input as Byte nextDouble – returns input as

Double. nextFloat – returns input as Float nextInt – returns input as an Int nextLine – return input as String. nextLong – return input as a long next Short – return input as a

short.

Page 6: Reading Keyboard Output

SCANNER SCANNER CLASS(CLASS(MethodsMethods))

EXAMPLE IMPLEMENTATION

Payroll.java

Page 7: Reading Keyboard Output

DIALOG BOXESDIALOG BOXESCONCEPT:

JOptionPane class allows a user to display a Dialog Box.

Dialog Box – is a small graphical window that displays a message to the user or request input. We can quickly display dialog boxes w/ JOptionPane class.

Page 8: Reading Keyboard Output

DIALOG BOXESDIALOG BOXESTypes of DIALOG BOXES

Message Dialog – a dialog box that displays a message; an OK button is also displayed.

Input Dialog – dialog box tat prompts the user for input & provides text field where input is typed; an OK button and a CANCEL button are also displayed.

Page 9: Reading Keyboard Output

DIALOG BOXESDIALOG BOXESBeginning Statement in your

code when using JoptionPane: import javax. swing.JOptionPane;

Purpose: this statement tells the compiler where to find the JOptionPane class, and make it available to your program.

Page 10: Reading Keyboard Output

Message DialogsMessage Dialogs showMessageDialog method- is used

to display a message dialog box.Statement to call the method: JOptionPane.showMessageDialog (null, “HELLO

WORLD”);

ARGUMENT PURPOSE: null – causes the dialog box to be displayed in the

center of the screen.HELLO WORLD – the message we want to display in

the dialog box.

Page 11: Reading Keyboard Output

INPUT DIALOGSINPUT DIALOGS showInputDialog method – to display

an input dialog in JOptionPane class.

Statement to call the method: String name; name = JOptionPane.showInputDialog(“Enter

your name.”);

Page 12: Reading Keyboard Output

Sample CodeSample Code import javax.swing.JOptionPane;

public class Names {

public static void main (String [ ] args)

{

firstName= JOptionPane.showInputDialog(“What’s your firstname”);

middleName=JOptionPane.showInputDialog(“What’s your middle name”);

lastName=JOptionPane.showInputDialog(“What’s your Last Name”);

JOptionPane.showMessageDialog(null, “HELLO” +firstName + “ “ + middleName + “ “ + lastName);

System.exit (0); This statement causes the program to end, & is

required if we use the JOptionPane class to display dialog

box.

} }

Page 13: Reading Keyboard Output

Disadvantage of JOptionPaneDisadvantage of JOptionPaneJOptionPane class does not have

different methods for reading values of different data types as input.

showInputDialog method always returns the user’s input as a String.

Problem when use in Math operation.Because we cannot perform math on

strings.In such case, you must convert the

input to a numeric value.

Page 14: Reading Keyboard Output

Methods for converting strings to Methods for converting strings to numbersnumbersByte.parseByte – method to convert string to

a byte.Double.parseDouble – method to convert

string to a double.Float.parseFloat – method to convert string to

a float.Integer.parseInt – method to convert string to

an int.Long.parseLong – method to convert string to

a long.Short.parseShort – method to convert string

to a short.

Page 15: Reading Keyboard Output

Sample UsageSample Usage int num;

String str;str=JOptionPane.showInputDialog(“Enter a number”);

num= Integer.parseInt(str);

num variable will hold the value entered by the user, converted to an int.

Page 16: Reading Keyboard Output

DECISION / DECISION / CONTROL FLOW CONTROL FLOW

STRUCTURESSTRUCTURES

Page 17: Reading Keyboard Output

IF STATEMENTIF STATEMENTCONCEPT:

> is used to create decision structures, which allow the program to have more than one path of execution> causes one or more statements to execute only when boolean expression is true.

NOTE: RELATIONAL OPERATIONS ARE USED

Page 18: Reading Keyboard Output

IF STATEMENT SYNTAXIF STATEMENT SYNTAX

If (BooleanExpression)statement;

BooleanExpression- appears inside the parentheses must be a boolean expression.

If the boolean expression is true, the next statement is executed.

Page 19: Reading Keyboard Output

Flow of If statementsFlow of If statements

Page 20: Reading Keyboard Output

SAMPLE USAGESAMPLE USAGEPROBLEM:

If the value is less than 32, displays the message “Invalid Number”

CODE: if ( value < 32)

System.out.println(“Invalid Number”);

Page 21: Reading Keyboard Output

If-else StatementIf-else StatementConcept:

> will execute one group of statements if its BOOLEAN EXPRESSION is True, or another group if its BOOLEAN EXPRESSION is False.

> expansion of the if statement.

Page 22: Reading Keyboard Output

If –else statement SYNTAXIf –else statement SYNTAX

if(BooleanExpression){

statement; } else {

statement; }

Page 23: Reading Keyboard Output

Logic of if-else StatementLogic of if-else Statement

Page 24: Reading Keyboard Output

NESTED IF STATEMENTNESTED IF STATEMENT

CONCEPT:

> To test more than one condition, an if statement can be nested inside another if statement.

Page 25: Reading Keyboard Output

Nested if statement Nested if statement SYNTAXSYNTAX

if (BooleanExpression)

{

if(BooleanExpression)

{

statement;

}

else

{

statement;

}

else

{

statement;

}

Page 26: Reading Keyboard Output

THE if-else-if StatementTHE if-else-if StatementCONCEPT: if-else-if statement test a series of conditions than with a set of nested if-else statement.

else-if statement is in between the if & else statement.

Page 27: Reading Keyboard Output

Syntax of if-else-if Syntax of if-else-if StatementStatement

if(BooleanExpression){statement;

} else if(BooleanExpression){

statement;} else{

statement;}

main test

Alternative test if the if statement is false

Page 28: Reading Keyboard Output

LOGICAL OPERATORSLOGICAL OPERATORSJava provides two binary operators, && and ||, which are used to combine two boolean expressions into a single expression.

Java also provides the unary ! operator, which reverses the truth of a boolean expression.

Page 29: Reading Keyboard Output

Truth table of the && Truth table of the && operatoroperator

EXPRESSION VALUE true&&false false false&&true false false&&false false true&&true true

Page 30: Reading Keyboard Output

Truth table of the || Truth table of the || operatoroperator

EXPRESSION VALUE true|| false true false|| true true false|| false false true|| true true

Page 31: Reading Keyboard Output

LOGICAL OPERATORS IN ORDER LOGICAL OPERATORS IN ORDER OF PRECEDENCEOF PRECEDENCE

1st = !2nd=&&3rd= ||Note: ! Operator has higher precedence

than many of Java’s other operators.You should enclose its operand in ()

unless you intend to apply it to a variable or simple expression w/ no other operators.

Page 32: Reading Keyboard Output

Example UsageExample Usage

Assume x is an int w/ a value stored in it:

! (x >2) read as “ is x not greater than 2?”

!x>2read as “ is logical complement of x is greater than 2? ”

Note: ! operator can be applied only to boolean expressions.

Page 33: Reading Keyboard Output

Switch StatementSwitch StatementCONCEPT:

switch statement lets the value of a variable or expression determine where the program will branch to.

is a multiple alternative decision structure.

can be used as an alternative of if-else-if statement that test the same variable w/ several different values.

Page 34: Reading Keyboard Output

StructureStructure

Page 35: Reading Keyboard Output

Switch statement syntaxSwitch statement syntax switch(variable) {

case value_1:statement;break;

case value_N : statement;break;

default :statement;break;

}

These statement is executed if the variable is equal to value_1

These statement is executed if the variable is equal to value_N.

These statement is executed if the variable is not equal to any of the case values.

Page 36: Reading Keyboard Output

Sample Usage(flowchart Sample Usage(flowchart example)example) switch(month)

{

case 1:

System.out.print(“January”);

break;

case 2:

System.out.print(“February”);

break;

case 3:

System.out.print(“March”);

break;

default:

System.out.print(“Error:”);

break;

}