Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Lists Introduction to Computing Science and Programming I.
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
More sophisticated behavior Using library classes to implement some more advanced functionality 3.0.
Programming with Collections Grouping & Looping - Collections and Iteration Week 7.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Lecture 14 The String Class Lecture 14 The String Class Instructor: Craig Duckett.
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
M1G Introduction to Programming 2 4. Enhancing a class:Room.
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Programming With Java ICS201 University Of Ha’il1 Chapter 14 Generics and The ArrayList Class.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
AP CS Workshop ArrayList It is very common for applications to require us to store a large amount of data. Array lists store large amounts of data.
Goals for Today  implement a Deck of Cards  composition  Iterator interface  Iterable interface 1.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
Lecture 2: Classes and Objects, using Scanner and String.
ArrayList, Multidimensional Arrays
Not overriding equals  what happens if you do not override equals for a value type class?  all of the Java collections will fail in confusing ways 1.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Java SE 8 for Programmers, Third Edition
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Data structures Abstract data types Java classes for Data structures and ADTs.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
10-Nov-15 Java Object Oriented Programming What is it?
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
More Java: Static and Final, Abstract Class and Interface, Exceptions, Collections Framework 1 CS300.
CSE 1201 Object Oriented Programming ArrayList 1.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session.
Comp1004: Environments The Java Library. Coming up Recap – Encapsulation – Constructors – Loops – Arrays – ArrayList – Iterators The Java Library – Implementation.
Comp1202: Conclusions Revision Session. Coming up Key Concepts - The Pillars HashMaps Exceptions The Exam Some Last Words.
CMSC 202 ArrayList Aug 9, 2007.
Lecture 5: Some more Java!
More sophisticated behavior
CMSC 202 ArrayList Aug 9, 2007.
CMSC 202 ArrayList Aug 9, 2007.
slides created by Ethan Apter
Creating and Modifying Text part 3
Building Java Programs
Web Design & Development Lecture 6
LCC 6310 Computation as an Expressive Medium
Coming up Variables Quick look at scope Primitives Objects
Presentation transcript:

Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps

Lecture 9 Shhh, no talking in the library OO

Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps

Implementation and interface The API describes – what a class does (interface of the class) – not how a class does it (implementation of a class) When you write classes, you write the implementation of those classes

Library The java library is full of helpful classes Like ArrayList What does the inside of an ArrayList look like? How does it handle the resizing? How does it know when the throw an error? How does it handle renumbering when removing elements?

Interface All we need to know to use the ArrayList class, and the other library classes is what their interface is Interface is how we interact with a class – what methods to call – what they will return – etc OO

Importing Library classes must be imported using an import statement – except classes from java.lang. i.e. String – look it up on API import java.util.ArrayList; public class myClass{ ArrayList arrl; arrl = new ArrayList ; public static void main(String[] args){ } }

Importing packages Classes are organised in packages. Single classes may be imported: import java.util.ArrayList; Whole packages can be imported: import java.util.*;

Strings Strings are actually objects – Did you notice we always use a capital S like other classes? You don’t need to import them – they are from the automatically imported from java.lang.*; As is System as in System.out.println()

Coming up Implementation vs. Interface The Truth about variables –W–Well, an abstract version of the truth anyway Comparing strings HashMaps

Have you ever.. Accidently tried to output (print) an object? int[] array = new int[10]; System.out.println(array); You’ll get something like: OO

What’s that? This is the address in memory that the computer uses to store objects in some other langauges (i.e. C++) you can manipulate those addresses and their contents directly It’s time to look into the “variables as cups” anaology in a bit more detail OO

So far... We’ve said – A variable is a cup – A primitive fits into a cup – A remote control to an object fits into a cup

The Truth The “cup” is a bit of storage on a silicone chip in the computer somewhere By giving the storage cups names, the computer associates the name of the cup with the address in memory where the storage is. int age = 7; really means – “when the user types ‘age’ they really mean the contents of memory area ”

The Truth A bit pattern goes into the storage – This bit pattern can either represent: – A number, character or other primitive value – The address in memory of something else, an object

So what? Why is this important? It’s important to know for comparing Strings. OO

Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps

A word about comparing Strings if(input == “hello") { //code here } if(input.equals(“hello")) { //code here } Strings should always be compared with.equals OO

identity vs equality Memory name1 String Harry Ron name2 String Harry name1 == name2 False, different addresses name1.equals(name2) False, different value name1==name2 False, different addresses name1.equals(name2) True, same value name1==name2 True, same address name1.equals(name2) True, same characters OO

Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps

HashMap One of the most useful classes in the library is the HashMap It: – takes an object (The key) – looks at the internal properties within it – uses algorithms to compute a number based on those properties. This is the object’s hash code – stores the object in something like an array in the index given by the hash code

Then... Then the HashMap associates the second object (the value) with the key object This is useful

Maps Maps are a collection type that map a key to a value add(“Alfie”,“407351”); and so on Memory Map: Name -> Phone number Alfie Jenny

Lookup Lookup is the act of supplying the key and having the value returned String num = myHashMap.get(“Alfie”); Memory myHashMap Alfie

HashMaps are used in upcoming labs... – you will come across Sets. They are a collection like any other, you can use the for-each loop with them etc – What happens if you add a key and a value, and then add the same key again, but with a different value? – Check out HashMaps in the API

Covered today Implementation vs. Interface The Truth about variables Comparing strings HashMaps