150 likes | 232 Views
Computer Science 112. Fundamentals of Programming II. Who Am I?. Dr. Lambert Office: Parmly 408 Phone: 8809 Email: klambert@wlu.edu Home Page: www.wlu.edu~lambertk. What Did I Learn in CSCI 111?. Problem solving techniques Algorithm development
E N D
Computer Science 112 Fundamentals of Programming II
Who Am I? Dr. Lambert Office: Parmly 408 Phone: 8809 Email: klambert@wlu.edu Home Page: www.wlu.edu\~lambertk
What Did I Learn in CSCI 111? • Problem solving techniques • Algorithm development • Abstraction mechanisms (methods and classes) • Basic object-oriented programming
Problem Solving – The Software Development Process • Analysis – describe the user requirements and what the software does to satisfy them • Design – Show how the algorithms and data structures solve the problem • Implementation – Code the algorithms and data structures
Algorithm Development • Use expressions for arithmetic, comparisons, and other computations (+, *, <=, etc.) • Use control structures to sequence instructions ({}), make choices (if-else), and repeat processes (while, for) • Pseudocode provides a high-level means of expressing algorithms
Example: Newton’s Algorithm for Computing Square roots Newton’s algorithm uses successive approximations, where we get a better guess by taking the average of our current guess and the number divided by the guess. guess2 n Let guess = 1 Let tolerance = 0.001 While (Math.abs((guess * guess) – n) >= tolerance) Set guess = (guess + (n / guess)) / 2
Abstraction - Methods • A method hides an algorithm in a black box that can be called by name • Examples: Math.sqrt(2)Math.abs(-3) • Methods simplify algorithms by hiding detail
Implementing sqrt and abs public class Math{ static public double sqrt(double n){ double guess = 1; double tolerance = 0.001; while (Math.abs(guess * guess – n) >= tolerance) guess = (guess + (n / guess)) / 2; return guess; } static public double abs(double n){ if (n > 0) return n; else return –n; } // Other Math class methods go here } The reserved word static allows the method to be invoked with the class name, e.g., Math.abs(-3)
Implementing sqrt and abs public class Math{ static public double sqrt(double n){ double guess = 1; double tolerance = 0.001; while (Math.abs(guess * guess – n) >= tolerance) guess = (guess + (n / guess)) / 2; return guess; } static public double abs(double n){ if (n > 0) return n; else return –n; } // Other Math class methods go here } Method signatures (visible to clients)
Implementing sqrt and abs public class Math{ static public double sqrt(double n){ double guess = 1; double tolerance = 0.001; while (Math.abs(guess * guess – n) >= tolerance) guess = (guess + (n / guess)) / 2; return guess; } static public double abs(double n){ if (n > 0) return n; else return –n; } // Other Math class methods go here } Method implementations (hidden from clients)
Abstraction - Classes • A class groups together related methods (as in the Math class) • A class groups together related data and methods for operating on those data (as in the String class)
Abstraction - Classes • Classes hide information about the data and operations from users or clients • Implementers write the code for this information • Clients just know the interface of the class
What Is an Interface? • An interface is the set of public methods of a class • Examples: • Math: • static public double sqrt(double n) • String: • public int length() • public char charAt(int index)
Where Can I Find the Interfaces? • For standard Java classes, check the JDK’s documentation • Java’s classes are organized in packages, the most commonly used of which are java.lang and java.util • Implementers of non-standard classes should provide similar documentation
For Next Class Read Chapters 1-3 of the textbook