Programming Principles using Java - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Programming Principles using Java

Description:

The operands of boolean operators are boolean expressions. Their evaluation results in a boolean expression. Truth Table for Boolean Operators ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 25
Provided by: michae332
Category:

less

Transcript and Presenter's Notes

Title: Programming Principles using Java


1
Programming Principles using Java
2
Program Structure
  • Many programs have this structure

Data Input
Processing
Output results
3
Retirement Program
  • PROMPT Enter your age
  • GET age
  • STORE age
  • years 65 age
  • OUTPUT You will retire in years years

4
Program Structure
Data Input //prompt String myAge
JOptionPane.showInputDialog(Enter your
age) //assign to variable int age
Integer.parseInt(myAge)
5
Data Input - prompt
6
Program Structure
  • Processing
  • \\work out how many years to retirement
  • int years 65 - age

7
Program Structure
  • Output
  • \\display how many years to retirement
  • JOptionPane.showMessageDialog(
  • null,
  • You will retire in years years,
  • Output, JOptionPane.PLAIN_MESSAGE)

8
Output
9
Retirement program
  • import javax.swing.JOptionPane
  • public class Retirement
  • public static void main(String args)
  • \\prompt
  • String myAge JOptionPane.showInputDialog(Enter
    your age)
  • \\convert to an int and assign to a variable
  • int age Integer.parseInt(myAge)
  • \\processing
  • int years 65 - age
  • \\output results of processing
  • JOptionPane.showMessageDialog(null, You will
    retire in years years, Output,
    JOptionPane.PLAIN_MESSAGE)
  • System.exit(0)

10
Program flow
  • Control of flow is the order in which program
    statements are executed
  • There are 3 types
  • SEQUENCE
  • SELECTION
  • REPETITION

11
SELECTION
  • Sometimes the code you execute depends upon
    whether a condition is true
  • if(pin 1234)
  • Then you can access your bank account
  • if(passwordp1w234)
  • Then you can access your university account

12
Selection Statements
  • Selection statements allow the program to choose
    between alternate actions during execution
  • if (ltconditional expressiongt)
  • ltstatementgt
  • if (age gt 18)
  • System.out.println(You can buy a drink)

13
if else Statement
  • if (conditional expression)
  • ltstatement1gt
  • else
  • ltstatement2gt
  • if (age gt 18)
  • System.out.println(You can buy a drink)
  • else
  • System.out.println(Sorry, wait until you are
    18)

14
Using Blocks
  • To include more than one statement to be executed
    if a condition is true, put them in a block, i.e.
  • if(age gt 18)
  • System.out.println(You can buy a drink)
  • System.out.println(You can go to University)

15
Relational Operators
  • Relational Operator Test Condition
  • a b equal to
  • ! a ! b not equal to
  • lt a lt b less than
  • lt a lt b less than or equal to
  • gt a gt b greater than
  • gt a gt b greater than or equal to

16
Selection Program
  • PROMPT Enter your pin number
  • GET input
  • STORE input
  • IF pin input
  • OUTPUT You can access your account
  • ELSE
  • OUTPUT You CANNOT access your account

17
Selection Program
  • import javax.swing.JOptionPane
  • public class Access
  • public static void main(String args)
  • int pin 1234
  • String myInput
  • int input
  • myInput JOptionPane.showInputDialog(Enter your
    pin number)
  • input Integer.parseInt(myInput)
  • if(pininput)
  • JOptionPane.showMessageDialog(null, You CAN
    access your account, Results,
    JOptionPane.PLAIN_MESSAGE)
  • else
  • JOptionPane.showMessageDialog(null, You CANNOT
    access the account, Results,
    JOptionPane.PLAIN_MESSAGE)
  • System.exit(0)

18
Drinking Program
  • PROMPT Enter your age
  • GET age
  • STORE age
  • IF age gt 18
  • OUTPUT You can drink
  • ELSE
  • OUTPUT Sorry, wait until you are 18

19
Selection Program
  • import javax.swing.JOptionPane
  • public class Drinking
  • public static void main(String args)
  • int drinkAge 18
  • String myInput
  • int input
  • myInput JOptionPane.showInputDialog(Enter your
    age )
  • input Integer.parseInt(myInput)
  • if(input gt drinkAge)
  • JOptionPane.showMessageDialog(null, You can buy
    a drink, Results, JOptionPane.PLAIN_MESSAGE)
  • else
  • JOptionPane.showMessageDialog(null, Sorry wait
    until you are 18, Results, JOptionPane.PLAIN_ME
    SSAGE)
  • System.exit(0)

20
Boolean Logical Operators
  • (AND) (OR) (XOR) !( NOT)
  • The operands of boolean operators are boolean
    expressions.
  • Their evaluation results in a boolean expression

21
Truth Table for Boolean Operators
  • P Q !P P Q P Q P Q
  • true true false true true false
  • true false false false true true
  • false true true false true true
  • false false true false false false

22
Examples
  • if((age gt5) (age lt16))
  • System.out.print(You are of school age)
  • if((age lt 5) (age gt 16))
  • System.out.print(You are not of school age)
  • if((x gt 10) (x lt 12))
  • System.out.print(Your number is in range)

23
Conditional Operators ,
  • Conditional operators are like boolean logical
    operators, except that their evaluation is
    short-circuited.
  • The left operator is evaluated before the right
    one.
  • x y true if both operands are true
  • x y true if either or both operands are
    true

24
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com