Bryce Canyon, Utah CSE 114 – Computer Science I Objects and Reference.

Slides:



Advertisements
Similar presentations
1 Value vs. reference semantics. Recall: Value semantics value semantics: Behavior where variables are copied when assigned to each other or passed as.
Advertisements

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland.
1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference l Parameter passing Classes, Objects, and Methods.
Classes, Objects, and Methods
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
COMP Classes Yi Hong May 22, Announcement  Lab 2 & 3 due today.
COMP More About Classes Yi Hong May 22, 2015.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Classes, Objects, and Methods
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Java and C++, The Difference An introduction Unit - 00.
CSE 114 – Computer Science I Strings, I/O, and Methods Bonneville Salt Flats, Utah.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
COMP Mid-Term Review Yi Hong May 27, 2015.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
CSE 114 Computer Science I Objects Lake Superior, Michigan.
Day 5. Task: Implement your vending machine code into the program with JOptionPane I/O (modify it) it to me.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
COMP 150: Introduction to Object-Oriented Programming Lecturer: Dr. AJ Bieszczad 4-1 Chapter 4 l Class and Method Definitions l Information Hiding and.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Objects and Classes Mostafa Abdallah
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
COMP 110 Worksheet review, debugger Luv Kohli September 29, 2008 MWF 2-2:50 pm Sitterson 014.
COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
SEEM Java – Basic Introduction, Classes and Objects.
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.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, 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.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
Utilities (Part 2) Implementing static features 1.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Catie Welsh March 4,  Midterm on Monday, March 14th ◦ Closed books, no notes, no computer  No office hours during Spring Break ◦ However, I will.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
1 reading: 3.3 Using objects. Objects So far, we have seen: methods, which represent behavior variables, which represent data (categorized by types) It.
Topic: Classes and Objects
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 21, 2011
CS 302 Week 11 Jim Williams, PhD.
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Pass by Reference, const, readonly, struct
Classes, Objects, and Methods
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Building Java Programs
CSE 214 – Computer Science I More on Linked Lists
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
CSS161: Fundamentals of Computing
Defining Classes and Methods
In this class, we will cover:
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
References Revisted (Ch 5)
Classes and Objects Object Creation
Corresponds with Chapter 5
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Bryce Canyon, Utah CSE 114 – Computer Science I Objects and Reference

Classes Class—definition of a kind of object Like an outline or plan for constructing specific objects Class specifies what kind of data objects of that class have –Each object has the same data items but can have different values Class specifies what methods each object will have –All objects of the same class have the same methods available to them

Class as an Outline Class Name: Automobile Data: amount of fuel ________ speed ________ license plate ________ Methods (actions): increaseSpeed: How: Press on gas pedal. stop: How: Press on brake pedal. Class Definition Objects that are instantiations of the class First Instantiation: Object name: patsCar amount of fuel: 10 gallons speed: 55 miles per hour license plate: “135 XJK” Second Instantiation: Object name: suesCar amount of fuel: 14 gallons speed: 0 miles per hour license plate: “SUES CAR” Third Instantiation: Object name: ronsCar amount of fuel: 2 gallons speed: 75 miles per hour license plate: “351 WLF”

Objects Variables that are named instances of a class –the class is their type Have both data and methods –called members of the object Data items are also called fields or instance variables Invoking a method means to call the method, i.e. execute the method. Ex: –objectVariableName.method() –objectVariableName is the calling (invoking) object

Containment A class contains another class if it instantiates an object of that class –“HAS-A” PairOfDice HAS-A Die

PairOfDice dice Die die1 int upValue int numFaces Die die2 int upValue int numFaces RollGames main method Object Variables as Instance Variables for other Objects

Primitive Types vs. Objects (Class Types) Primitive variables: –assigned a memory location when declared –variable’s data stored there Object variables –assigned a memory location when it is declared –address of where the object data will be is stored there –address starts as null assigned when object variable is constructed

