Programming Principles using Java - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Programming Principles using Java

Description:

System.out.println('You can access your account'); else ... Increment & Decrement Operators. i increment AFTER the expression is evaluated ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 23
Provided by: michae332
Category:

less

Transcript and Presenter's Notes

Title: Programming Principles using Java


1
Programming Principles using Java
2
Control of Flow
  • SEQUENCE
  • SELECTION
  • REPETITION

3
Control of Flow - Selection
  • ifelse
  • if(pin1234)
  • System.out.println(You can access your
    account)
  • else
  • System.out.println(Wrong pin - N0 ACCESS)

4
Control of Flow - Selection
  • ifelse
  • if(age gt 18)
  • System.out.println(You can drink)
  • else
  • System.out.println(Wait until you are 18)

5
Control of Flow - Selection
  • switch..case
  • switch (menu)
  • case 1
  • System.out.println(chocolate selected)
  • break
  • case 2
  • System.out.println(fruitgums selected)
  • break
  • case 3
  • System. out.println(sherbet selected)
  • break
  • default
  • System.out.println(Nothing selected)

6
Repetition - iteration
  • used to repeat one or more programming statements
  • Data Input
  • e.g. for inputting a number of items of data
  • - inputting a number of a students marks
  • Processing
  • e.g for doing a series of calculations
  • - adding up the total of each students marks
    scored
  • Output
  • e.g. for outputting a number of items of data
  • - outputting each of the student totals for a
    class of students

7
while loops
  • while loops continue to loop while some condition
    is true, and stops when the condition becomes
    false
  • while (condition) do this
  • int age1
  • while (age lt 18)
  • System.out.println(You cant drink)
  • age

8
Deterministic loops
  • Deterministic loops execute a fixed number of
    times and this number is known in advance of the
    loop executing
  • int counter 0
  • while (counter lt 5)
  • System.out.println(counter iscounter)
  • counter

9
Repetition
  • public class Drinker
  • public static void main(String args)
  • int age0
  • while(age lt18)
  • System.out.println(You are too young to
    drink")
  • age age1
  • System.out.println(You can now drink")

10
  • import javax.swing.JOptionPane
  • public class Choice
  • public static void main(String args)
  • int counter, menu0
  • counter 1
  • while(counter lt5)
  • String s1 JOptionPane.showInputDialog("Enter
    your choice")
  • menu Integer.parseInt(s1)
  • switch (menu)
  • case 1
  • System.out.println("chocolate selected")
  • break
  • case 2
  • System.out.println("fruitgums selected")
  • break
  • case 3
  • System. out.println("sherbet
    selected")
  • break

11
Non- deterministic loops- sentinel controlled
loops
Enter a data item
Does this value match the sentinel?
YES
NO
process the item
enter a data item
exit the loop
12
Sentinel controlled loop
  • import javax.swing.JOptionPane
  • public class Sentinel
  • public static void main(String args)
  • int input1
  • while(input !0 )
  • String s1 JOptionPane.showInputDialog("Enter
    your choice, 0 to end")
  • input Integer.parseInt(s1)
  • if(input1)
  • System.out.println("chocolate selected")
  • else
  • if(input2)
  • System.out.println("fruitgums selected")
  • else
  • System.out.println("Nothing selected")

13
Validating input
  • import javax.swing.JOptionPane
  • public class SentinelGuard
  • public static void main(String args)
  • int input0
  • String s1
  • s1 JOptionPane.showInputDialog(Enter your
    choice, ONLY ENTER 1 or 2")
  • input Integer.parseInt(s1)
  • while(input lt 1 input gt2 )
  • s1 JOptionPane.showInputDialog(Enter your
    choice, ONLY ENTER 1 or 2")
  • input Integer.parseInt(s1)
  • //end while loop
  • if(input1)
  • System.out.println("chocolate selected")
  • if(input2)
  • System.out.println("fruitgums selected")

14
  • import javax.swing.JOptionPane
  • public class SentinelAverage1
  • public static void main( String args )
  • double average // number with decimal
    point
  • int num, counter, grade, total
  • String snum
  • // initialization phase
  • total 0
  • counter 0
  • // processing phase. First, "prime" the
    loop
  • snum JOptionPane.showInputDialog( "Enter
    a number, with -1 to end " )
  • numInteger.parseInt(snum)
  • while ( num ! -1 )
  • if ( num 1 )
  • total total 4

15
Increment Decrement Operators
  • i increment AFTER the expression is evaluated
  • i increment BEFORE the expression is evaluated
  • i 0 i 0
  • x i \\ x is 1 x i \\ x
    is 0
  • Also i-- and --i

16
Casting and Conversion
  • Sometimes we need to convert from one data type
    to another, to do this we use a cast
  • An arithmetic operation ( - / ) on 2 integers
    results in an integer
  • integer integer / integer
  • To get a decimal result we have to cast
  • double average int total, counter
  • average (double) total / counter

17
DecimalFormat
  • import java.text.
  • public class Format
  • public static void main(String args)
  • double result0 int first 4, second3
  • result (double) first/second
  • System.out.println(result)
  • DecimalFormat twoplaces new DecimalFormat("0.00"
    )
  • String output twoplaces.format(result)
  • System.out.println(output)
  • System.out.println(twoplaces.format(result))

18
DecimalFormat
  • format output to 2 decimal places
  • DecimalFormat twoplaces new DecimalFormat("0.00"
    )
  • String output twoplaces.format(result)
  • format output to 1 decimal place
  • DecimalFormat oneplaces new DecimalFormat("0.0")
  • String output oneplaces.format(result)
  • format output to 3 decimal places
  • DecimalFormat threeplaces new
    DecimalFormat("0.000")
  • String output oneplaces.format(result)

19
(No Transcript)
20
  • import javax.swing.JOptionPane
  • import java.text.DecimalFormat
  •  
  • public class Average2
  • public static void main( String args )
  • int gradeCounter, // number of grades
    entered
  • gradeValue, // grade value
  • total // sum of grades
  • double average // average of all grades
  • String input // grade typed by user
  •  
  • // Initialization phase
  • total 0 // clear total
  • gradeCounter 0 // prepare to loop
  • // Processing phase
  • // prompt for input and read grade from
    user
  • input JOptionPane.showInputDialog(

21
  • // Fig. 4.9 Average2.java
  • // Class average program with sentinel-controlled
    repetition
  • import javax.swing.JOptionPane
  • import java.text.DecimalFormat
  •  
  • public class Average2
  • public static void main( String args )
  • int gradeCounter, // number of grades
    entered
  • gradeValue, // grade value
  • total // sum of grades
  • double average // average of all grades
  • String input // grade typed by user
  •  
  • // Initialization phase
  • total 0 // clear total
  • gradeCounter 0 // prepare to loop
  • // Processing phase

22
  • while ( gradeValue ! -1 )
  • // add gradeValue to total
  • total total gradeValue
  •  
  • // add 1 to gradeCounter
  • gradeCounter gradeCounter 1
  •  
  • // prompt for input and read grade from
    user
  • input JOptionPane.showInputDialog(
  • "Enter Integer Grade, -1 to
    Quit" )
  •  
  • // convert grade from a String to an
    integer
  • gradeValue Integer.parseInt( input )
  •  
  • // Termination phase
  • DecimalFormat twoDigits new
    DecimalFormat( "0.00" )
  •  
  • if ( gradeCounter ! 0 )
Write a Comment
User Comments (0)
About PowerShow.com