Reference Details (copying, comparing). References You were introduced to the concept of pointers earlier this term. The pointer capabilities in pseudocode.

Slides:



Advertisements
Similar presentations
Step Up to Writing.
Advertisements

© 2005 by Prentice Hall Chapter 13 Finalizing Design Specifications Modern Systems Analysis and Design Fourth Edition Jeffrey A. Hoffer Joey F. George.
Traversing a Binary Tree Binary Search Tree Insertion Deleting from a Binary Search Tree.
Linked List: Traversal Insertion Deletion. Linked List Traversal LB.
Static Single-Assignment ? ? Introduction: Over last few years [1991] SSA has been Stablished as… Intermediate program representation.
The University of Adelaide, School of Computer Science
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Building a Linked List in Java. Linked List In the Procedural Paradigm a linked list consisted of: –A pointer to the head of the list –Nodes (in dynamic.
Analysis of programs with pointers. Simple example What are the dependences in this program? Problem: just looking at variable names will not give you.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
MIPS Function Continued
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
An Introduction to Sorting Chapter 8 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
1 Problem Solving Abstraction Oftentimes, different real-world problems can be modeled using the same underlying idea Examples: Runtime storage, Undo operation.
From: From:
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
PSUCS322 HM 1 Languages and Compiler Design II Parameter Passing Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
CS Winter 2011 Introduction to the C programming language.
Insertion into a B+ Tree Null Tree Ptr Data Pointer * Tree Node Ptr After Adding 8 and then 5… 85 Insert 1 : causes overflow – add a new level * 5 * 158.
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversion and Helper Modules.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
Object Matching With Faces CS460 Project Presentation By Sam Buyarski.
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 17, 2013 Carolyn Seaman University of Maryland, Baltimore County.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 17, 2013 Marie desJardins University of Maryland, Baltimore County.
MIPS function continued. Recursive functions So far, we have seen how to write – A simple function – A simple function that have to use the stack to save.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
The way someone or something acts; what they do a)behaviorbehaviorb)darknessdarknessc)illnessillnessd)silencesilence.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 12: Programming Project.
An Introduction to Sorting Chapter 8 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with.
© 2006 ITT Educational Services Inc. SE350 System Analysis for Software Engineers: Unit 10 Slide 1 Chapter 13 Finalizing Design Specifications.
Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.
Announcements Review problems Review Outline. Online Survey
Assembly Language for x86 Processors 7th Edition Chapter 13: High-Level Language Interface (c) Pearson Education, All rights reserved. You may modify.
The Software Development Process
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Review Problems. What is the Big O? i
Chapter 13 Finalizing Design Specifications
1 Chapter 6 Methods for Making Data Structures. 2 Dynamic Arrays in Data Structures In almost every data structure, we want functions for inserting and.
Lecture Notes 2 Biology A Eaton Rapids High School K.Coppins.
Subtraction Counting Down Draw then number line
Modules (Methods) Functions and Procedures Parameters...Output.
Random Logic l Forum.NET l State Machine Mechanism Forum.NET 1 st Meeting ● December 27, 2005.
Lists in Python Lists as Arguments/Parameters. Lists as arguments to functions Just like other data types, lists can be sent to functions as arguments.
Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.
Module 9: Operator overloading #1 2000/01Scientific Computing in OOCourse code 3C59 Module 9: Operator Overloading In this module we will cover Overloading.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
CSCE 240 – Intro to Software Engineering Lecture 3.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Credible Compilation With Pointers Martin Rinard and Darko Marinov Laboratory for Computer Science Massachusetts Institute of Technology.
Programming Language Basics. What is a Programming Language? “A computer, human-created language used to write instructions for a computer.” “An artificial.
Copyright © 2006, Oracle. All rights reserved Rule Engine.
Chapter 14 Functions.
Storage Allocation Mechanisms
Introduction to the C programming language
1. Systems and Software Development
An Introduction to Sorting
Linked Lists with Tail Pointers
Lists.
Simple Dynamic Data (Linked Lists)
Lists.
Linked Lists with Tail Pointers
Coding Concepts (Basics)
Programming Language Basics
Chapter 13 Finalizing Design Specifications
Course Overview PART I: overview material PART II: inside a compiler
Linked Lists.
MIPS function continued
Presentation transcript:

Reference Details (copying, comparing)

References You were introduced to the concept of pointers earlier this term. The pointer capabilities in pseudocode are relatively tame as compared to languages such as C which was originally developed for system programming In fact, pointers (and their misuse) are considered by some experts to be a very dangerous aspect of programming. The designers of Java decided to implement their language and eliminate many of the "dangerous" aspects of pointers. They even decided to call them something else: references