new Used when constructing an object Asks the JVM for a block of memory What for? –to store the instance variables of the object What does it return? –the address (#) of the memory block

Primitive vs. Class - Example int sum = 0; Die die1; MEMORY die1 null MEMORY sum0 die1.roll(); // CAN'T DO THIS! // die1 hasn’t been constructed Before object variables are constructed, they store the address null (0) Syntax or Runtime Error

Primitive vs. Class – Example (cont’d) die1 = new Die(8,3); die1.roll(); // Now it’s OK to roll MEMORY die1 upValue numFaces 3 8 etc… die1 stores an address, which is the address of the beginning of the memory block where the object’s data is

== and Objects Die die1 = new Die(8,4); Die die2 = new Die(8,4); if (die1==die2) FALSE! if (die1.equals(die2)) SHOULD BE TRUE! DEPENDING ON equals DEF. MEMORY etc… die1 upValue numFaces die2 upValue numFaces etc…

.equals and Objects equals should return true when the two objects have equivalent state (instance variable) values // INSIDE Die class public boolean equals(Die otherDie) { return ( (upValue == otherDie.upValue) && (this.numFaces==otherDie.numFaces) ); } this is optional, but what is it?

Call-by-value Revisited Note: –method arguments are copies of the original data Consequence? –methods cannot assign (‘=’) new values to arguments and affect the original passed variables Why? –changing argument values changes the copy, not the original

Java Primitives Example public static void main(String[] args) { int a = 5; int b = 5; changeNums(a, b); System.out.println(a); System.out.println(b); } public static void changeNums(int x, int y) { x = 0; y = 0; } Output? 5 5

Java Objects (Strings) Example public static void main(String[] args) { String a = "Hateful"; String b = "Career Opportunities"; changeStrings(a, b); System.out.println(a); System.out.println(b); } public static void changeString(String x, String y) { x = "The Magnificent Seven"; y = "The Magnificent Seven"; } NOTE: When you pass an object to a method, you are passing a copy of the object’s address Output? Hateful Career Opportunities

How can methods change local variables? By assigning returned values Ex, in the String class: –substring method returns a new String String s = "Hello"; s.substring(0, 4); System.out.println(s); s = s.substring(0, 4); System.out.println(s); Output? Hello Hell

Parameter Passing Revisited Primitive variables are passed using call-by-value. –a copy of the value is sent to the method –any change to that value in the method does NOT affect the original primitive variable What about with objects? –a copy of the address is sent –any changes made via mutator methods can change the original object –Danger: Reconstructing an object parameter for a method does not reconstruct the originally passed variable

swap a die x y upValue numSides Example Method: public void swap(int x, Die y) { int temp = x; x = y.getUpValue(); y.setUpValue(temp); } Example Method Call: int a = 3; Die die = new Die(6,1); swap(a,die); System.out.print("a is " + a); System.out.print(", die is " + die.getUpValue()); Parameter Passing Revisited (cont’d) OUTPUT: a is 3, die is 3 x and y are temporary variables, and are stored in different places in memory than a and die 31

Changing passed parameters public class ParameterPassing { public static void change (int idNum, String name, StringBuffer surname) { idNum = 15; name = "Oscar"; surname.replace(0, 4, "Jobs"); } public static void main(String[] args) { int id = 0; String fName = "Steve"; StringBuffer lName = new StringBuffer("????"); change(id, fName, lName); System.out.println(fName + " " + lName + " is employee # " + id); } OUTPUT:Steve Jobs is employee # 0 What is the output? These are only temporary variables

What’s happening in memory? … chars[0]"O" chars[4]"r" …… "J" "s" 15 … change 0idNum surname name … … 0id lName fName … … … MEMORY main chars[0] "S" chars[4] "e" … … chars[0]"?" chars[3]"?" …… … … … MEMORY … …

= and Objects = Assigns an ADDRESS TO OBJECT VARIABLE! Die die1 = new Die(8,4); Die die2; die2 = die1; Doesn't make a new copy of object! Now die1 and die2 both refer to the same object! die1.roll(); Causes same change in die1 AND die2 MEMORY die2 upValue numFaces 3 8 die1 etc… 6