Main and Control Flow - PowerPoint PPT Presentation

1 / 70
About This Presentation
Title:

Main and Control Flow

Description:

Signature. Name. Named Constant. Statement List. Statement ... Statement ... Signature. Name. Named Constant. Implements. Statement. Interface. Implements ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 71
Provided by: Prasun2
Learn more at: http://www.cs.unc.edu
Category:
Tags: control | flow | main | signature

less

Transcript and Presenter's Notes

Title: Main and Control Flow


1
Main and Control Flow
  • Programming without ObjectEditor
  • Main Class
  • Control Flow
  • Real Programming
  • Linear
  • Branching
  • Looping
  • Programmer-Defined Overloading
  • Console Input
  • Programmer-Defined Library Class

2
Programming Vs Bicycle Riding
3
Starting with the Big Picture
Name
Signature
Statement
Statement
4
Starting with the Small Picture
Statement
5
Starting with the Abstractions
Name
Signature
Statement
Statement
6
Starting with Optional Components
Name
Signature
Statement
Required
Statement
7
Focussing on Control Flow (Real Programming)
Linear
8
Removing Training Wheels
Text Editor
, Main Class
Java Interpreter
9
Equivalent User Interfaces
10
Algorithm (edit)
11
Algorithm (edited)
12
Algorithm (soln)
  • Create ALoanPair instance based on the two values
    entered by the user in the console window.
  • Print the properties of the loan pair object in
    the console window.
  • Pause until user presses some keystroke.

13
Main Method
public static void main (String args)
LoanPair loanPair new ALoanPair(readCarLoan(),
readHouseLoan()) print (loanPair)
pause()
14
Main Method without Method Chaining
Stepwise refinement
public static void main (String args)
Loan carLoan readCarLoan() Loan houseLoan
readHouseLoan() LoanPair loanPair new
ALoanPair(carLoan, houseLoan()) print
(loanPair) pause()
15
readCarLoan()
16
readCarLoan()
  • Prompt user
  • Return an instance of ALoan constructed from the
    principal.