What’s all the fuss? Pointers (and references) are used for a very simple reasons: efficiency Pointers save time Pointers save space Example: Procedure add(cur iot in/out Ptr toa Node, dataIn iot in SomeBigRecType)... Function get returnsa SomeBigRecType (cur iot in Ptr toa Node)...

Efficient? The add procedure asks us to pass in a big record of some type. This (as we have defined it) would require us to make a local copy inside the module. Then with a line of code like: temp^.data <- datain We would make another copy! The get module would reverse the process. It might again copy the record one or more times. All this space...all this time to copy!

The Real World In the real world code would be much more likely to simply make some space for the data once and from then on just pass pointers to the data. So the linked list Node wouldn’t really contain a data record and a next pointer. It would contain a pointer to a data record AND a pointer to the next Node. data next Big record data next Big record

Java References Java takes advantage of this concept by manipulating all objects (which are actually chunks of memory on the heap) using references. This may cause some confusion! Let’s make a small class to use as an illustration. This class will hold info about boxes.

class Box class Box { private int len; private int wid; public Box(int len, int wid) { setLen(len); setWid(wid); } public void setLen(int len) { this.len = len; } public void setWid(int wid) { this.wid = wid; } public int getLen() { return len; } public int getWid() { return wid; } public int getArea() { return getLen()*getWid(); } public String toString() { return ("Rec: l= "+getLen()+", w= "+getWid()); } } // Box OK? len wid

Confusion? Box b1 = new Box(3, 4); Box b2 = new Box(3, 4); if(b1 == b2) System.out.println("The boxes are obviously equal!"); else System.out.println("I'm sooooo confused!"); Your final answer? b1 b2 box(3, 4)

Solution We need a method that compares two boxes! (And it has to be in the Box class!) public boolean equals(Box b) { if(getLen() == b.getLen() && getWid() == b.getWid()) return true; else return false; } class Box { private int len; private int wid; public Box(int len, int wid) { setLen(len); setWid(wid); } public void setLen(int len) { this.len = len; } public void setWid(int wid) { this.wid = wid; } public int getLen() { return len; } public int getWid() { return wid; } public int getArea() { return getLen()*getWid(); } public String toString() { return "(Rec: l= "+getLen()+", w= "+getWid(); }

Solution Box b1 = new Box(3, 4); Box b2 = new Box(3, 4); if(b1.equals(b2)) System.out.println("The boxes are obviously equal!"); else System.out.println("I’m sooooo confused!"); Your final answer? b1 b2 box(3, 4)

Copying? Box b1 = new Box(3, 4); Box b2 = new Box(5, 7); b2 = b1; Where did the other box go? b1 b2 box(3, 4) box(5, 7) b1 b2 box(3, 4)

Duplicating*? What if I want to make another identical box? One way: Box b1 = new Box(3, 4); Box b2 = new Box(b1.getLen(), b1.getWid()); Another way...add a duplicate method into the Box class! public Box duplicate() { return new Box(getLen(), getWid()); } Now we can say: Box b2 = b1.duplicate(); *cloning?

Copying? Box b1 = new Box(3, 4); Box b2 = b1.duplicate(); Box b3 = b2; b2.setWid(8); System.out.println(b3.getWid()); b2 = null; b1 b2 box(3, 4) box(3, 8) b3 b1 box(3, 4) b2 box(3, 8) b3 b1 box(3, 4)

Reference Details: int x; Creates an int primitive Queue q; Creates a reference (pointer) to a queue object. Note: No object has been created q = new Queue(); q is now pointing to a Queue object or more correctly q now has a reference to a queue object. Queue q2 = new Queue(); The q2 reference can be created and initialized in one step. It is now pointing at a queue object.

Complete Box Class class Box { private int len; private int wid; public Box(int len, int wid) { setLen(len); setWid(wid); } public void setLen(int len) { this.len = len; } public void setWid(int wid) { this.wid = wid; } public int getLen() { return len; } public int getWid() { return wid; } public int getArea() { return getLen()*getWid(); } public String toString() { return "(Rec: l= "+getLen()+", w= "+getWid(); } public boolean equals(Box b) { if(getLen() == b.getLen() && getWid() == b.getWid()) return true; else return false; } public Box duplicate() { return new Box(getLen(), getWid()); } public static void main(String args[]) { Box b1 = new Box(3, 4); Box b2 = new Box(3, 4); if(b1 == b2) System.out.println("The boxes are obviously equal!"); else System.out.println("I'm sooooo confused!"); if(b1.equals(b2)) System.out.println("The boxes are obviously equal!"); else System.out.println("I'm sooooo confused!"); Box b3 = b1.duplicate(); if(b1.equals(b3)) System.out.println("The boxes are obviously equal!"); else System.out.println("I'm sooooo confused!"); } } // Box