slides created by Ethan Apter

Slides:



Advertisements
Similar presentations
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Advertisements

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Static Methods Static methods are those methods that are not called on objects. In other words, they don’t have an implicit parameter. Random number generation.
Road Map Introduction to object oriented programming. Classes
CSE 143 Lecture 3 Inheritance slides created by Marty Stepp
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 15 Lecture 15-2: testing ArrayIntList; pre/post conditions and exceptions reading:
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
The need for Programming Languages
Chapter 3: Using Methods, Classes, and Objects
Classes and Objects 2nd Lecture
Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header:
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Recursion 12-Nov-18.
this keyword this : A reference to the implicit parameter Syntax:
Phil Tayco Slide version 1.0 Created Oct 16, 2017
Object Oriented Programming COP3330 / CGS5409
this keyword this : A reference to the implicit parameter Syntax:
Lecture 2: Implementing ArrayIntList reading:
Building Java Programs
Building Java Programs
Defining New Types of Objects, part 3
slides created by Marty Stepp and Ethan Apter
Lecture 26: Advanced List Implementation
Recursion 2-Dec-18.
Recursion 2-Dec-18.
Java Programming Arrays
CSE 143 Lecture 2 Collections and ArrayIntList
Building Java Programs
Lecture 2: Implementing ArrayIntList reading:
Recursion 29-Dec-18.
slides created by Ethan Apter
Today’s topics UML Diagramming review of terms
Namespaces, Scopes, Access privileges
CSE 143 Lecture 4 More ArrayIntList:
slides created by Ethan Apter
ArrayLists 22-Feb-19.
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CSE 143 Lecture 5 References and Linked Nodes
Building Java Programs
Go to pollev.com/cse143.
CSE 143 Lecture 3 Implementing ArrayIntList reading:
Suggested self-checks: Section 7.11 #1-11
CSE 143 Lecture 5 More ArrayIntList:
Recursion 23-Apr-19.
CSE 143 Lecture 4 Implementing ArrayIntList; Binary Search
slides created by Alyssa Harding
Linked Lists based on slides created by Ethan Apter
CSE 143 Lecture 2 ArrayIntList, binary search
Building Java Programs
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
slides created by Ethan Apter
CSE 143 Lecture 4 Implementing ArrayIntList reading:
slides created by Ethan Apter and Marty Stepp
slides created by Marty Stepp
Linked Lists and Loops based on slides by Ethan Apter
CSE 143 Lecture 21 Advanced List Implementation
CMPE212 – Reminders Assignment 2 due next Friday.
slides created by Ethan Apter
Review for Midterm 3.
Classes and Objects Object Creation
slides created by Marty Stepp
Implementing ArrayIntList
CS 240 – Advanced Programming Concepts
Presentation transcript:

slides created by Ethan Apter CSE 143 Lecture 3 ArrayIntList slides created by Ethan Apter http://www.cs.washington.edu/143/

remove ArrayIntList has an add, so it should also have a remove remove will take an index as a parameter But how do we remove from ArrayIntList? Is it enough to just set the value to 0 or -1? No! 0 and -1 can represent real, valid data Instead we need to: shift all remaining valid data, so there is no “hole” in our data decrement size, so there’s one less piece of data

