Defining Your Own Classes II

Slides:



Advertisements
Similar presentations
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Lecture 2 Classes and objects, Constructors, Arrays and vectors.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
CS180 Review Questions. Administriva Final Exam –Friday 8am MTHW 210 –No GUI programming question –Format 35 multiple choice questions 4 programming.
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.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Review of C++ Programming Part II Sheng-Fang Huang.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
ECE122 Feb. 22, Any question on Vehicle sample code?
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
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 ◦
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
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.
Classes - Intermediate
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
CS180 Recitation Chapter 7: Defining Your Own Classes II.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Yan Shi CS/SE 2630 Lecture Notes
Topic: Classes and Objects
Learning Objectives Pointers as dada members
3 Introduction to Classes and Objects.
Static data members Constructors and Destructors
Examples of Classes & Objects
CSE 8A Lecture 17 Reading for next class: None (interm exam 4)
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
Methods.
CS 302 Week 11 Jim Williams, PhD.
Modern Programming Tools And Techniques-I Inheritance
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 6 Methods: A Deeper Look
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
Defining New Types of Objects, part 3
Classes & Objects: Examples
Group Status Project Status.
Classes, Encapsulation, Methods and Constructors (Continued)
Introduction to Classes and Objects
Building Java Programs
CS2011 Introduction to Programming I Methods (II)
JAVA Constructors.
Building Java Programs
7 Arrays.
9-10 Classes: A Deeper Look.
Object Oriented Programming in java
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Class.
Java Programming Language
In this class, we will cover:
COP 3330 Object-oriented Programming in C++
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Review for Midterm 3.
Constructors & Destructors
9-10 Classes: A Deeper Look.
Classes and Objects Systems Programming.
SPL – PS3 C++ Classes.
Presentation transcript:

Defining Your Own Classes II CS180 Recitation Defining Your Own Classes II

Announcements Project 5 is out Exam 2 weeks to finish. Milestone due on Wednesday. Exam Handed back today. Class next week Prof. Prabhakar is traveling. Class will be held at the same time. Lectures will be given by Barry Whitman on Monday and Prof. Clifton on Wednesday.

Exam Postview Which of the following is NOT a 'type' in Java? void. int. Integer. Static. * (keyword, not a type)

Exam Postview What is the output of the following code if the input string is “CS 180”. Scanner scanner = new Scanner(); String str = scanner.next(); System.out.print(str); CS180. CS. CS 180. The above code fragment does not compile.* Scanner needs argument.

Exam Postview What is the value of y when this code is executed? int x=4; int y = Math.ceil(x % 5 + x / 5.0); 1. 6. 5. * precedence rules work left to right 4.

Exam Postview int n=20; int p = n+5; int q = p-10; int r = 2 * (p-q); switch(n) { case p: n = n+1; case q: n = n+2; case r: n = n+3; default: n = n+4; } Does not compile!

Exam Postview int x = 5, y = 5, z = 5; if (x > 3) if(y > 4) if(z > 5) z += 1; else z += 2; z +=3; z += 4; z == 11!

Exam Postview Public class Test { public static void main(String[] args) { private static final int value = 5; float total; total = value + value / 2; System.out.println(total); } Will not compile. Private variables cannot be declared insides a method body.

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() { x=0; } public A(int x) { this.x = x; } … } public class A { private int x; public A() { this(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) {…}

Constructors Recall: Constructors are used to initialize member variables. If you don't write a constructor, Java will provide a default constructor for your class. Possible to write multiple constructors. Public A() Public A(int x) // use x as some initial value Public A(A a) // copy constructor Providing any constructor results in Java not providing the default constructor.

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 A a1 = new A(); A a2 = new A(2); a1 = a2; a2.set(3); 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()?

Memory Management public class A { private int x; A a1 = new A(); 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()?4 a2.get()?4 a3.get()?3

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 void set(int x) { this.x = x; } public int get() { return x; } } A a1 = new A(); A a2 = new A(2); a1 = a2; a1.set(3); A a3 = new A(a1); a1.set(4); a3.set(1); a1.get()? a2.get()? a3.get()?

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 void set(int x) { this.x = x; } public int get() { return x; } } A a1 = new A(); A a2 = new A(2); a1 = a2; a1.set(3); A a3 = new A(a1); a1.set(4); a3.set(1); a1.get()?1 a2.get()?1 a3.get()?1

null keyword Indicates the non-existence of an object (think address 0x00000000) 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()?

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()?4 a2.get()?0 a3.get()?0

Pass-By-Value Parameters in Java are passed by value (as opposed to pass by reference). 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()?

Pass-By-Value Parameters in Java are passed by value (as opposed to pass by reference). 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()?4

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 */ Uses @ for special information Author Parameter Return As an example, look at the Java 5 API on the class webpage.

Quiz What is the difference between an instance variable and a class variable?