COMP 110 Objects and references Luv Kohli October 8, 2008 MWF 2-2:50 pm Sitterson 014.

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

Written by: Dr. JJ Shepherd
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
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.
Catie Welsh March 2,  Program 3 due tonight by 11:59pm  Lab 5 due Friday by 1pm  Sample Midterm is posted on course website ◦ Solutions will.
Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 41 Defining Classes and Methods Chapter 4.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
COMP 110 Objects and References Tabitha Peck M.S. February 27, 2008 MWF 3-3:50 pm Philips
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS180 Chapter 4 2/1/08. Announcements Project 3 is out –2 weeks due to the exam Exam –Thursday, February 7, 8:30 – 9:30 pm, WTHR 104 –Covers chapters.
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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.
Final Exam Review Closed book Closed laptop One sheet of notes permitted SE-0010 Dr. Mark L. Hornick 1.
Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner.
COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CSC 212 Object-Oriented Programming and Java Part 2.
February 28, 2013 COMP Introduction to Programming Objects and References Haohan Li TR 11:00 – 12:15, SN 011 Spring 2013.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
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 Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
Conditionals Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zPass by value limitations.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014.
Written by: Dr. JJ Shepherd
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014.
Exam Review 10/01/2014 Happy October. The Exam  Will be in Canvas  Two parts  Part A is recall – closed book, closed notes ◦Quizzes, in class activity.
Catie Welsh March 4,  Midterm on Monday, March 14th ◦ Closed books, no notes, no computer  No office hours during Spring Break ◦ However, I will.
COMP 110 More arrays, 2D arrays, Program 4 Luv Kohli November 10, 2008 MWF 2-2:50 pm Sitterson 014.
COMP 110 Arrays Luv Kohli November 5, 2008 MWF 2-2:50 pm Sitterson 014.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
COMP 110 Augustus Gloop, Augustus Gloop… Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
COMP 110 Some more about objects and references Luv Kohli October 10, 2008 MWF 2-2:50 pm Sitterson 014.
Midterm preview.
Computer Organization and Design Pointers, Arrays and Strings in C
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 7 User-Defined Methods.
COMP Objects and References
COMP 110 Review for midterm exam
Computers & Programming Languages
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Chapter 2 Programming Basics.
Names of variables, functions, classes
Happy October Exam Review 10/01/2014.
Variables and Constants
Announcements Lab 5 due Wednesday at noon.
Presentation transcript:

COMP 110 Objects and references Luv Kohli October 8, 2008 MWF 2-2:50 pm Sitterson 014

Announcements 2

Questions? 3

Today in COMP 110 Discuss Lab 4 Objects and references 4

Lab 4 Averages should be represented as floating-point numbers Variables such as totalGames, gamesOver90 should be integers else does not work for min/max unless you do it a certain way 5

Lab 4 Took off a point if loop did not handle the case of 0 games properly ◦ Took off another point if the sentinel value was used in your calculations Lenient this time around on comments if code was written clearly Took off a point for incorrect submission Next time, be careful with indentation/whitespace 6

Assignment statements as values int totalGames; int gamesOver90; int scoreSum; totalGames = gamesOver90 = scoreSum = 0; The value of the expression scoreSum = 12; is 12. 7

Review Classes Objects Instance variables Methods ◦ Return types ◦ Parameters and arguments Information hiding and encapsulation ◦ public/private ◦ accessors/mutators 8

Variables of a class type Behave differently from variables of a primitive type 9

Variables of a primitive type When declaring a variable, a certain amount of memory is assigned based on the declared primitive type What goes in this memory? 10 int age ; double length ; char letter ; memory

Variables of a primitive type A data value is stored in the location assigned to a variable of a primitive type 11

Variables of a class type What goes in these variables? 12 Student jack ; String inputString ; memory

Variables of a class type Contain the memory address of the object named by the variable ◦ NOT the object itself What is an address? Object is stored in some other location in memory The address to this other location is called a reference to the object Class types are also called reference types 13

Example: Books Assume we have a class named Book Book jacksBook = new Book(“Java”); Book apusBook = new Book(“Java”); vs. Book jacksBook = new Book(“Java”); Book apusBook = jacksBook; 14

Objects in memory 15 jacksBook apusBook ???? Memory Book jacksBook; Book apusBook; jacksBook = new Book(“Java”); apusBook = new Book(“Java”); jacksBook.setPage(137); apusBook.setPage(253); apusBook = jacksBook; apusBook.setPage(509); jacksBook is now on p. 509! ???????? ? Java ? Java ? Java ? Java ? Java 137 Java 253 Java 137 Java 253 Java ? ???? ?

Remember Variables of a class type contain memory addresses ◦ NOT objects themselves 16

== vs. equals() for Strings explained String is a class type What happens when you have String s1 = new String(“Hello”); String s2 = new String(“Hello”); boolean strEqual = (s1 == s2); strEqual is false! Why? s1 and s2 store different addresses! 17

== vs. equals() for Strings explained What happens when you have String s1 = new String(“Hello”); String s2 = new String(“Hello”); boolean strEqual = (s1.equals(s2)); strEqual is true! Why? String’s.equals() method checks if all the characters in the two Strings are the same 18

Writing the.equals() method public class Book { private String name; private int page; public boolean equals(Book book) { return (this.name.equals(book.name) && this.page == book.page); } 19

.equals() Every class has a default.equals() method if it is not explicitly written ◦ Does not necessarily do what you want You decide what it means for two objects of a specific class type to be considered equal ◦ Perhaps books are equal if the names and page numbers are equal ◦ Perhaps only if the names are equal ◦ Put this logic inside.equals() method 20

Parameters of a primitive type public void increaseNum(int num) { num++; } public void doStuff() { int x = 5; increaseNum(x); System.out.println(x); } Prints 5. Why? num is local to increaseNum method; does not change x 21

Parameters of a class type public void changeBook(Book book) { book = new Book(“Biology”); } public void doStuff() { Book jacksBook = new Book(“Java”); changeBook(jacksBook); System.out.println(jacksBook.getName()); } Prints Java. Why? book is local to changeBook, does not change jacksBook 22

Parameters of a class type public void changeBook(Book book) { book.setName(“Biology”); } public void doStuff() { Book jacksBook = new Book(“Java”); changeBook(jacksBook); System.out.println(jacksBook.getName()); } Prints Biology. Why? book contains the same address as jacksBook! 23

Friday Some more information on objects and references DecimalFormat Help with Lab 5 24