Methods CSC 171 FALL 2001 LECTURE 3. History The abacus.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Lecture 10 Methods COMP1681 / SE15 Introduction to Programming.
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 5 Functions for All Subtasks.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
©2004 Brooks/Cole Chapter 6 Methods. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using Methods We've already seen examples of using methods.
Classes and Objects CSC 171 FALL 2004 LECTURE 2. LABS Labs start this week. Go to your 2 nd (Wed or Thurs) lab Expect something like the programming exercises.
Methods Liang, Chapter 4. What is a method? A method is a way of running an ‘encapsulated’ series of commands. System.out.println(“ Whazzup ”); JOptionPane.showMessageDialog(null,
Introduction to Computers and Programming Introduction to Methods in Java.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Methods CSC 171 FALL 2004 LECTURE 3. Methods Variables describe static aspects – “nouns” Methods describe dynamic aspects – “verbs”, “behaviors” – Methods.
SELECTION CSC 171 FALL 2004 LECTURE 8. Sequences start end.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
Introduction to Methods
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Interfaces.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab.
Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
State Design Pattern. Behavioral Pattern Allows object to alter its behavior when internal state changes Uses Polymorphism to define different behaviors.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
Java and C++ Transitioning. A simple example public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello.
Department of Computer Engineering Methods Computer Programming for International Engineers.
Computer Science 112 Fundamentals of Programming II.
THINKING PROCEDURALLY OR CONCURRENT (SIMULTANEOUSLY OR SIDE BY SIDE OR IN LOGICAL ORDER A- IDENTIFY THE COMPONENTS OF A PROBLEM B-IDENTIFY THE COMPONENTS.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
Programming for Interactivity Professor Bill Tomlinson Tuesday & Wednesday 6:00-7:50pm Fall 2005.
Testing It is much better to have a plan when testing your programs than it is to just randomly try values in a haphazard fashion. Testing Strategies:
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
Staples are our staple Building upon our solution.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Examples of Classes & Objects
AKA the birth, life, and death of variables.
Department of Computer Science
Sum of natural numbers class SumOfNaturalNumbers {
Interface.
Building Java Programs
Systems of Computation
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Starting Out with Java: From Control Structures through Objects
Introduction to Computer Programming
Classes & Objects: Examples
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
class PrintOnetoTen { public static void main(String args[]) {
METHODS, CLASSES, AND OBJECTS A FIRST LOOK
COM-152: Computer Programming Types, Variables, Operators Part 1 of 2
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Scope of variables class scopeofvars {
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
CS149D Elements of Computer Science
Building Java Programs
CPS125.
Introduction to Methods and Interfaces
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Methods CSC 171 FALL 2001 LECTURE 3

History The abacus

History The Father of Computing Charles Babbage The Difference Engine First Government grant Babbage’s ideas were not widley accepted because of poor documentation

Methods Variables describe static aspects – “nouns” Methods describe dynamic aspects – “verbs”, “behaviors” – Methods “do things”

Methods We have seen some assignment statements where we do the computation in the program – int x = a + 5; – String b = “Hello” + “ “ + “World\n”;

Methods We have also had some occasion to use methods that do computation “elsewhere” – double d1 = Math.sqrt(50.9);

Methods Think of a method as a “black box” – We put something in – We get something out Putting something in – parameters Getting something out – Return values

Black Box Math sqrt() double x = Math.sqrt(9.0);

A Computer Program public class myFirstProg { public static void main(String args[]){ System.out.println(“Hello, CSC171”); }

Black Box MyFirstProg main() void String args[] “side effects”

A Computer Program public class mySecondProg { public static void main(String args[]){ int x = 5, y = 8; int product = mymult(x,y); System.out.println( x + “ * “ + y + “ is “ + product); } public static int mymult(int n1, int n2){ return n1 * n2; }

Black Box MyFirstProg main() void String args[] mymult()

Sequence of events main() mymult() System.out.println() 5,8 40 “5 * 8 is 40” void

Proceedural Abstraction We define the input and output behavior – Contract We embed some complex behavior in a method. We refer to the method by a name We can write & debug the method once & then use it whenever we want.

public class myThirdProg { public static void main(String args[]){ int x = 5, y = 8; int product = mymult(x,y); System.out.println( x + “^3 * “ + y + “ is “ + product); } public static int mymult(int n1, int n2){ return mycube(n1) * n2; } public static int mycube(int x){ return x * x * x; } }

Sequence of events main() mymult() System.out.println() 5, “5^3 * 8 is 1000” void mycube() 5 125

Interfaces – needed for project Interfaces are ways of “writing contracts” – Client communicates the input & output requirements Method names Parameters for each method Return types for each method

Interface definition public interface myMinMax { public int myMin(int x, int y); // no body public int myMax(int x, int y); // no body }

Interface use public class myProject implements myMinMax { public int myMin(int x, int y) { if (x < y) return x ; else return y; } public int myMax(int x, int y){ if (x>y) return x; else return y; }