CS180 Recitation Chapter 7: Defining Your Own Classes II.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Introduction to Programming Lecture 39. Copy Constructor.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
More methods and classes, 4 of 4 Math 130 Introduction to Programming Lecture # 18 Monday, October 8, 2007.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
More on Objects CS 102 More, more, more on Objects.
Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.
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.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
CSE 115 Week 4 February 4 - 8, Monday Announcements Software installation fest Tuesday & Wednesday 4-7 in Baldy 21. Software installation fest Tuesday.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
1 More on Classes Overview l Overloading l The this keyword l The toString method l The equals method.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
CSC Programming I Lecture 8 September 9, 2002.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
The Java Programming Language
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
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.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Defining Classes II. Today’s topics  Static methods  Static variables  Wrapper classes  References  Class parameters  Copy constructor.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Chapter 9 Classes: A Deeper Look, Part I Part II.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CS1101 Group1 Discussion 7 Lek Hsiang Hui comp.nus.edu.sg
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 ◦
More Object Concepts— Farrell, Chapter 4 Dr. Burns.
Recitation 8 User Defined Classes Part 2. Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4);
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Chapter 7- Defining Your Own Classes Part 2 : Objectives After you have read and studied this chapter, you should be able to –Describe how objects are.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Defining Your Own Classes II
Yan Shi CS/SE 2630 Lecture Notes
Learning Objectives Pointers as dada members
CSE 8A Lecture 17 Reading for next class: None (interm exam 4)
Java Primer 1: Types, Classes and Operators
Class: Special Topics Copy Constructors Static members Friends this
Defining Classes II.
Building Java Programs
Defining New Types of Objects, part 3
slides created by Ethan Apter
Classes & Objects: Examples
CLASS DEFINITION (FIELDS)
slides created by Ethan Apter
Java Programming Language
Copy Constructors and Overloaded Assignment
SPL – PS3 C++ Classes.
More About Objects and Methods Chapter 5 Programming with Methods
Presentation transcript:

CS180 Recitation Chapter 7: Defining Your Own Classes II

Announcements Fall break next week –No lecture Monday –No labs all week –No mentoring session on Monday –No lab hours on Monday Project 5 is out –2 weeks again –Milestone is due next Wednesday despite Fall break Exam –Handed back –If you haven’t signed the academic integrity form yet, do so now!

this keyword this is a pointer to an object’s self Always used implicitly, but sometimes useful to be used explicitly In case of ambiguity resulting from using the same variable name twice, use this to refer to the class’s variable and not the local variable class A { private int a; public int add(int a) { return this.a + a; } A more clear version avoids ambiguity class A { private int a; public int add(int b) { return a + b; }

this keyword Use this keyword to call another constructor public class A { private int x; public A() { this(0); } public A(int x) { this.x = x; } … } public class A { private int x; public A() { x=0; } public A(int x) { this.x = x; } … }

Operation Overloading It is sometimes useful to have two methods of the same name but accept different parameters (very common in Math library) May not differ only by return type or variable name Constructors can be overloaded like other methods Valid: public String f(int x) {…} public String f(double x) {…} public String f(float f) {…} Invalid: public int f(int x) {…} public double f(int x) {…}

Copy Constructors and Memory Management Copy constructors are useful in making a deep copy of an object –Shallow copy: simple reference assignment with “=“ (what problems may crop up?) –Deep copy: copy over all values and objects (deep copy usually) explicitly into new memory

Memory Management public class A { private int x; public A() { x=0; } public A(int x) { set(x); } public A(A a) { x = a.get(); } public void set(int x) { this.x = x; } public int get() { return x; } } A a1 = new A(); A a2 = new A(2); a1 = a2; a2.set(3); A a3 = new A(a1); a1.set(4); a1.get()? a2.get()? a3.get()?

static keyword Useful for methods which do not need to access class variables or methods –Called using the class name –Think: Math library Used for class variables which are shared over instances of the class class A { private static int a; public void setA(int x) { a = x; } public int getA() { return a; } A a1 = new A(); A a2 = new A(); a1.setA(5); System.out.println(“a: “+a2.getA());

static Memory Management public class A { private static int x; public A() { x=0; } public A(int x) { set(x); } public A(A a) { x = a.get(); } public static void set(int x) { this.x = x; } public static int get() { return x; } } A a1 = new A(); A a2 = new A(2); a1 = a2; A.set(3); A a3 = new A(a1); a1.set(4); a3.set(1); a1.get()? a2.get()? a3.get()?

null keyword Indicates the non-existence of an object (think address 0x ) Can be used to represent any object type A null object can NOT call any methods –This will result in a runtime error called a NullPointerException –Get in the habit of checking against the null object!

null Memory Management public class A { private int x; public A() { x=0; } public A(int x) { set(x); } public A(A a) { x = a.get(); } public void set(int x) { this.x = x; } public int get() { return x; } } A a1 = null; a1 = new A(); A a2 = new A(a1); A a3 = null; if (a3 == null) a3 = new A(); else a3.set(3); a1.set(4); a1.get()? a2.get()? a3.get()?

Pass-By-Value public class A { private int x; public A() { x=0; } public A(int x) { set(x); } public A(A a) { x = a.get(); } public static void reset(A a) { a = new A(); } public void set(int x) { this.x = x; } public int get() { return x; } } A a1 = new A(); a1.set(4); A.reset(a1); a1.get()? Parameters in Java are passed by value (as opposed to pass by reference).

Javadoc Can provide a consistent form for comments in all your Java files Can be used to generate API-like html files for your classes Starts with /**, ends with */ for special information –Author –Parameter –Return

Packages Used to organize common Java classes together (like java.util) We will use packages later on in the semester