CS61B Lecture #5: Arrays and Objects

Similar documents
CS61B Lecture #7. Announcements:

Announcements. Last modified: Fri Sep 8 00:59: CS61B: Lecture #7 1

CS61B Lecture #6: Arrays

Public-Service Announcement

Public-Service Announcement I

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

STUDENT LESSON A5 Designing and Using Classes

CS558 Programming Languages

Programming Language Concepts: Lecture 2

Object-Oriented Principles and Practice / C++

CS61B Lecture #2. Public Service Announcements:

CS61B Lecture #3. Homework: Please see Homework #1 on the lab page. Last modified: Wed Sep 10 20:34: CS61B: Lecture #3 1

QUIZ. What is wrong with this code that uses default arguments?

CS558 Programming Languages

2. [20] Suppose we start declaring a Rectangle class as follows:

Memory Management. Didactic Module 14 Programming Languages - EEL670 1

Memory Management. Chapter Fourteen Modern Programming Languages, 2nd ed. 1

CS 116 Week 8 Page 1

Software Design and Analysis for Engineers

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

Quiz 1 Unit 5A Arrays/Static Name

Question: Total Points: Score:

University of Palestine. Mid Exam Total Grade: 100

CS61B Lecture #9: Interfaces and Abstract Classes

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

DO NOT. package java.lang; public final class String implements Serializable, Comparable, CharSequence {

Testing, Debugging, and Verification

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

CS 251 Intermediate Programming Java Basics

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

CS

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

Introduction to Programming Using Java (98-388)

Defensive Programming

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

Memory and C++ Pointers

JAVA: A Primer. By: Amrita Rajagopal

CS1007: Object Oriented Design and Programming in Java. Lecture #2 Sept 8 Shlomo Hershkop Announcements

Lecture Topics. Administrivia

enum Types 1 1 The keyword enum is a shorthand for enumeration. Zheng-Liang Lu Java Programming 267 / 287

Introduction to Object-Oriented Programming

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

COP4020 Programming Assignment 1 - Spring 2011

FORM 2 (Please put your name and form # on the scantron!!!!)

Converting Collections to Arrays. A Bad Approach to Array Conversion. A Better Approach to Array Conversion. public Object[] toarray();

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Creating and Using Objects

CS61B Lecture #12. Programming Contest: Coming up Saturday 5 October. See the contest announcement page, here.

Inf1-OOP. OOP Exam Review. Perdita Stevens, adapting earlier version by Ewan Klein. March 16, School of Informatics

Streams. CS21b: Structure and Interpretation of Computer Programs Spring Term, 2004

CS 61c: Great Ideas in Computer Architecture

A First Look At Java. Didactic Module 13 Programming Languages - EEL670 1

Handout 9 OO Inheritance.

Public-Service Announcement

Introduction to Java

1 Terminology. 2 Environments and Static Scoping. P. N. Hilfinger. Fall Static Analysis: Scope and Types

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

CSCI 1226 Second Midterm Test

Imperative Languages!

Objects and Classes. Lecture 10 of TDA 540 (Objektorienterad Programmering) Chalmers University of Technology Gothenburg University Fall 2017

2 rd class Department of Programming. OOP with Java Programming

CSCI-1200 Data Structures Fall 2009 Lecture 25 Concurrency & Asynchronous Computing

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Review: Classes and Object Instances. Review: Creating an Object. Using Multiple Objects. DrawingGizmo pencil; pencil = new DrawingGizmo();

JavaScript: Sort of a Big Deal,

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Instantiation of Template class

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Choose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points.

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

CIS Fall 2012 Midterm, 7 June 2012, Answer Key. Miscellaneous

CS S-08 Arrays and Midterm Review 1

Introduction to Java. Handout-1d. cs402 - Spring

Array. Array Declaration:

Rushikesh K Joshi. Department of Computer Science and Engineering Indian Institute of Technology Bombay

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

THE UNIVERSITY OF WESTERN AUSTRALIA. School of Computer Science & Software Engineering CITS1001 OBJECT-ORIENTED PROGRAMMING AND SOFTWARE ENGINEERING

CSE331 Winter 2014, Midterm Examination February 12, 2014

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

CS 251 Intermediate Programming Methods and Classes

6.092: Java for 6.170

CS 251 Intermediate Programming Methods and More

About this exam review

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

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

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Getting started with Java

5. PLEASE TAKE HOME the question bundle, but turn in 2 paper sheets: The scantron AND the paper where you wrote your programming question solution!

ECE 122. Engineering Problem Solving with Java

G Programming Languages - Fall 2012

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Dynamic memory allocation (malloc)

Anatomy of a Class Encapsulation Anatomy of a Method

Transcription:

CS61B Lecture #5: Arrays and Objects For faster response, please send urgent problems (like the lab files don t compile ) as mail to cs61b, rather than using class messages. Homeworks are generally due by the next lab. For next week, please read Head First Java, chapters 5 and 6. Last modified: Fri Jan 27 14:45:24 2006 CS61B: Lecture #5 1

Arrays An array is structured container whose components are length, a fixed integer. a sequence of length simple containers of the same type, numbered from 0. (.length field usually implicit in diagrams.) Arrays are anonymous, like other structured containers. Always referred to with pointers. For array pointed to by A, Length is A.length Numbered component i is A[i] (i is the index) Important feature: index can be any integer expression. Last modified: Fri Jan 27 14:45:24 2006 CS61B: Lecture #5 2

A Few Samples Java Results int[] x, y, z; String[] a; x = new int[3]; y = x; a = new String[3]; x[1] = 2; y[1] = 3; a[1] = "Hello"; int[] q; q = new int[] { 1, 2, 3 ; // Short form for declarations: int[] r = { 7, 8, 9 ; x: 0 3 0 y: z: a: Hello q: 1 2 3 r: 7 8 9 Last modified: Fri Jan 27 14:45:24 2006 CS61B: Lecture #5 3

Example: Accumulate Values Problem: Sum up the elements of array A. static int sum (int[] A) { int N; N = 0; // New (1.5) syntax for (int i = 0; i < A.length; i += 1) for (int x : A) N += A[i]; N += x; return N; // For the hard-core: could have written int N, i; for (i=0, N=0; i<a.length; N += A[i], i += 1) { // or just ; // But please don t: it s obscure. Last modified: Fri Jan 27 14:45:24 2006 CS61B: Lecture #5 4

Example: Insert into an Array Problem: Want a call like insert (A, 2, "gnu") to convert (destructively) A: bear gazelle hartebeest skunk to A: bear gazelle gnu hartebeest /** Insert X at location K in ARR, moving items * K, K+1,... to locations K+1, K+2,... * The last item in ARR is lost. */ static void insert (String[] arr, int k, String x) { for (int i = arr.length-1; i > k; i -= 1) // Why backwards? arr[i] = arr[i-1]; // Alternative to this loop: // System.arraycopy ( arr, k, {{ from arr[k] = x; arr, k+1, {{ to arr.length-k-1); {{ # to copy Last modified: Fri Jan 27 14:45:24 2006 CS61B: Lecture #5 5

Growing an Array Problem: Suppose that we want to change the description above, so that A = insert2 (A, 2, "gnu") does not shove skunk off the end, but instead grows the array. A: bear gazelle hartebeest skunk to A: bear gazelle gnu hartebeest skunk /** Return array, r, where r.length = ARR.length+1; r[0..k-1] * the same as ARR[0..K-1], r[k] = x, r[k+1..] same as ARR[K..]. */ static String[] insert2 (String[] arr, int k, String x) { String[] result = new String[arr.length + 1]; System.arraycopy (arr, 0, result, 0, k); System.arraycopy (arr, k, result, k+1, arr.length-k); result[k] = x; return result; Why do we need a different return type from insert?? Last modified: Fri Jan 27 14:45:24 2006 CS61B: Lecture #5 6

Basic Idea. Object-Based Programming Function-based programs are organized primarily around the functions (methods, etc.) that do things. Data structures (objects) are considered separate. Object-based programs are organized around the types of objects that are used to represent data; methods are grouped by type of object. Simple banking-system example: Function-based Object-based account deposit account withdraw deposit Account withdraw balance: 1420 account account Exported methods Exported field Last modified: Fri Jan 27 14:45:24 2006 CS61B: Lecture #5 7

Philosophy Idea (from 1970s and before): An abstract data type is a set of possible values (a domain), plus a set of operations on those values (or their containers). InIntList,forexample,thedomainwasasetofpairs: (head,tail), where head is an int and tail is a pointer to an IntList. The IntList operations consisted only of assigning to and accessing the two fields (head and tail). In general, prefer a purely procedural interface, where the functions (methods) do everything no outside access to fields. That way, implementor of a class and its methods has complete control over behavior of instances. In Java, the preferred way to write the operations of a type is as instance methods. Last modified: Fri Jan 27 14:45:24 2006 CS61B: Lecture #5 8

You Saw It All in CS61A: The Account class (define-class (account balance0) (instance-vars (balance 0)) (initialize (set! balance balance0)) (method (deposit amount) (set! balance (+ balance amount)) balance) (method (withdraw amount) (if (< balance amount) (error "Insufficient funds") (begin (set! balance (- balance amount)) balance))) ) (define my-account (instantiate account 1000)) (ask my-account balance) (ask my-account deposit 100) (ask my-account withdraw 500) public class Account { public int balance; public Account (int balance0) { balance = balance0; public int deposit (int amount) { balance += amount; return balance; public int withdraw (int amount) { if (balance < amount) throw new IllegalStateException ("Insufficient funds"); else balance -= amount; return balance; Account myaccount = new Account (1000); myaccount.balance myaccount.deposit (100); myaccount.withdraw(500); Last modified: Fri Jan 27 14:45:24 2006 CS61B: Lecture #5 9

The Pieces Class declaration defines a new type of object, i.e., new type of structured container. Instance variables such as balance are the simple containers within these objects (fields or components). Instance methods, such as deposit and withdraw are like ordinary (static) methods that take an invisible extra parameter(called this). The new operator creates (instantiates) new objects, and initializes them using constructors. Constructors such as the method-like declaration of Account are special methods that are used only to initialize new instances. They take their arguments from the new expression. Method selection picks methods to call. For example, myaccount.deposit(100) tells us to call the method named deposit that is defined for the object pointed to by myaccount. Last modified: Fri Jan 27 14:45:24 2006 CS61B: Lecture #5 10

Getter Methods Slight problem with Java version of Account: anyone can assign to the balance field This reduces the control that the implementor of Account has over possible values of the balance. Solution: allow public access only through methods: public class Account { private int balance;... public int balance () { return balance;... Now the balance field cannot be directly referenced outside of Account. (OK to use name balance for both the field and the method. Java can tell which is meant by syntax: A.balance vs. A.balance().) Last modified: Fri Jan 27 14:45:24 2006 CS61B: Lecture #5 11

Class Variables and Methods Suppose we want to keep track of the bank s total funds. This number is not associated with any particular Account, but is common to all it is class-wide. In Java, class-wide static public class Account {... private static int funds = 0; public int deposit (int amount) { balance += amount; funds += amount; return balance; public static int funds () { return funds;... // Also change withdraw. From outside, can refer to either Account.funds() or myaccount.funds() (same thing). Last modified: Fri Jan 27 14:45:24 2006 CS61B: Lecture #5 12

Instance method such as Instance Methods int deposit (int amount) { balance += amount; funds += amount; return balance; behaves sort of like a static method with hidden argument: static int deposit (final Account this, int amount) { this.balance += amount; funds += amount; return this.balance; NOTE: Just explanatory: Not real Java (not allowed to declare this ). (final is real Java; means can t change once set. ) Likewise, the instance-method call myaccount.deposit (100) is like a call on this fictional static method: Account.deposit (myaccount, 100); Inside method, as a convenient abbreviation, can leave off leading this. on field access or method call if not ambiguous. Last modified: Fri Jan 27 14:45:24 2006 CS61B: Lecture #5 13

Instance and Static Don t Mix Since real static methods don t have the invisible this parameter, makes no sense to refer directly to instance variables in them: public static int badbalance (Account A) { int x = A.balance; // This is OK (A tells us whose balance) return balance; // WRONG! NONSENSE! Reference to balance here equivalent to this.balance, But this is meaningless (whose balance?) However, it makes perfect sense to access a static(class-wide) field or method in an instance method or constructor, as happened with funds in the deposit method. There s only one of each static field, so don t need to have a this to get it. Can just name the class. Last modified: Fri Jan 27 14:45:24 2006 CS61B: Lecture #5 14

Constructors To completely control objects of some class, you must be able to set their initial contents. A constructor is a kind of special instance method that is called by the new operator right after it creates a new object, as if tmp = pointerto 0 ; L = new IntList(1,null) = tmp.intlist(1, null); L = tmp; Instance variables initializations are moved inside constructors: class Foo { int x = 5; Foo () { DoStuff ();... class Foo { int x; Foo () { x = 5; DoStuff ();... In absence of any explicit constructor, get default constructor: public Foo() {. Multiple overloaded constructors possible (different parameters). Last modified: Fri Jan 27 14:45:24 2006 CS61B: Lecture #5 15

Summary: Java vs. CS61A OOP in Scheme Java CS61A OOP class Foo... (define-class (Foo args)... int x =...; (instance-vars (x...)) Foo(args) {... (initialize...) int f(...) {... (method (f...)...) static int y =...; (class-vars (y...)) static void g(...) {... (define (g...)...) afoo.f (...) (ask afoo f...) afoo.x (ask afoo x) new Foo (...) (instantiate Foo...) this self Last modified: Fri Jan 27 14:45:24 2006 CS61B: Lecture #5 16