Using PARAMETERS In Java.

Slides:



Advertisements
Similar presentations
Rectangles moving and responding to the mouse. We want a window with a pile of rectangles in it When we click a rectangle it changes from filled to unfilled.
Advertisements

Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Made with love, by Zachary Langley Applets The Graphics Presentation.
Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.
BGI graphics library And Visual Studio.
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Variables and Operators
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Chapter 2 Programming by Example. A Holistic Perspective Three sections separated by blank lines. Program comment File: FileName.java
Chapter 5 Ch 1 – Introduction to Computers and Java Defining Classes and Methods 1.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Lists Introduction to Computing Science and Programming I.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
Java Programs u 1 project file –with an extension of.mcp –contains information that CodeWarrior needs to run the program u >= 1 source files –have an extension.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects Real and Java COMP.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
Visual C++ Programming: Concepts and Projects Chapter 6A: Methods (Concepts)
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
VARIABLES AND TYPES CITS1001. Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Scope.
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
* * 0 Chapter 6 Java Methods. * * 0 Method Syntax [access specifier][qualifier] return type method name(argument list) Access specifier public – method.
A way to pull together related data A Shape Class would contain the following data: x, y, height, width Then associate methods with that data A Shape.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
© A+ Computer Science - Chicken yeller = new Chicken();
Java Applets 1. What is an applet? An applet is a small Java program that is typically embedded in a Web page and can be run using the applet viewer or.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Agenda March 8 1.PowerPoint Presentation on Variables. For download:
Constructing Objects, the API and variables.  We are going to take the first 30 minutes of class for a design meeting  Each member of the team will.
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
Topics Instance variables, set and get methods Encapsulation
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Chapter 9 A Second Look at Classes and Objects - 2.
A structured walkthrough
OOP Powerpoint slides from A+ Computer Science
A Second Look at Classes and Objects - 2
CS106A, Stanford University
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
Basic Graphics Drawing Shapes 1.
AP Java Unit 3 Strings & Arrays.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
SSEA Computer Science: Track A
A+ Computer Science METHODS.
Functions Pass By Value Pass by Reference
Parameter Passing in Java
Topics discussed in this section:
A+ Computer Science METHODS.
February , 2009 CSE 113 B.
C Programming Pointers
Data Structures & Algorithms
Variables and Computer Memory
Dr. Sampath Jayarathna Cal Poly Pomona
Variables and Operators
A+ Computer Science PARAMETERS
Methods/Functions.
Introduction to Computer Science and Object-Oriented Programming
C Parameter Passing.
Presentation transcript:

Using PARAMETERS In Java

First, what is a reference? © A+ Computer Science - www.apluscompsci.com

References In Java, any variable that refers to (points to) an Object is a reference variable, where it is located. The variable stores the memory address of the actual Object. All variables in Java that refer to Objects are called references. Reference variables store the location / memory address of the actual Object. For most situations, the value stored in a reference is a memory address.

References x "Chuck" Name x = new Name("Chuck"); The variable x here refers to the Object created on the right – it points to the the Object (in its location) in memory – it ..holds the address. x In this example, x and y both the store the location / address of Chuck. There is only one String containing Chuck. There are two reference variables storing the location / address of Chuck. For this example, x==y is true. x==y compares the values stored in x and y. x and y both store the same location / address. For this example, x.equals(y) is true. x.equals(y)compares the contents of the Objects referred to by x and y. Chuck is being compare to Chuck. 0x2B3 0x2B3 "Chuck"

References y x "Chuck" Name x = new Name("Chuck"); Name y = x; x and y now store the same memory address. y x In this example, x and y both the store the location of Chuck. There is only one String containing Chuck. There are two reference variables storing the location / address of Chuck. For this example, x==y is true. x==y compares the values stored in x and y. x and y both store the same location / address. For this example, x.equals(y) is true. x.equals(y)compares the contents of the Objects referred to by x and y. Chuck is being compare to Chuck. 0x2B7 0x2B7 0x2B7 "Chuck"

References y x “Pilot" “Accord" Honda x = new Honda(“Accord"); Honda y = new Honda(“Pilot"); x and y – two different variables - store different memory addresses. Two different objects – two memory locations with two different variable names y x In this example, x stores the location / address of a String Object that stores the value Chuck. y also stores the location of a different String Object that stores the value Chuck. x and y do not store the same location / address. For this example, x==y is false. x and y do not store the same location / address. For this example, x.equals(y) is true. 0x2FE 0x2B7 0x2FE 0x2B7 “Pilot" “Accord"

References y x "Chuck" String x = "Chuck"; x holds 0x2B7 String y = "Chuck"; y holds 0x2B7 x = null; x now holds null y x In this example, x and y both the store the location / address of Chuck. There is only one String containing Chuck. There are two reference variables storing the location / address of Chuck. At the start, x==y is true. x is then referred to null. x now stores null. y was in no way changed. y still stores the address of Chuck. After changing the value of x, x==y is false. 0x2B7 0x2B7 0x2B7 null "Chuck"

What is a parameter? © A+ Computer Science - www.apluscompsci.com

Parameters Parameters are variables in a method signature parentheses. public int gator( int ali, int croc) { eater = ali + croc } a) Notice, they are variables, not actual values. b) These parameters also occupy a certain position. Notice that ali is first, then croc. Parameters are used to pass information to a method. Many methods need information in order to perform some operation. The parameters tell the method what to do and how to do it.

Parameters public int gator( int ali, int croc) { int eater = ali + croc; System.out.println(eater); } Animal.gator( 200, 400 ); arguments When “gator” is called here, the calling method has data -- called arguments – that is passed to the parameters, to be used inside the method . Animal calls gator, passing 200 and 400 to it, to use. It will print 600. Parameters are used to pass information to a method. Many methods need information in order to perform some operation. The parameters tell the method what to do and how to do it.

Parameters void setColor(Color theColor) the call passing “red” setColor() is a method of the Graphics class whose parameters receives a Color. void setColor(Color theColor) the call passing “red” window.setColor( Color.red ); Most, if not all, of the Graphics class methods require parameters. The parameters communicate to the Graphics methods information about what needs to be done. The setColor() method changes the current drawing color to the color passed in. setColor() cannot be called without a color parameter. method call: passing argument to parameter

Parameters void fillRect (int x, int y, int width, int height) window.fillRect( 10, 50, 30, 70 ); method call: arguments to parameters The fillRect() method requires four pieces of information. fillRect() needs an x value, a y value, a width, and a heigth. fillRect() will draw a filled rectangle on the window at x,y with height and width as stated by the parameters.

Parameters void fillRect(int x, int y, int width, int height) window.fillRect( 10, 50, 30, 70 ); The call to fillRect will draw a rectangle at position 10,50 with a width of 30 and a height of 70.