Computer Science 112 Fundamentals of Programming II.

Slides:



Advertisements
Similar presentations
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Advertisements

Methods Java 5.1 A quick overview of methods
PROBLEM SOLVING TECHNIQUES
Procedural programming in Java
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Introduction to Computers and Programming Introduction to Methods in Java.
Department of Computer Science University of Maryland, College Park
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
Introduction to Java Programming, 4E Y. Daniel Liang.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
1 Interactive Applications (CLI) and Math Interactive Applications Command Line Interfaces The Math Class Example: Solving Quadratic Equations Example:
1 Topic 04 Methods Programming II/A CMC2522 / CIM2561 Bavy Li.
 2003 Prentice Hall, Inc. All rights reserved Introduction Modules –Small pieces of a problem e.g., divide and conquer –Facilitate design, implementation,
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
University of Limerick1 Work with API’s. University of Limerick2 Learning OO programming u Learning a programming language can be broadly split into two.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Java: Chapter 1 Computer Systems Computer Programming II.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CSCI 125 & 161 Lecture 13 Martin van Bommel. Floating Point Data Floating point numbers are not exact Value 0.1 in binary is very close to 1/10, but not.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Chapter 1 Introduction to Computers and C++ Programming Goals: To introduce the fundamental hardware and software components of a computer system To introduce.
“The greatest crimes do not arise from a want of feeling for others but from an over-sensibility for ourselves and an over-indulgence to our own desires.”
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
Data Structures Chapter 1- Introduction Mohamed Mustaq.A.
Computer Science II 810:062 Section 01. How is CS I different from CS II? When you teach Java there are a series of decisions that have to be made…
SE-1010 Dr. Mark L. Hornick 1 Some Java Classes using & calling methodsS.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
How to Read Code Benfeard Williams 6/11/2015 Susie’s lecture notes are in the presenter’s notes, below the slides Disclaimer: Susie may have made errors.
Math Class Part of the Java.lang package. This package is from object so you do not have to import the lang package. Static: Math Class is static.
Introduction to Computer Programming CS 126 Lecture 4 Zeke Maier.
COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Salman Marvasti Sharif University of Technology Winter 2015.
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
The Math Class Methods Utilizing the Important Math Operations of Java!
1 Introduction  Algorithms  Data structures  Abstract data types  Programming with lists and sets © 2008 David A Watt, University of Glasgow Algorithms.
Modularity Computer Science 3. What is Modularity? Computer systems are organized into components called modules. The extent to which this is done is.
2.1 Functions. Functions in Mathematics f x y z f (x, y, z) Domain Range.
CSci 162 Lecture 8 Martin van Bommel. Large-scale Programming Up to now we have been writing relatively short programs to solve simple problem Want to.
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Methods CSC 171 FALL 2001 LECTURE 3. History The abacus.
COVERED BASICS ABOUT ALGORITHMS AND FLOWCHARTS
Computing Square Roots
Switch, Rounding Errors, Libraries
Anatomy of a Class & Method
Chapter 4: Writing Classes
Section 3.2c Strings and Method Signatures
Primitive Types Vs. Reference Types, Strings, Enumerations
Design by Contract Fall 2016 Version.
Higher-Order Procedures
Functions, Procedures, and Abstraction
MSIS 655 Advanced Business Applications Programming
IFS410 Advanced Analysis and Design
Chapter 5 Methods.
Functions, Procedures, and Abstraction
Introduction to Methods and Interfaces
Presentation transcript:

Computer Science 112 Fundamentals of Programming II

Who Am I? Dr. Lambert Office: Parmly 408 Phone: Home Page:

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

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