Download presentation
Presentation is loading. Please wait.
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 Statements Expressions Primitive Types Least Privilege Interfaces Reuse Stepwise- Refinement
3
Interface Implements Class Method Return Type/ Formal Parameter Statement Starting with the Big Picture Variable (State) Local Variable Method Header Signature Name Named Constant Statement List Statement... Statement
4
Starting with the Small Picture Class Method Return Type/ Formal Parameter Statement List Statement... Variable (State) Local Variable Interface Method Header Signature Name Named Constant Implements Statement
5
Interface Implements Class Method Return Type/ Formal Parameter Statement Starting with the Abstractions Variable (State) Local Variable Method Header Signature Name Named Constant Statement List Statement... Statement Data Abstraction exports Operation Abstraction exports
6
Interface Implements Class Method Return Type/ Formal Parameter Statement Starting with Optional Components Variable (State) Local Variable Method Header Signature Name Named Constant Statement List Statement... Statement Real-Work Required
7
Focussing on Control Flow (“Real Programming”) Statement Statement List Statement... Statement List Statement... Statement Linear Statement List Stmt Statement Stmt Branching Statement List Stmt Statement Stmt Looping
8
Removing Training Wheels Text Editor ALoanPair Source Code creates Java Compiler reads ALoanPair Object (Byte) Code creates Java Interpreter ALoanPair Instance getTotalLoan() instantiates calls ObjectEditor main calls, Main Class Main Class Object (Byte) Code Static method
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
public static void main (String[] args) { LoanPair loanPair = new ALoanPair(readCarLoan(), readHouseLoan()); print (loanPair); pause(); } Main Method Method chaining List of user-supplied arguments main header
14
public static void main (String[] args) { Loan carLoan = readCarLoan(); Loan houseLoan = readHouseLoan(); LoanPair loanPair = new ALoanPair(carLoan, houseLoan()); print (loanPair); pause(); } Main Method without Method Chaining Undefined Stepwise refinement
15
readCarLoan()
16
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()); } Called by class method (main) Undefined
17
static DataInputStream dataIn = new DataInputStream (System.in); public static int readInt() { try { return Integer.parseInt(dataIn.readLine()); } catch (Exception e) { System.out.println(e); return 0; } 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.
18
try { return Integer.parseInt(dataIn.readLine()); } catch (Exception e) { System.out.println(e); return 0; } Try-Catch Block Exception Object Program fragment that can cause exception Exception Handler
19
public class ALoanPairDriver {... … } import java.io.DataInputStream; public class ALoanPairDriver { …. …... } Importing a Package new DataInputStream(System.in) new java.io.DataInputStream(System.in) package java.io; public class DataInputStream { …. } short name full name Import Declaration
20
Printing a LoanPair
21
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()); } public static void print (Loan loan) { System.out.println("Principal:" + loan.getPrincipal()); System.out.println("Yearly Interest:" + loan.getYearlyInterest()); System.out.println("Monthly Interest:" + loan.getMonthlyInterest()); } Programmer-defined Overloading
22
public static void pause() { try { System.in.read(); } catch (Exception e) { System.out.println(e); } pause() Returns single char System.out.println(pause()); Legal Expression & Statement
23
public static void pause() { try { System.in.read(); } catch (Exception e) { System.out.println(e); } Expression Vs Statement System.out.println(pause()); 5 + 3; Illegal
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 print (Loan) readCarLoanreadHouseLoanprint(LoanPair)pause readInt
26
Class-Level Decomposition Monolithic Main Class (Loan User Interface and Loan Computation) Object Editor Programmer- defined Class (A Loan Pair) Driver (ALoanPairDriver)
27
Class-Level Decomposition main print (Loan) readCarLoanreadHouseLoanprint(LoanPair)pause readInt
28
Separate Class pause()readInt() readDouble() readBoolean() readChar() Keyboard 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) printPassFailStatus(25) public static void printPassFailStatus(int score) { if (score < PASS_CUTOFF) System.out.println("FAILED"); else System.out.println("PASSED"); } PASS FAIL
36
If-else Statement if ( ) else
37
If-Else Statement true false
38
Compound Statement public void fancyPrintGrade(int score) { if (score < PASS_CUTOFF) { System.out.println("**************"); System.out.println("FAILED"); System.out.println("**************"); } else { System.out.println("**************"); System.out.println("PASSED"); System.out.println("Congratulations!"); System.out.println("**************"); }
39
Avoding Code Duplication in If-Else public void fancyPrintGrade(int score) { System.out.println("**************"); if (score < PASS_CUTOFF) System.out.println("FAILED"); 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 < PASS_CUTOFF) System.out.println("FAILED"); 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 ( ) ;
42
if Statement true false
43
Nested if-else public static char toLetterGrade (int score) { if (score >= A_CUTOFF) return 'A'; else if (score >= B_CUTOFF) return 'B'; else if (score >= C_CUTOFF) return 'C'; else if (score >= D_CUTOFF) return 'D'; else return 'F'; }
44
Nested If-Else if (score >= A_CUTOFF) return 'A'; else if (score >= B_CUTOFF) return 'B'; else if (score >= C_CUTOFF) return 'C'; else if (score >= D_CUTOFF) return 'D'; else return 'F';
45
Nested If-Else
46
Looping printHello(2) hello printHello(3) hello
47
Loops int counter = 0; if (counter < n) { counter = counter + 1; System.out.println (“hello”); } public static void printHello(int n) { }
48
Loops int counter = 0; while (counter < n) { counter = counter + 1; System.out.println (“hello”); } public static void printHello(int n) { }
49
If Vs While Statement if ( ) ; while ( ) ;
50
if Statement true false
51
while Statement true false
52
while loop true 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); Loops + Arrays Recursion Variable # of Statements Variable # of Subexpressions (function calls)
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); N-1 repetitions
59
While Loop Loan sumLoan = readLoan(); //first loan Loan nextLoan = readLoan(); //second loan while (nextLoan().getPrincipal() >= 0) { sumLoan = ALoan.add(nextLoan, sumLoan); nextLoan = readLoan(); // next loan or sentinel } print (sumLoan); InputResult Program waits for ever for second loan Boundary Condition
60
Loan sumLoan = new ALoan(0); //initial value Loan nextLoan = readLoan(); //second loan while (nextLoan().getPrincipal() >= 0) { sumLoan = ALoan.add(nextLoan, sumLoan); nextLoan = readLoan(); // next loan or sentinel } print (sumLoan); Correct Solution ALoan.add(new ALoan(0), add(loan1, add (…., loanN) identity
61
Loan sumLoan = new ALoan(0); //initial value Loan nextLoan = readLoan(); //second loan while (nextLoan().getPrincipal() >= 0) { sumLoan = ALoan.add(nextLoan, sumLoan); nextLoan = readLoan(); // next loan or sentinel } print (sumLoan); A Single Sentinel Value
62
Loan sumLoan = new ALoan(0); //initial value Loan nextLoan = readLoan(); //second loan while (nextLoan().getPrincipal() >= 0) { sumLoan = Aloan.add(nextLoan, sumLoan); nextLoan = readLoan(); // next loan or sentinel } print (sumLoan); A Single Loan
63
Loan sumLoan = new ALoan(0); //initial value Loan nextLoan = readLoan(); //second loan while (nextLoan().getPrincipal() >= 0) { sumLoan = Aloan.add(nextLoan, sumLoan); nextLoan = readLoan(); // next loan or sentinel } print (sumLoan); Two Loans
64
Multiplying Numbers (edit) ???
65
Multiplying Numbers (edited) ???
66
Multiplying Numbers int product = 1; int num = Keyboard.readInt(); while (num >= 0) { product = product*num; num = Keyboard.readInt(); } print (product); 1*20*2*3 Identity
67
Comparing Two Solutions int product = 1; int num = Keyboard.readInt(); while (num >= 0) { product = product*num; num = Keyboard.readInt(); } print (product); Loan sumLoan = new ALoan(0); Loan nextLoan = readLoan(); while (nextLoan().getPrincipal() >= 0) { sumLoan = ALoan.add(nextLoan, sumLoan); nextLoan = readLoan(); } print (sumLoan); // print value Identity result nextVal read first value read other values Binary folding function !isSentinel(nextVal)
68
Generalized Folding of a Sentinal-Terminated List a1a1 f f f anan a3a3 a2a2 f: T, T T F(x, I) x
69
T result = I; T nextValue = getNextValue() while (!isSentinel(nextValue)) { result = f(result, nextValue); nextValue = getNextValue(..); } Generalized Folding Solution Loan, int new ALoan(0), 1 ALoan.add(), * >= 0
70
Comparing Two Solutions (Comments) int product = 1; //identity int num = Keyboard.readInt(); // read next list value while (num >= 0) { // sentinel checking product = product*num; // 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() >= 0) {// sentinel checking sumLoan = Aloan.add(nextLoan, sumLoan); // binary folding function nextLoan = readLoan(); // read next list value } print (sumLoan); // print value
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.