Title: Main and Control Flow
1Main and Control Flow
- Programming without ObjectEditor
- Main Class
- Control Flow
- Real Programming
- Linear
- Branching
- Looping
- Programmer-Defined Overloading
- Console Input
- Programmer-Defined Library Class
2Programming Vs Bicycle Riding
3Starting with the Big Picture
Name
Signature
Statement
Statement
4Starting with the Small Picture
Statement
5Starting with the Abstractions
Name
Signature
Statement
Statement
6Starting with Optional Components
Name
Signature
Statement
Required
Statement
7Focussing on Control Flow (Real Programming)
Linear
8Removing Training Wheels
Text Editor
, Main Class
Java Interpreter
9Equivalent User Interfaces
10Algorithm (edit)
11Algorithm (edited)
12Algorithm (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.
13Main Method
public static void main (String args)
LoanPair loanPair new ALoanPair(readCarLoan(),
readHouseLoan()) print (loanPair)
pause()
14Main 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()
15readCarLoan()
16readCarLoan()
- 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())
17readInt()
- 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
18Try-Catch Block
try return Integer.parseInt(dataIn.readLine
()) catch (Exception e)
System.out.println(e) return 0
19Importing 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)
20Printing a LoanPair
21Printing 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())
22pause()
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
23Expression Vs Statement
public static void pause() try
System.in.read() catch (Exception e)
System.out.println(e)
5 3
System.out.println(pause())
24Function 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
25Multi-level Algorithm/Code
main
26Class-Level Decomposition
Monolithic Main Class (Loan User Interface and
Loan Computation)
27Class-Level Decomposition
main
28Separate Class
Keyboard
pause()
readInt()
readDouble()
readBoolean()
readChar()
readString()
29Using Keyboard
public static Loan readCarLoan()
System.out.println("Please enter car
principal") return new ALoan(readInt())
return new ALoan(Keyboard.readInt())
30Separation of Concerns
- Make independent piece of code as separate
method. - Make independent set of methods as separate
class. - Use operation and data abstraction!
31Running Main
32Single-Stepping
33Step Into Vs Step Over
34Inspecting Variables
35Conditionals
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")
36If-else Statement
if (ltbool exprgt) ltstatement
1gt else ltstatement 2gt
37If-Else Statement
true
false
ltbool exprgt
38Compound 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("")
39Avoding 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("")
40Avoding 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("")
41If Statement
if (score MAX_SCORE) System.out.println
("Perfect Score! Congratulations!")
if (ltbool exprgt) ltstatementgt
42if Statement
true
ltbool exprgt
false
ltstatementgt
43Nested 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'
44Nested 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'
45Nested If-Else
46Looping
printHello(2)
printHello(3)
hello
hello
hello
hello
hello
47Loops
public static void printHello(int n)
int counter 0 if (counter lt n)
counter counter 1 System.out.println
(hello)
48Loops
public static void printHello(int n)
int counter 0 while (counter lt n)
counter counter 1 System.out.println
(hello)
49If Vs While Statement
if (ltbool exprgt) ltstatementgt
while (ltbool exprgt) ltstatementgt
50if Statement
true
ltbool exprgt
false
ltstatementgt
51while Statement
true
ltbool exprgt
false
ltstatementgt
52while loop
ltstatement gt
true
ltbool exprgt
false
53Sentinel-based Folding
54Adding 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)
55Generalizing 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)
56Space 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)
57More 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)
58More 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)
59While 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
60Correct 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)
61A 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)
62A 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)
63Two 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)
64Multiplying Numbers (edit)
???
65Multiplying Numbers (edited)
???
66Multiplying Numbers
int product 1 int num Keyboard.readInt()
while (num gt 0) product productnum
num Keyboard.readInt()
print (product)
12023
67Comparing 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
68Generalized Folding of a Sentinal-Terminated List
a1
an
a3
a2
f
f
f T, T ? T
f
F(x, I) ? x
69Generalized Folding Solution
T result I T nextValue
getNextValue() while (!isSentinel(nextValue))
result f(result, nextValue) nextValue
getNextValue(..)
gt 0
70Comparing 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