BM214E Object Oriented Programming Lecture 7

Similar documents
Computer Science II (20082) Week 1: Review and Inheritance

Introduction to Programming Using Java (98-388)

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

CS 106 Introduction to Computer Science I

Class, Variable, Constructor, Object, Method Questions

Java Fundamentals (II)

Lecture 10 Declarations and Scope

Array. Prepared By - Rifat Shahriyar

Computer Science II (20073) Week 1: Review and Inheritance

Chapter 4 Defining Classes I

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

BM214E Object Oriented Programming Lecture 6. Classes and Objects

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

MIDTERM REVIEW. midterminformation.htm

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

Garbage collec,on Parameter passing in Java. Sept 21, 2016 Sprenkle - CSCI Assignment 2 Review. public Assign2(int par) { onevar = par; }

Abstract Classes and Interfaces

BM214E Object Oriented Programming Lecture 8

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

CS-202 Introduction to Object Oriented Programming

System.out.print(); Scanner.nextLine(); String.compareTo();

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015

Abstract and final classes [Horstmann, pp ] An abstract class is kind of a cross between a class and an interface.

Declarations and Access Control SCJP tips

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Chapter 6 Introduction to Defining Classes

1 Shyam sir JAVA Notes

COP 3330 Final Exam Review

And Even More and More C++ Fundamentals of Computer Science

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

COE318 Lecture Notes Week 4 (Sept 26, 2011)

Chapter 4. Defining Classes I

About this exam review

Objects and Iterators

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

COE318 Lecture Notes Week 8 (Oct 24, 2011)

INHERITANCE. Spring 2019

C# Programming for Developers Course Labs Contents

CMSC 132: Object-Oriented Programming II

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Objects and Classes. Basic OO Principles. Classes in Java. Mark Allen Weiss Copyright 2000

Java Identifiers, Data Types & Variables

Comments are almost like C++

COMP 110/L Lecture 7. Kyle Dewey

CMSC 331 Second Midterm Exam

Recitation 3 Class and Objects

Chapter 3 Objects and Classes

Unit 5: More on Classes/Objects Notes

Rules and syntax for inheritance. The boring stuff

Chapter 9: A Second Look at Classes and Objects

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Arrays Classes & Methods, Inheritance

Object Oriented Programming

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Java Object Oriented Design. CSC207 Fall 2014

Lecture 7 Objects and Classes

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Java Programming Training for Experienced Programmers (5 Days)

Chapter 6 Classes and Objects

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Object Oriented Programming 2015/16. Final Exam June 28, 2016

Timing for Interfaces and Abstract Classes

Classes Classes 2 / 35

Implements vs. Extends When Defining a Class

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Lecture 7: Classes and Objects CS2301

Java Magistère BFA

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

ECE 122. Engineering Problem Solving with Java

Inheritance and Interfaces

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double("3.14"); 4... Zheng-Liang Lu Java Programming 290 / 321

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Object Oriented Modeling

CS112 Lecture: Defining Instantiable Classes

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

Ticket Machine Project(s)

Chapter 4. Defining Classes I

Object-Oriented Concepts

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

CS 106 Introduction to Computer Science I

Object Oriented Programming. Java-Lecture 6 - Arrays

CS 1331 Exam 1 ANSWER KEY

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

Language Features. 1. The primitive types int, double, and boolean are part of the AP

JAVA GUI PROGRAMMING REVISION TOUR III

Programming II (CS300)

Lecture 3. Lecture

Interfaces. An interface forms a contract between the object and the outside world.

Java: introduction to object-oriented features

Transcription:

BM214E Object Oriented Programming Lecture 7

References

References Revisited What happens when we say: int x; double y; char c;???

We create variables x y c Variable: Symbol plus a value

Assume that we have class Box class LLNode as well as the usual suspects: class String, etc.

What happens when we say Box b; LLNode head; String s;

We create variables b head Reference variables hold the location of objects. If they aren't referencing an object what is their value? s Box LLNode String Java keeps track of what type of object each reference can "point" to.

Common Errors! Box b = new Box(1, 2, 3);... Box b = new Box(4, 5, 6);...

Should be: Box b = new Box(1, 2, 3);... b = new Box(4, 5, 6);...

Common Errors! Box b = new Box(1, 2, 3); b = anotherbox;

Should be: Box b; b = anotherbox;

