Principles of Programming using Java - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Principles of Programming using Java

Description:

e.g for doing a series of calculations - adding up the total of ... public class Drinker { public static void main(String[] args){ int age=0; while(age 18) ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 21
Provided by: michae332
Category:

less

Transcript and Presenter's Notes

Title: Principles of Programming using Java


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

3
Selection Statements
  • ifelse
  • switchcase

4
Repetition - iteration
  • used to repeat one or more programming statements
  • Data Input
  • e.g. for inputting a number of items of data
  • - inputting a class of 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

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

6
Repetition - deterministic loop
  • 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")

7
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
8
non-deterministic loop sentinel controlled
  • 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")

9
Counter controlled repetition
  • for loop is a deterministic loop
  • i.e. it executes a fixed number of times, which
    is known before the execution of the loop
  • for(counter starting value condition changes to
    counter)
  • statement(s)

10
Iteration for loops
  • for loops allow us to repeat a statement or group
    of statements, a fixed number of times
  • for (starting value condition counter)
  • for (num 0 num lt 10 num)
  • System.out.println(Count from 0 to num )

11
  • public class Forloop
  • public static void main(String args)
  • int sum0
  • for(int count 1 countlt10 count)
  • sum sum count
  • System.out.println(Sum of the integers 1 to 10
    sum)

12
  • write a program using the for loop, to convert
    yards 1 to 10, to inches.
  • DISPLAY Yards Inches
  • FOR count 1 TO 10
  • inches count 36
  • DISPLAY count inches

13
  • write a program using the for loop that will take
    a number from the user and produce an
  • n times table for the first 6 numbers
  • GET num
  • FOR count 1 TO 6
  • sum num count
  • DISPLAY count num sum

14
do while loop
  • In do while loops the test condition comes at the
    end of the loop. So, the loop always iterates at
    least once.
  • do
  • statement(s)
  • while(condition)

15
do while loop
  • int num JOptionPane.showInputDialog(Enter
    number)
  • do
  • if(num1)
  • System.out.println(chocolate selected)
  • else
  • if(num2)
  • System.out.println(sherbet selected)
  • else
  • System.out.println(peanuts default choice)
  • while(num gt 0)

16
Which Loop, When?
  • for loop when you know how many times the loop
    will be executed
  • while loop when you dont know how many times
    the loop will execute, including when the user is
    in control
  • while loop when you may not want the loop to
    execute even once
  • do while when you want the loop to execute even
    once

17
JTextArea
18
JTextArea
  • JTextArea outputArea new JTextArea()
  • String output This is what I want in my
    TextArea - row 1\n and this on row 2
  • outputArea.setText(output)
  • JOptionPane.showMessageDialog(null, outputArea,
    "Results", JOptionPane.INFORMATION_MESSAGE)

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

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