COMP 110 Some more about objects and references Luv Kohli October 10, 2008 MWF 2-2:50 pm Sitterson 014.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

Formatting Output For a pleasant interaction with the user.
1 Chapter 11 Introducing the Class Pages ( )
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.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable cost:1024.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
COMP 110 Objects and References Tabitha Peck M.S. February 27, 2008 MWF 3-3:50 pm Philips
COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
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 03 / 30 / 2007 Instructor: Michael Eckmann.
COMP 110 Errors, Strings, and Review Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
COMP 110 Information Hiding and Encapsulation Tabitha Peck M.S. February 25, 2008 MWF 3-3:50 pm Philips
11 Chapter 3 DECISION STRUCTURES CONT’D. 22 FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS We can use the DecimalFormat class to control.
Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
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.
Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.
CS1101: Programming Methodology Aaron Tan.
COMP 110 Objects and references Luv Kohli October 8, 2008 MWF 2-2:50 pm Sitterson 014.
Character Encoding & Handling doubles Pepper. Character encoding schemes EBCDIC – older with jumps in alphabet ASCII 1967 (7 bit)– Handled English, –ASCII.
February 28, 2013 COMP Introduction to Programming Objects and References Haohan Li TR 11:00 – 12:15, SN 011 Spring 2013.
Week 3 - Friday.  What did we talk about last time?  Operations on boolean values  !, &&, ||  Operations on char values  +, -  Operations on String.
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.
COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014.
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
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.
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.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
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 Augustus Gloop, Augustus Gloop… Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014.
COMP 110 More about classes Luv Kohli October 3, 2008 MWF 2-2:50 pm Sitterson 014.
Copyright © Texas Education Agency, Computer Programming Variables and Data Types.
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
User Interaction and Variables
Chapter 7 User-Defined Methods.
Formatting Output & Enumerated Types & Wrapper Classes
Crash course in the Java Programming Language
COMP 110 More arrays, 2D arrays, Program 4
Multiple if-else boolean Data
CS305J Introduction to Computing
COMP Objects and References
COMP 110 Review for midterm exam
Dialogues and Wrapper Classes
Object Oriented Systems Lecture 03 Method
COMP 110 More about inheritance
Boolean Expressions And if…else Statements.
Starting Out with Java: From Control Structures through Objects
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.
Java Lesson 36 Mr. Kalmes.
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
Michele Weigle - COMP 14 - Spr 04
CS360 Client/Server Programming Using Java
Announcements Lab 5 was due today Program 3 due Monday by 12pm
Announcements & Review
Chapter 3 Introduction to Classes, Objects Methods and Strings
Happy October Exam Review 10/01/2014.
Math class services (functions)
Michele Weigle - COMP 14 - Spr 04
Announcements Lab 5 due Wednesday at noon.
Presentation transcript:

COMP 110 Some more about objects and references Luv Kohli October 10, 2008 MWF 2-2:50 pm Sitterson 014

Announcements Midterm will not cover any new material discussed after today’s class, except for Lab 5 and today’s in-class exercise Ivan Sutherland talk on Monday, 4pm, Sitterson Hall 011 2

Questions? 3

Today in COMP 110 DecimalFormat Some more about objects and references 4

DecimalFormat import java.text.DecimalFormat; DecimalFormat df = new DecimalFormat(“0.00”); double number = ; System.out.println(df.format(number)); Output: “0.00” is the pattern that the format method will use to format its output ◦ Two digits after the decimal point, one digit before (but it will display all digits if more than one before) Fractional portion will be rounded 5

DecimalFormat DecimalFormat’s format method returns a StringBuffer, not a String, but you can still print out a StringBuffer See Appendix 6 (4 th edition) or Appendix 4 (5 th edition) for many more details Experiment with it on your own 6

Variables of a class type Contain the memory address of the object named by the variable ◦ NOT the object itself 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 7

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); } 8 Not quite the right way to write the equals method but okay for now

.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 9

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 10

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 11

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! 12

Monday 13