Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
Introduction to Computers and Programming Introduction to Methods in Java.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
CS 106 Introduction to Computer Science I 02 / 25 / 2008 Instructor: Michael Eckmann.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
CS 201 Functions Debzani Deb.
1 Memory Model of A Program, Methods Overview l Closer Look at Methods l Memory Model of JVM »Method Area »Heap »Stack l Preview: Parameter Passing.
 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
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 4.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
CSE 131 Computer Science 1 Module 1: (basics of Java)
Methods. 2 A sequence of statements can be packaged together as a unit and re-used. A method is a named unit of re-usable code. modifier returnType methodName(
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Procedural programming in Java Methods, parameters and return values.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 6.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Methods: A Deeper Look. Template for Class Definition public class { } A.Import Statement B.Class Comments C.Class Name D.Data members E.Methods (inc.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Building java programs, chapter 3 Parameters, Methods and Objects.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Methods CSCI 1301 What is a method? A method is a collection of statements that performs a specific task. Pre-Defined methods: available in Java library.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Methods.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
CS305j Introduction to Computing Parameters and Methods 1 Topic 8 Parameters and Methods "We're flooding people with information. We need to feed it through.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 7.
CS 106 Introduction to Computer Science I 10 / 10 / 2007 Instructor: Michael Eckmann.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 6.
Suppose we want to print out the word MISSISSIPPI in big letters.
Suppose we want to print out the word MISSISSIPPI in big letters.
Methods Chapter 6.
Programming Fundamentals Lecture #7 Functions
Chapter 6 Methods: A Deeper Look
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
Paul Ammann & Jeff Offutt
Group Status Project Status.
CS2011 Introduction to Programming I Methods (II)
Chapter 6 Methods.
METHODS, CLASSES, AND OBJECTS A FIRST LOOK
6 Methods: A Deeper Look.
Corresponds with Chapter 5
Methods and Data Passing
Presentation transcript:

Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5

Note Set 5 Overview Methods – In Depth Static methods Review of parameters Return values and the call stack

Method Allow you to break a program up in to self-contained units Process called modularization – to modularize your code Write once – use over and over Don’t reinvent the wheel – but make sure you build it well so it doesn’t pop Why modularize? Allow for the divide-and-conquer approach to problem solving Break it down into smaller pieces and solve each smaller problem Allows for greater software reusibility Build some blocks – then re-use them. If you repeat the same algorithm in your code 20 times and it is wrong…………….

Methods Should perform one task Should perform that task effectively Basic idea: Method invoked by a method call Method performs and completes its task Returns a result to the caller OR simply returns control of the program

Static Methods and Fields Static Methods do not require an object to perform its task Math.pow(…) – you don’t need an object of type Math Integer.parseInt() Double.parseDouble() yourClass.main() Static Field Not a data member of an object One doesn’t exist for each object instantiated Where (besides Math.PI and Math.E) have you seen static fields?

Example public class SampleClass { private int x; private int y; public static int z; //instance methods } public class TestSampleClass { public static void main(String[] args){ SampleClass s1, s2; //other stuff } xyxy xyxy s1s2 Notice: each object does not have a “z” data member

Why is Main Static? JVM can invoke main without having to first create an object When you execute java from command line, you type: java ClassName where ClassName contains the main that you want to execute But always starts in main – so you don’t have to specify Every class *can* contain a main method. jvm will execute the main that is in the class you indicate at command line.

Parameters Send data into a method can send multiple pieces of data public double findMax (double a, double b, double c) { int max = a; if(max < b) max = b; if(max < c) max = c; return max; } Remember: must be one argument for each parameter Arguments copied to parameters in order that they appear. double maxValue = findMax(5, 10, 3);

Method Calls in Method Calls Possible to embed one call inside another return Math.max(x, Math.max(y, z)); Must be evaluated first Result is used as 2 nd arg to outer call to max Excellent example of software reuse

Aside - Strings use + to concatenate Actually creates a new string object holding the concatenation Watch out for conversion/casting issues with strings. String s; s = "Hello"; s += " World"; System.out.println(s); s += " "; int y = 3; System.out.println(s + y + 3); System.out.println(s + (y + 3)); Hello World Hello World 33 Hello World 6

Stopping a function from executing No return statement – function just completes execution Think about mutator methods return; method can still have void return type. returns to the calling method return expression; must have a non-void return type. return-type is the data type of value being returned.

Understanding Calls - Stack Data Structure Understanding stacks is useful for understanding return Stacks are LIFO Last In – First Out Last value put in (pushed) is first value removed (pop) When method A calls method B, the system (jvm) must know where to return to in A when B is finished Activation Record – information on local variables in a method

Write Code Implement method printStars that will print a triangle of stars. It should accept an integer representing the number of lines used to make the triangle. The method should not print a triangle with more than 20 rows. Example triangle for input of 5: * * * * * * * * * * * Notice the space here

Class CircleGeometry Create Utility Class CircleGeometry with the following static methods distance – calculates the Euclidean distance between two points in a Cartesian plane. Accepts 4 integer values: x1, x2, y1, y2 circumference – calculates the circumference of a circle: Accepts 4 integer values: x1, x2, y1, y2; x represents center of circle, y represents point on edge of circle. area – calculates the area of a circle: Accepts 4 integer values: x1, x2, y1, y2; x represents the center of circle, y represents point on edge of circle