CSE 143 Lecture 2 More ArrayList; classes and objects

Slides:



Advertisements
Similar presentations
Building Java Programs Chapter 10
Advertisements

Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Building Java Programs
1 CSE 143 Lecture 1: ArrayList reading: Welcome to CSE 143! I'm Allison Obourn
Building Java Programs
Introduction to Object-Oriented Programming
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
ARRAYLIST.. Hazen High School. Vocabulary to Know ArrayList Generic Class ArrayList Operations ArrayList Methods ArrayList Searching For-Each Wrapper.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 4 ArrayList Reading: 10.1 slides created by Marty Stepp
Copyright 2008 by Pearson Education Building Java Programs Chapter 10 Lecture 10-1: ArrayList reading: 10.1.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
CSE 143 Lecture 2 ArrayList reading: 10.1 slides created by Marty Stepp
Copyright 2010 by Pearson Education Building Java Programs Chapter 10 Lecture 21: ArrayList reading: 10.1.
Problem 1 Bank.  Manage customers’ bank account using the following operations: Create a new account given a customer’s name and initial account. Deposit.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 4 Exceptions and ArrayList slides created by Marty Stepp
Chapter 3 Implementing Classes
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 10 ArrayList Copyright (c) Pearson All rights reserved.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
slides created by Marty Stepp
Collections.
Collections.
CSE 143 Lecture 3 More ArrayList; object-oriented programming
Building Java Programs
Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header:
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Welcome to CSE 143!.
Building Java Programs
Building Java Programs Chapter 10
TCSS 143, Autumn 2004 Lecture Notes
Lecture 2: Implementing ArrayIntList reading:
ArrayLists.
Building Java Programs
Building Java Programs
CSE 143 Lecture 4 ArrayList Reading: 10.1.
ArrayLists.
Building Java Programs Chapter 10
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Building Java Programs
Topic 33 ArrayList.
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Chapter 10 ArrayList reading: 10.1
Welcome to CSE 143!.
Lecture 1: ArrayList reading: 10.1
Lecture 2: Implementing ArrayIntList reading:
A programming problem Given a file of cities' (x, y) coordinates, which begins with the number of cities: Write.
Building Java Programs
Lecture 13: ArrayLists AP Computer Science Principles
References and objects
Lecture 16: Arraylist Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Go to pollev.com/cse143.
CSE 143 Lecture 3 Implementing ArrayIntList reading:
Building Java Programs
ArrayLists.
slides created by Marty Stepp
Building Java Programs
Building Java Programs
CSE 143 Lecture 3 More ArrayList; object-oriented programming
slides created by Marty Stepp
Building Java Programs
Building Java Programs
Building Java Programs
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Building Java Programs
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

CSE 143 Lecture 2 More ArrayList; classes and objects reading: 10.1; 8.1 - 8.7 slides adapted from Marty Stepp and Hélène Martin http://www.cs.washington.edu/143/

Words exercise, revisited Write a program that reads a file and displays the words of that file as a list. Then display the words in reverse order. Then display them with all plural words removed.

Exercise solution (partial) ArrayList<String> allWords = new ArrayList<String>(); Scanner input = new Scanner(new File("words.txt")); while (input.hasNext()) { String word = input.next(); allWords.add(word); } // display in reverse order for (int i = allWords.size() - 1; i >= 0; i--) { System.out.println(allWords.get(i)); // remove all plural words for (int i = 0; i < allWords.size(); i++) { String word = allWords.get(i); if (word.endsWith("s")) { allWords.remove(i); i--;

ArrayList of primitives? The type you specify when creating an ArrayList must be an object/class type; it cannot be a primitive type. // illegal; int cannot be a type parameter ArrayList<int> list = new ArrayList<int>(); But we can still use ArrayList with primitive types by using special classes called wrapper classes in their place. // legal; creates a list of ints ArrayList<Integer> list = new ArrayList<Integer>();

Wrapper classes A wrapper is an object whose sole purpose is to hold a primitive value. Once you construct the list, use it with primitives as normal: ArrayList<Double> grades = new ArrayList<Double>(); grades.add(3.2); grades.add(2.7); ... double myGrade = grades.get(0); Primitive Type Wrapper Type int Integer double Double char Character boolean Boolean

Classes and objects class: A program entity that represents: A complete program or module, or A template for a type of objects. (ArrayList is a class that defines a type.) object: An entity that combines state and behavior. object-oriented programming (OOP): Programs that perform their behavior as interactions between objects. abstraction: Separation between concepts and details. Objects provide abstraction in programming.

BankAccount exercise Suppose we have a class BankAccount with the methods: public BankAccount(String name, int id) public void deposit(double amount) public void withdraw(double amount) public double getBalance() public int getID() Make each account keep a log of all its transactions. Desired: a printLog method that shows all transactions so far. Deposit of $7.82 Withdrawal of $2.55 Deposit of $6.18 Another method idea: eraseLog method that erases all log text. (call clear on arraylist)

Objects storing collections An object can have an array, list, or other collection as a field. public class Course { private double[] grades; private ArrayList<String> studentNames; public Course() { grades = new double[4]; studentNames = new ArrayList<String>(); ... } Now each object stores a collection of data inside it.