public static Loan readCarLoan()
System.out.println("Please enter car
principal") return new ALoan(readInt())
17
readInt()
  • Wait for the user to enter a string (of digits)
    on the next line.
  • Return the int represented by the string.
  • In case the user makes an error or terminates
    input before entring a valid int, return 0 and
    print an error message.

static DataInputStream dataIn new
DataInputStream (System.in) public
static int readInt() try return
Integer.parseInt(dataIn.readLine()) catch
(Exception e) System.out.println(e) retur
n 0
18
Try-Catch Block
try return Integer.parseInt(dataIn.readLine
()) catch (Exception e)
System.out.println(e) return 0
19
Importing a Package
import java.io.DataInputStream public class
ALoanPairDriver . ...
package java.io public class DataInputStream
.
new DataInputStream(System.in)
public class ALoanPairDriver ...
new java.io.DataInputStream(System.in)
20
Printing a LoanPair
21
Printing a LoanPair
public static void print (LoanPair loanPair)
System.out.println("Car Loan") print
(loanPair.getCarLoan()) System.out.println("
House Loan") print(loanPair.getHouseLoan()
) System.out.println("Total
Loan") print (loanPair.getTotalLoan())
Programmer-defined Overloading
public static void print (Loan loan)
System.out.println("Principal"
loan.getPrincipal()) System.out.println("Ye
arly Interest" loan.getYearlyInterest())
System.out.println("Monthly Interest"
loan.getMonthlyInterest())
22
pause()
Returns single char
public static void pause() try
System.in.read() catch (Exception e)
System.out.println(e)
System.out.println(pause())
Legal Expression Statement
23
Expression Vs Statement
public static void pause() try
System.in.read() catch (Exception e)
System.out.println(e)
5 3
System.out.println(pause())
24
Function Calls
  • Can be used as expression
  • Can be used as statement
  • When return value is to be ignored
  • Rarely happens
  • Check program to see all return values being used
    as expressions
  • Other expressions not statements
  • Procedure calls never expressions

25
Multi-level Algorithm/Code
main
26
Class-Level Decomposition
Monolithic Main Class (Loan User Interface and
Loan Computation)
27
Class-Level Decomposition
main
28
Separate Class
Keyboard
pause()
readInt()
readDouble()
readBoolean()
readChar()
readString()
29
Using Keyboard
public static Loan readCarLoan()
System.out.println("Please enter car
principal") return new ALoan(readInt())
return new ALoan(Keyboard.readInt())
30
Separation of Concerns
  • Make independent piece of code as separate
    method.
  • Make independent set of methods as separate
    class.
  • Use operation and data abstraction!

31
Running Main
32
Single-Stepping
33
Step Into Vs Step Over
34
Inspecting Variables
35
Conditionals
printPassFailStatus(95)
PASS
printPassFailStatus(25)
FAIL
public static void printPassFailStatus(int
score) if (score lt PASS_CUTOFF) System.out.pr
intln("FAILED") else System.out.println("PASSE
D")
36
If-else Statement
if (ltbool exprgt) ltstatement
1gt else ltstatement 2gt
37
If-Else Statement
true
false
ltbool exprgt
38
Compound Statement
public void fancyPrintGrade(int score) if
(score lt PASS_CUTOFF) System.out.print
ln("") System.out.println("FAILED
") System.out.println("")
else System.out.println("
") System.out.println("PASSED") System.
out.println("Congratulations!") System.out.prin
tln("")
39
Avoding Code Duplication in If-Else
public void fancyPrintGrade(int score)
System.out.println("") if
(score lt PASS_CUTOFF) System.out.println("FAI
LED") else System.out.println("PASSED")
System.out.println("Congratulations!")
System.out.println("")
40
Avoding Code Duplication in If-Else
public void fancyPrintGrade(int score)
System.out.println("") if
(score lt PASS_CUTOFF) System.out.println("FAI
LED") else System.out.println("PASSED")
System.out.println("Congratulations!")
System.out.println("")
41
If Statement
if (score MAX_SCORE) System.out.println
("Perfect Score! Congratulations!")
if (ltbool exprgt) ltstatementgt
42
if Statement
true
ltbool exprgt
false
ltstatementgt
43
Nested if-else
public static char toLetterGrade (int score) if
(score gt A_CUTOFF) return 'A' else if
(score gt B_CUTOFF) return 'B' else if
(score gt C_CUTOFF) return 'C' else if
(score gt D_CUTOFF) return 'D' else
return 'F'
44
Nested If-Else
if (score gt A_CUTOFF) return 'A' else if
(score gt B_CUTOFF) return 'B' else if
(score gt C_CUTOFF) return 'C' else
if (score gt D_CUTOFF) return 'D'
else return
'F' 
45
Nested If-Else
46
Looping
printHello(2)
printHello(3)
hello
hello
hello
hello
hello
47
Loops
public static void printHello(int n)
int counter 0 if (counter lt n)
counter counter 1 System.out.println
(hello)

48
Loops
public static void printHello(int n)
int counter 0 while (counter lt n)
counter counter 1 System.out.println
(hello)

49
If Vs While Statement
if (ltbool exprgt) ltstatementgt
while (ltbool exprgt) ltstatementgt
50
if Statement
true
ltbool exprgt
false
ltstatementgt
51
while Statement
true
ltbool exprgt
false
ltstatementgt
52
while loop
ltstatement gt
true
ltbool exprgt
false
53
Sentinel-based Folding
54
Adding Fixed Number of Loans
Loan loan1 readLoan() Loan loan2
readLoan() Loan loan3 readLoan() Loan loan4
readLoan() Loan sumLoan ALoan.add(loan1,
ALoan.add(loan2, ALoan.add(loan3,
loan4))) print (sumLoan)
55
Generalizing to Variable Loans
Loan loan1 readLoan() Loan loan2
readLoan() Loan loan3 readLoan() Loan loan4
readLoan() Loan loanN readLoan() Loan
sumLoan ALoan.add(loan1, ALoan.add(loan2,
ALoan.add(loan3, Aloan.add(loan4, (add
(loanN-1, loanN) print (sumLoan)
56
Space Efficient Adding of Fixed Number of Loans
Loan loan1 readLoan() Loan loan2
readLoan() Loan sumLoan ALoan.add(loan1,
loan2) loan1 readLoan() // 3rd loan sumLoan
ALoan.add(sumLoan, loan1) loan1 readLoan()
// 4th loan sumLoan ALoan.add(sumLoan,
loan1) print (sumLoan)
57
More Space Efficient Adding of Fixed Number of
Loans
Loan sumLoan readLoan() //first loan Loan
nextLoan readLoan() //second loan sumLoan
Aloan.add(nextLoan, sumLoan) nextLoan
readLoan() // 3rd loan sumLoan
ALoan.add(sumLoan, nextLoan) nextLoan
readLoan() // 4th loan sumLoan
ALoan.add(sumLoan, nextLoan) print (sumLoan)
58
More Space Efficient Adding of Variable Number of
Loans
Loan sumLoan readLoan() //first loan Loan
nextLoan readLoan() //second loan sumLoan
Aloan.add(nextLoan, sumLoan) nextLoan
readLoan() // 3rd loan sumLoan
ALoan.add(sumLoan, nextLoan) nextLoan
readLoan() // 4th loan sumLoan
ALoan.add(sumLoan, nextLoan)
nextLoan readLoan() //Nth loan
sumLoan ALoan.add(sumLoan, nextLoan)
nextLoan readLoan() //sentinel print
(sumLoan)
59
While Loop
Loan sumLoan readLoan() //first loan
Loan nextLoan readLoan() //second loan
while (nextLoan().getPrincipal() gt 0)
sumLoan ALoan.add(nextLoan,
sumLoan) nextLoan readLoan() // next loan or
sentinel print (sumLoan)
Program waits for ever for second loan
60
Correct Solution
Loan sumLoan new ALoan(0) //initial
value Loan nextLoan readLoan() //second
loan while (nextLoan().getPrincipal() gt
0) sumLoan ALoan.add(nextLoan,
sumLoan) nextLoan readLoan() // next loan or
sentinel print (sumLoan)
ALoan.add(new ALoan(0), add(loan1, add (., loanN)
61
A Single Sentinel Value
Loan sumLoan new ALoan(0) //initial
value Loan nextLoan readLoan() //second
loan while (nextLoan().getPrincipal() gt
0) sumLoan ALoan.add(nextLoan,
sumLoan) nextLoan readLoan() // next loan or
sentinel print (sumLoan)
62
A Single Loan
Loan sumLoan new ALoan(0) //initial
value Loan nextLoan readLoan() //second
loan while (nextLoan().getPrincipal() gt
0) sumLoan Aloan.add(nextLoan,
sumLoan) nextLoan readLoan() // next loan or
sentinel print (sumLoan)
63
Two Loans
Loan sumLoan new ALoan(0) //initial
value Loan nextLoan readLoan() //second
loan while (nextLoan().getPrincipal() gt
0) sumLoan Aloan.add(nextLoan,
sumLoan) nextLoan readLoan() // next loan or
sentinel print (sumLoan)
64
Multiplying Numbers (edit)
???
65
Multiplying Numbers (edited)
???
66
Multiplying Numbers
int product 1 int num Keyboard.readInt()
while (num gt 0) product productnum
num Keyboard.readInt()
print (product)
12023
67
Comparing Two Solutions
int product 1 int num Keyboard.readInt()
while (num gt 0) product productnum
num Keyboard.readInt()
print (product)
Loan sumLoan new ALoan(0) Loan
nextLoan readLoan() while
(nextLoan().getPrincipal() gt 0) sumLoan
ALoan.add(nextLoan, sumLoan) nextLoan
readLoan() print (sumLoan)
// print value
68
Generalized Folding of a Sentinal-Terminated List
a1
an
a3
a2
f
f
f T, T ? T
f
F(x, I) ? x
69
Generalized Folding Solution
T result I T nextValue
getNextValue() while (!isSentinel(nextValue))
result f(result, nextValue) nextValue
getNextValue(..)
gt 0
70
Comparing Two Solutions (Comments)
int product 1 //identity int num
Keyboard.readInt() // read next list value
while (num gt 0) // sentinel checking
product productnum // binary folding
function num Keyboard.readInt() // read
next value print (product)//
print value
Loan sumLoan new ALoan(0) //identity
Loan nextLoan readLoan() // read next list
value while (nextLoan().getPrincipal() gt
0) // sentinel checking sumLoan
Aloan.add(nextLoan, sumLoan) // binary folding
function nextLoan readLoan() // read next
list value print (sumLoan) //
print value
Write a Comment
User Comments (0)
About PowerShow.com