Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 112 Fundamentals of Programming II.

Similar presentations


Presentation on theme: "Computer Science 112 Fundamentals of Programming II."— Presentation transcript:

1 Computer Science 112 Fundamentals of Programming II

2 Who Am I? Dr. Lambert Office: Parmly 408 Phone: 8809 Email: klambert@wlu.edu Home Page: www.wlu.edu\~lambertk

3 What Did I Learn in CSCI 111? Problem solving techniques Algorithm development Abstraction mechanisms (methods and classes) Basic object-oriented programming

4 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

5 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

6 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. Let guess = 1 Let tolerance = 0.001 While (Math.abs((guess * guess) – n) >= tolerance) Set guess = (guess + (n / guess)) / 2 guess 2  n

7 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

8 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)

9 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)

10 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)

11 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)

12 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

13 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)

14 Where Can I Find the Interfaces? For standard Java classes, check the JDK’s documentation 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

15 For Next Class Read Chapters 1-3 of the textbook


Download ppt "Computer Science 112 Fundamentals of Programming II."

Similar presentations


Ads by Google