Topics Anatomy of an API A development walkthrough General characteristics of utility classes 8 The Development Process Analysis Design Implementation Testing Deployment 9 1
The Development Process Analysis Design The requirement: Input and its validation Output and its formatting Implementation Testing Deployment 10 Analysis The Development Process Design An algorithm (function) that determines the output given the input Implementation Testing Deployment 11 2
Analysis The Development Process Design Implementation Turn the algorithm into a program Testing Deployment 12 Analysis The Development Process Design Implementation Testing Does the program meet the requirement? Deployment 13 3
Analysis The Development Process Design Implementation Testing Deployment Installing, porting, training, support 14 The Mortgage Application - Analysis Requirement Compute the monthly payment of a mortgage 15 4
The Mortgage Application - Analysis Requirement Compute the monthly payment of a mortgage Input Mortgage amount; annual interest percent; both reals Validation: amount > 0; interest in range (0, 100) 16 The Mortgage Application - Analysis Requirement Compute the monthly payment of a mortgage Input Mortgage amount; annual interest percent; both reals Validation: amount > 0; interest in range (0, 100) Output Monthly payment Formatting: rounded to the nearest cent; thousands separator 17 5
The Mortgage Application - Analysis Sample run of proposed program: Enter the amount 285000 The annual interest percent 375 The monthly payment is: $1,46527 18 The Mortgage Application - Design P 1 ra 1 (1 r) n P is the monthly payment, r is the monthly interest rate, A is the mortgage amount, and n is the number of months (We'll assume a 25-year amortization) 19 6
The Mortgage Application - Implementation Implement a class to take care of prompts and inputs Ignore validation for now We ll do the computation ourselves with the help of A class that computes powers A class for output Ignore formatting for now 20 Implementation Notes The importance of prompting Using print versus println Use the next methods of Scanner Converting from an annual percent to a monthly rate Hard-coded constants like 12 are a source of confusion; using final 21 7
Mortgage01java First Attempt In-class Note: View from Eclipse 22 The printf method Output Formatting The first parameter holds format specifiers Each specifier has the form: %[flag][width][precision]conversion The conversion letter can be d, f, s, or n The flag can be, or 0 (, = thousands separator; 0 = pad left with zeros) The width specifies the minimum field width and the precision specifies the number of decimals Example outputprintf("%,62f", x); 23 8
Second Attempt (with printf) Mortgage02java Instructor Note: View from Eclipse 24 Relational Operators Relational operators take two operands and produce a boolean result < <= > >= Numeric operands ==!= Operands of any type 25 9
Precedence (add to previous table) Precedence Operator Operands Syntax true if < numeric x < y x is less than y <= numeric x <= y x is less than or equal to y -7 > numeric x > y x is greater than y >= numeric x >= y x is greater than or equal to y instanceof x instanceof C is true if object reference x points to an instance of class C or a subclass of C -8 == any type x == y x is equal to y!= any type x!= y x is not equal to y 26 Input Validation Invalid inputs are the cause of most errors in programs Therefore, upon encountering one, a program has three options: 1 Print a message and end 2 Print a message then allow the user to retry several times or decide to abort 3 Trigger a runtime error; ie, crash For now, let us use the 3 rd option via a method in Toolbox: static void crash(boolean, String) 27 10
Third Attempt (with crash) Mortgage03java Instructor Note: View from Eclipse 28 Assertions A simple yet powerful tool to guard against errors that arise from misunderstandings If you believe some non-trivial condition is true, assert it, eg assert payment >=0; You cannot assert a validation because user input is not under the programmer's control Hence, do not confuse assert (a Java statement) with crash (a method) 29 11
Topics Anatomy of an API A development walkthrough General characteristics of utility classes 30 Memory Diagrams Compile, load, and run Circlejava (which uses a field and method in the Math utility class) import javautilscanner; import javaioprintstream; public class Circle { public static void main(string[] args) { Scanner input = new Scanner(Systemin); PrintStream output = Systemout; outputprint("enter radius: "); int radius = inputnextint(); outputprintln(mathpi * Mathpow(radius, 2)); } } 31 12
Memory Diagrams (2) Before looking at a memory diagram, improve Circlejava by printing the area with some lead-in text and displaying the area to two decimal places 32 Memory Diagrams radius 0 1 64 80 Circle PI pow 800 824 880 Math 33 13
Advantages of Utility Classes Simplicity To access a static field f in a class C, write: Cf To invoke a static method m in a class C, write Cm( ) There is only one copy of a static class in memory Suitability A utility class is best suited to hold a group of methods that do not hold state, eg, javalangmath Even in non-utility classes, static is best suited for features that are common to all instances, eg the MAX_VALUE field and the parseint method of the (non-utility) class Integer 34 Case Study: Dialog I/O Consider two static methods in: javaxswingjoptionpane To display a message: void showmessage(null, message) To prompt for and read an input: String showinputdialog(null, prompt) Note: showinputdialog returns a String To read a number, you must invoke one of the parse methods in the corresponding wrapper class (eg, Integer, Double) 35 14
The Mortgage Application (using JOptionPane) Mortgage04java Instructor Note: View from Eclipse 36 Thank You 37 15