remove remove code: public void remove(int index) { for (int i = index; i < size – 1; i++) { elementData[i] = elementData[i + 1]; } size--; We didn’t “reset” any value to 0. Why not? Be careful with loop boundaries!

remove If we made an ArrayIntList and added the values 6, 43, and 97, it would have the following state: 0 1 2 3 4 98 99 elementData: 6 43 97 0 0 ... 0 0 size: 3 After a call of remove(0) it has this state: elementData: 43 97 97 0 0 ... 0 0 size: 2 We don’t care what values are in the invalid data valid invalid valid invalid

Is ArrayIntList Finished? What we’ve done so far: Made an ArrayIntList class Gave it enough variables to maintain its state Gave it three methods: add, remove, and toString Sure we could add more methods... But what if our client is malicious? ArrayIntList list = new ArrayIntList(); list.add(6); list.size = -1 list.size = 9000; list.elementData = null; This can really mess up our ArrayIntList!

private private is a keyword in Java private is used just like public, but has the opposite effect When something is made private, it can be accessed only by the class in which it is declared Some things that can be private: methods fields inner classes (but we’re not going to cover this one)

ArrayIntList Now we’ll update ArrayIntList to use private fields: public class ArrayIntList { private int[] elementData = new int[100]; private int size = 0; ... } Now the malicious code won’t work! If the client tries to access elementData or size, he’ll get a compiler error

A Problem! What if the client wants to know the current size of ArrayIntList? This seems like a reasonable request... But we’ve completely blocked all access to size We don’t mind telling the client the current size, we just don’t what him to change it How can we solve this problem?

Accessor Methods We can write a method that returns the current size: public int size() { return size; } Because size is an int, this returns a copy of size Our size method is an accessor method Accessor method: a method that returns information about an object without modifying the object

get We should also provide a way for the client to read the values in elementData This should also be an accessor method. We’ll call it get: get will return the value of elementData at a given index Code for get: public int get(int index) { return elementData[index]; }

Preconditions What happens if someone passes an illegal index to get? possible illegal indexes: -100, 9999 Our code will break! This means get has a precondition Precondition: a condition that must be true before a method is called. If it is not true, the method may not work properly So, a precondition for get is that the index be valid The index must be greater than or equal to zero And the index must be less than size At the very least, we should record this precondition in a comment

Postconditions While we’re writing a comment for get, we should also say what it action it performs Postcondition: a condition a method guarantees to be true when it finishes executing, as long as the method’s preconditions were met What is get’s postcondition? it has returned the current value located at the given index

Pre/Post for get One way to record preconditions and postconditions is with a pre/post style comment: // pre: 0 <= index < size() // post: returns the value at the given index public int get(int index) { return elementData[index]; } Comments should include a method’s preconditions and postconditions

Constructors Whenever you use the keyword new, Java calls a special method called the constructor Constructors have special syntax they have the same name as the class they do not have a return type Here’s how to write a simple constructor for ArrayIntList: public ArrayIntList() { // constructor code ... }

Default Constructor But didn’t we already use new on our ArrayIntList? How does that work when we hadn’t yet written a constructor? If a class does not have any constructors, Java provides a default constructor The default constructor is often known as the zero-argument constructor, because it takes no parameters/arguments However, as soon as you define a single constructor, Java no longer provides the default constructor

ArrayIntList Constructor Here’s the updated code for ArrayIntList, now with a constructor: public class ArrayIntList { private int[] elementData; private int size; public ArrayIntList() { elementData = new int[100]; size = 0; } ... Notice that I moved the initialization of the fields into the constructor. This is considered better style in Java, and we will look for it when grading.

Automatic/Implicit Initialization What happens if the fields are never initialized? If you don’t initialize your fields, Java will automatically initialize them to their zero-equivalents Some zero-equivalents, by type: int: 0 double: 0.0 boolean: false arrays and objects: null This means we did not have to initialize size to 0 before. Both styles (explicit and implicit initialization) are acceptable.

Multiple Constructors You can have more than one constructor Just like when overloading other methods, all constructors for the same class must have different parameters An ArrayIntList constructor that takes a capacity as a parameter: public ArrayIntList(int capacity) { elementData = new int[capacity]; size = 0; }

this Now we have the following two constructors: We can use the keyword this to fix our redundancy. Using this with parameters will call the constructor in the same class that requires those parameters. Updated constructor code: public ArrayIntList() { public ArrayIntList(int capacity) { elementData = new int[100]; elementData = new int[capacity]; size = 0; } public ArrayIntList() { public ArrayIntList(int capacity) { this(100); elementData = new int[capacity]; } size = 0;

Constants Our default value of 100 for capacity is arbitrary We should make it a class constant instead Code to declare a class constant: public static final int DEFAULT_CAPACITY = 100; Updated zero-argument constructor: public ArrayIntList() { this(DEFAULT_CAPACITY); }

Completed ArrayIntList Has two fields elementData and size Has one constant DEFAULT_CAPACITY Has two constructors ArrayIntList() and ArrayIntList(int capacity) Has seven methods (some not covered in lecture) size(), get(int index), toString(), indexOf(int value), add(int value), add(int index, int value), and remove(int index)

Quick Discussion: static static is hard to understand Many of you will pass CSE 143 without understanding static When something is declared static, it is shared by all instances of a class What would happen if we made size a static field? All instances of ArrayIntList would use and update the same size variable! We do not want to do this... But what would happen if we tried it?

Quick Discussion: static Making size a static field: private static int size; Consider the following code ArrayIntList list1 = new ArrayIntList(); ArrayIntList list2 = new ArrayIntList(); list1.add(6); list1.add(9); System.out.println(“sizes: “ + list1.size() + “, “ + list2.size()); System.out.println(“toStrings: “ + list1 + “, “ + list2); What is printed? sizes: 2, 2 toStrings: [6, 9], [0, 0] Making size static affects more than just size()!