Week 8 - Friday CS 121.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Introduction to C Programming
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Chapter 5 Part 1 COSI 11a Prepared by Ross Shaull.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
©2004 Brooks/Cole Chapter 6 Methods and Scope. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 A Method Can Be Viewed as a Black Box To use a.
COMP More About Classes Yi Hong May 22, 2015.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
COSC 1P02 Introduction to Computer Science 3.1 Cosc 1P02 Week 3 Lecture slides Birthdays are good for you. Statistics show that the people who have the.
Parameters. Overview A Reminder Why Parameters are Needed How Parameters Work Value Parameters Reference Parameters Out Parameters.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
Introduction to Programming Writing Java Beginning Java Programs.
Week 6: Functions - Part 2 BJ Furman 01OCT2012. The Plan for Today Comments on midterm exam (next week in lab!) Review of functions Scope of identifiers.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Chapter 6 Methods Chapter 6 - Methods.
Week 8 - Friday.  What did we talk about last time?  Static methods.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Week 10 - Friday.  What did we talk about last time?  References and primitive types  Started review.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Review – Primitive Data What is primitive data? What are 3 examples of primitive data types? How is a piece of primitive data different from an object?
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Week 2 - Wednesday CS 121.
Functions Chapter 5 CS12 - Computer Programming 1 Chapter 5.
Building Java Programs
Chapter 5 Conclusion CIS 61.
Week 15 – Wednesday CS 121.
CompSci 230 Software Construction
Week 9 - Monday CS 121.
Control structures Chapter 3.
PHP Functions, Scope MIS 3501, Fall 2015 Jeremy Shafer
Chapter 4 Procedural Methods.
CSC 253 Lecture 8.
Static Methods 14-Nov-18.
Lecture 11 C Parameters Richard Gesick.
CSC 253 Lecture 8.
CS 1110 Final Exam: Review Session 1 Drawing frames for calls, executing method calls Biggest issue!!! You can’t do questions on this topic correctly.
Introduction to Computer Programming Counting Loops 2
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Passing Parameters by value
Building Java Programs
Dr. Bhargavi Dept of CS CHRIST
CS2011 Introduction to Programming I Arrays (II)
CS2011 Introduction to Programming I Methods (II)
CS 1110 Final Exam: Review Session 1 Drawing frames for calls, executing method calls Biggest issue!!! You can’t do questions on this topic correctly.
Chapter 7 Procedural Methods.
Building Java Programs
Building Java Programs
Lecture 8-2: Object Behavior (Methods) and Constructors
Methods/Functions.
Corresponds with Chapter 5
Building Java Programs
Functions Maria Novosolov.
Week 7 - Monday CS 121.
Methods Coding in Java Copyright © Curt Hill.
Week 7 - Friday CS 113.
Presentation transcript:

Week 8 - Friday CS 121

Last time What did we talk about last time? Static methods

Questions?

Project 3

Calling Static Methods

Calling syntax Proper syntax for calling a static method gives first the name of the class that the method is in, a dot, the name of the method, then the arguments If the method is in the same class as the code calling it, you can leave off the Class. part If it is a value returning method, you can store that value into a variable of the right type Class.name(arg1, arg2, arg3);

Scope Variables from outside of the method don’t exist unless they have been passed in as parameters No matter how complex a program is, inside this method, only x, y, and z variables exist public static int add(int x, int y){ int z = x + y; return z; }

Binding A magical process called binding happens which copies the values from the calling code into the parameters of the method The calling code can use variables with the same names, with different names, or even just literals The method does not change the values of the variables in the original code Remember, it only has copies of the values

Binding example No connection between the two different x’s and y’s int a = 10; int x = 3; int y = add( 5, a ); //y contains 15 now No connection between the two different x’s and y’s public static int add(int x, int y){ int z = x + y; //5 + 10 return z; }

Static method rules Start a method with the keywords public static Next comes the return type Then the name of the method Then, in parentheses, the arguments you want to give to the method Inside braces, put the body of the method Include a return statement that gives back a value if needed

Static Method Examples

Distance Finding the Euclidean distance between two points is very important There are applications in: Route planning Graphic design software Video game AI Let’s make a method Formula:

Star drawing Drawing a star is more complicated than drawing a rectangle For a five pointed star, we need 5 points Let’s put it into a method so that we can do it repeatedly We want the method to take a scale, a starting point, and a color 108°

Lab 8

Upcoming

Next time… 2D arrays Overloading methods More method practice

Reminders Keep reading Chapter 8 of the textbook Keep working on Project 3