Common Errors! class Whatever { Box b; // Constructor public Whatever() { Box b = new Box(1, 2, 3);

Should be: class Whatever { Box b; // Constructor public Whatever() { b = new Box(1, 2, 3);

Understanding References Key to understanding Arrays of Objects Testing for Equality of Objects Passing Objects to Methods Assignment vs. Clone etc. For example...

Understanding Equality The key to understanding equality comes from knowing how Java handles references and primitives. First, here s the rule: When comparing primitives, use ==, thus: if ( icount == imaximum ) // etc. When comparing objects, use.equals(), thus: if ( box1.equals(box2) ) // etc. Now, let s consider WHY...

Testing Equality: Primitives Equality with Primitives: int x; int y; x = 11; y = 3; x: 11 y: 3 System.out.println(x == 11); System.out.println(y = = 3); System.out.println(x = = y); // prints true // prints true // prints false x = y; x: 3 y: 3 System.out.println(x = = 3); System.out.println(y = = 3); System.out.println(x == y); System.out.println(x > y); // prints true // prints true // prints true // prints false

Testing Equality: Objects 1. Are the references equal? Test with == In other words, are both references pointing to exactly the same object? Considered to be the most stringent test for equality

Testing Equality: Objects 2. Are the objects themselves equal? The answer depends on your definition of equality! Create an equals() method which compares the internal state of the object with another object passed in as a reference. You need to create this method for every class where you will need to compare objects for equality

Example class Rectangle { int len, wid; String name; // Constructor and basic get/set methods not shown public boolean equals(object o) { boolean retval = false; if(o instanceof Rectangle) { Rectangle r = (Rectangle)o; if(r.getlen() == len && r.getwid() == wid) retval = true; } return retval; }

Example public static void main(string args[]) { Rectangle r1 = new Rectangle(3,4,"Bob"); Rectangle r2 = new Rectangle(3,4,"Ted"); Rectangle r3 = new Rectangle(8,7,"Bob"); Rectangle r4 = r1; String s = "Goofy"; System.out.println(r1.equals(r2)); System.out.println(r1.equals(r3)); System.out.println(r1.equals(r4)); System.out.println(r1.equals(s)); } } // Rectangle

Recall the LLNode public boolean equals(object o) { boolean retval = false; if(o instanceof LLNode) { retval=getdata().equals(((llnode)o).getdata()); } return retval; }!!!?? data next data next

The LLNode public static void main(string args[]) { LLNode n4 = new LLNode("delta", null); LLNode n3 = new LLNode("gamma", n4); LLNode n2 = new LLNode("beta", n3); LLNode n1 = new LLNode("alpha", n2); LLNode tester = new Node("beta", null); } System.out.println(n1.equals(tester)); System.out.println(n2.equals(tester)); System.out.println(n3.equals(tester)); System.out.println(n4.equals(tester));

Note! The.equals() method in class Object is simply ==

Scope

Scope Local variables (declared as a part of method): Can be seen only from within their method. Outside of their method, their identifiers have no meaning. Instance and Class variables (declared as part of a class, but not within a particular method): Can be seen from anywhere in the instance. This means they can be seen from within methods defined within the class without passing them to the method as parameters. May or may not be visible beyond (soon). Within a method, local variable identifiers take precedence over instance variable IDers

Preventing Identifier Ambiguity What we want: class Person { String strname;... public void setname (String strname) { WRONG! strname = strname; } // of setname Inside the method, the String strname refers to the String in the method signature. This creates a problem: which strname is which?

Preventing Identifier Ambiguity What we get: class Person { String strname;... public void setname (String strname) { WRONG! strname = strname; } // of setname Inside the method, the String strname refers to the String in the method signature. This creates a problem: which strname is which?

Preventing Identifier Ambiguity Solutions: Rename the formal parameter: public void setname (String strnewstringname) { strname = strnewstringname; } // of setname Use the keyword this to refer to current object public void setname (String strname) { this.strname = strname; } // of setname Preferred with Javadocs

Shadowing

Shadowing Refers to "hiding" something that still exists Simple case today Will come back to this topic during discussion on inheritance

class Widget { Shadowing public String name; public void somemethod(int i) { String name; /* Shadows instance variable */ name = "Bob"; this.name = "Wally"; /* Accessing the shadowed variable! */

Chaining

Some Constructors class Rectangle { private int length, width; private String name; public Rectangle () { setlength(0); setwidth(0); setname("unnamed"); } // constructor public Rectangle (int l, int w, String n){ setlength(l); setwidth(w); setname(n); } // constructor public Rectangle (int l, int w) { setlength(l); setwidth(w); setname("unnamed"); } // constructor

Constructor "Chaining" class Rectangle { private int length, width; private String name; public Rectangle () { this(0,0,"unnamed"); } // constructor public Rectangle (int l, int w, String n){ setlength(l); setwidth(w); setname(n); } // constructor public Rectangle (int l, int w) { this(l, w,"unnamed"); } // constructor

Add a count? class Rectangle { private int length, width; private String name; private static count = 0; public Rectangle () { setlength(0); setwidth(0); setname("unnamed"); count++; } // constructor public Rectangle (int l, int w, String n){ setlength(l); setwidth(w); setname(n); count++; } // constructor public Rectangle (int l, int w) { setlength(l); setwidth(w); setname("unnamed"); count++; } // constructor

Add a count, simpler with "Chaining" class Rectangle { private int length, width; private String name; private static int count = 0; public Rectangle () { this(0,0,"unnamed"); } // constructor public Rectangle (int l, int w, String n){ setlength(l); setwidth(w); setname(n); count++; } // constructor public Rectangle (int l, int w) { this(l, w,"unnamed"); } // constructor

Arrays

Arrays An array may be declared to be: an array of primitives, or an array of objects (actually references!!!). The Array itself is an Object, even if the array contains primitives. (Array identifier references an object). If an array of objects, then: the array identifier is a reference to the array object each array element is a reference to an object of the class specified as elements Instantiating the array object does not instantiate the various element objects to which it refers. Element objects must be explicitly instantiated and initialized.

Example: Imagine a Flatware object which keeps track of: knives, forks and spoons. class Flatware { int knives = 0; int forks = 0; int spoons = 0; public Flatware(int knives, int forks, int spoons) { setknives(knives); setforks(forks); setspoons(spoons); } // of constructor /* We assume accessors, modifiers--getknives(), setknives(), etc. -- are implemented here */ } // of Flatware Arrays

Arrays Then, the code segment: Flatware fw1 = new Flatware(10, 20, 30); Flatware fw2; produces: fw1 fw2 knives = 10 forks = 20 spoons = 30

Arrays and the code segment: Flatware[ ] flatwarearray = new Flatware[5]; produces: flatwarearray which could then be initialized via: int i; for ( i = 0; i < flatwarearray.length; i++) { flatwarearray[i] = new Flatware(10, 20, 30); } // of for loop initialization

giving: Arrays fw1 fw2 flatwarearray knives = 10 forks = 20 spoons = 30 knives = 10 forks = 20 spoons = 30 knives = 10 forks = 20 spoons = 30 knives = 10 forks = 20 spoons = 30 knives = 10 forks = 20 spoons = 30 knives = 10 forks = 20 spoons = 30

Arrays but the code segment: int i; for ( i = 0; i < flatwarearray.length; i++) { flatwarearray[i] = fw1; } // of for loop initialization produces: flatwarearray fw1 knives = 10 forks = 20 spoons = 30

Review: Array Creation Declaration int myints[]; int[] myints; Instantiation myints = new int[10]; Declaration and instantiation: int[] myints = new int[10]; Access to individual element myints[3] = 9; x = myints[4]; Static initialization int[] myints = {1,2,5,6,7,4};

Multi-Dimensional Arrays int[][] mytwodimarray; mytwodimarray = new int[10][5]; Quiz Yourself! Valid? String[][] s; s = new String[10][]; Valid? s = new String[][10]; //YES! // NO!

Static multidimensional initialization: String s[][] = {{"Static", "multidimensional", "initialization", "of"}, {"arrays", "requires", "the", "use"}, {"of", "nested", "curlies"} }; Creates: Static multidimensional Initialization of arrays requires the use of nested curlies (null)

Arrays Summary of Arrays All arrays are objects (you have to declare, instantiate, and initialize) Arrays are either arrays of primitive elements (e.g. int) or arrays of objects (really references to objects) Arrays are statically sized: you can t change length after declaration; arrays know their own length Use Array.length in for loops to avoid off-by-one errors 2D arrays are arrays of arrays

Questions?