AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.

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

CSCI 160 Midterm Review Rasanjalee DM.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
 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.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs.
Writing Methods. Create the method Methods, like functions, do something They contain the code that performs the job Methods have two parts.
Do You Understand Methods and Parameters? In this section you will be shown 25 different programs. Most of these programs have some type of error. A few,
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 (part 2) Khalid Siddiqui.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
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.
Mixing integer and floating point numbers in an arithmetic operation.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Method Overloading.. Method Overloading Can two methods in a class have the same name? Two methods in a class can have the same name provided – they take.
The 9 th and 10 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/2/2012 and 10/3/2012 -Review loop structures and improve CheckISBN and CourseAverage.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
Building java programs, chapter 3 Parameters, Methods and Objects.
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.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Methods.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
05 Method Calling. 2 What is a Method? Declaring a Method Method Calling Method Call Stack Parameter Passing Pass by Value Outline.
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.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 7.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Staples are our staple Building upon our solution.
Department of Computer Science
Building Java Programs
Starting Out with Java: From Control Structures through Objects
Writing Methods.
Computing Adjusted Quiz Total Score
Lesson A4 – Object Behavior
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Group Status Project Status.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Functions Pass By Value Pass by Reference
class PrintOnetoTen { public static void main(String args[]) {
© A+ Computer Science - OOP Pieces © A+ Computer Science -
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Scope of variables class scopeofvars {
See requirements for practice program on next slide.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Building Java Programs
CIS 110: Introduction to Computer Programming
Presentation transcript:

AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java

AP Computer Science A – Healdsburg High School 2 Passing Parameters to Constructors and Methods Any expression that has an appropriate data type can serve as a parameter: double u = 3, v = - 4;... Polynomial p = new Polynomial (1.0, - (u + v), u * v); double y = p.getValue (2 * v - u); public class Polynomial { public Polynomial (double a, double b, double c) {... } public double getValue (double x) {... }...

AP Computer Science A – Healdsburg High School 3 Passing Parameters (cont’d) int is promoted to double when necessary: The same as: (3.0)... Polynomial p = new Polynomial (1, - 5, 6); double y = p.getValue (3); The same as: (1.0, -5.0, 6.0)

AP Computer Science A – Healdsburg High School 4 Passing Parameters (cont’d) Primitive data types are always passed “by value”: the value is copied into the parameter. double x = 3.0; double y = p.getValue ( x ); public class Polynomial {... public double getValue (double u) { double v;... } x: 3.0 u: 3.0 copy u acts like a local variable in getValue

AP Computer Science A – Healdsburg High School 5 Passing Parameters (cont’d) public class Test { public double square (double x) { x *= x; return x; } public static void main(String[ ] args) { Test calc = new Test (); double x = 3.0; double y = calc.square (x); System.out.println (x + " " + y); } x here is a copy of the parameter passed to square. The copy is changed, but the original x is unchanged. Output: 3 9

AP Computer Science A – Healdsburg High School 6 Passing Parameters (cont’d) Objects are always passed as references: the reference is copied, not the object. Fraction f1 = new Fraction (1, 2); Fraction f2 = new Fraction (5, 17); Fraction f3 = f1.add (f2); public class Fraction {... public Fraction add (Fraction f) {... } copy reference A Fraction object: num = 5 denom = 17 refers to the same object refers to

AP Computer Science A – Healdsburg High School 7 Passing Parameters (cont’d) A method can change an object passed to it as a parameter (because the method gets a reference to the original object). A method can change the object for which it was called (this object acts like an implicit parameter):

AP Computer Science A – Healdsburg High School 8 Passing Parameters (cont’d) Inside a method, this refers to the object for which the method was called. this can be passed to other constructors and methods as a parameter: public class ChessGame {... Player player1 = new Player (this);... A reference to this ChessGame object

AP Computer Science A – Healdsburg High School 9 public int fun(int x, int y) { y -= x; return y; } What is printed to the screen after the following code is executed? int a = 3, b = 7; b = fun(a, b); a = fun(b, a); System.out.println(a + " " + b); Example: Suppose the method fun is defined as: