1 / 15

Computer Science 112

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

deepak
Télécharger la présentation

Computer Science 112

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  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 • 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

More Related