CS 302 Week 8 Jim Williams, PhD.

Slides:



Advertisements
Similar presentations
The ArrayList Class and the enum Keyword
Advertisements

CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Alice in Action with Java
ArrayList, Multidimensional Arrays
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Arrays of Objects 1 Fall 2012 CS2302: Programming Principles.
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.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
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.
WEEK 2 Introduction to Java II CSE 252 Principles of Programming Languages LAB SECTION.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
CSE 1201 Object Oriented Programming ArrayList 1.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
More on Arrays Review of Arrays of ints, doubles, chars
CMSC 202 ArrayList Aug 9, 2007.
Sixth Lecture ArrayList Abstract Class and Interface
Lecture Note Set 1 Thursday 12-May-05
CS Week 10 Jim Williams, PhD.
Class Variables We’ve seen how to add extra subroutines into our programs Can be accessed from within one another We can also add variables that are “global”
CS Week 14 Jim Williams, PhD.
CS 200 Arrays, Loops and Methods
CS 302 Week 15 Jim Williams, PhD.
CS Week 13 Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
CS Week 8 Jim Williams, PhD.
CS 200 Using Objects Jim Williams, PhD.
AP Java Unit 3 Strings & Arrays.
CS Week 7 Jim Williams, PhD.
CS 200 Arrays, Loops and Methods
CS 200 More Classes Jim Williams, PhD.
CS Week 9 Jim Williams, PhD.
Week 6 CS 302 Jim Williams, PhD.
Arrays of Objects Fall 2012 CS2302: Programming Principles.
CS 200 Objects and ArrayList
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
CMSC 202 ArrayList Aug 9, 2007.
ArrayLists.
Arrays of Objects Fall 2012 CS2302: Programming Principles.
CS240: Advanced Programming Concepts
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
CS 200 Loops Jim Williams, PhD.
CMSC 202 ArrayList Aug 9, 2007.
Grouped Data Arrays, and Array Lists.
CS 200 Primitives and Expressions
int [] scores = new int [10];
CS 200 Arrays Jim Williams, PhD.
slides created by Ethan Apter
CS 200 Primitives and Expressions
CS 200 Methods, Using Objects
Lecture 13: ArrayLists AP Computer Science Principles
CS 200 Objects and ArrayList
Introduction to Data Structure
CMPE212 – Reminders Course Web Site:
CSE 143 Lecture 2 ArrayIntList, binary search
Review: libraries and packages
CS 200 More Classes Jim Williams, PhD.
slides created by Ethan Apter
Barb Ericson Georgia Institute of Technology Oct 2005
Two-Dimensional Arrays
Lecture 6: References and linked nodes reading: 16.1
CS Week 3 Jim Williams, PhD.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Week 7 CS 302 Jim Williams.
CS 200 Objects and ArrayList
CS302 Week 6 Jim Williams.
Presentation transcript:

CS 302 Week 8 Jim Williams, PhD

This Week Lab: Debugging with Debugger Lecture: Static memory area Object-Oriented Programming ArrayList Wrapper Classes Thursday: Gary Dahl

What is the value of num? num: 10 Stuff.num: 6 Stuff.num: 10 class Stuff { final static int MAX_VALUE = 10; //allowed P2 static int num = 6; //NOT allowed in P2 static void change( int n) { num = n + 1; } public static void main( String [] args) { int num = MAX_VALUE; change( num); System.out.println("num:" + num); System.out.println("Stuff.num:" + Stuff.num); num: 10 Stuff.num: 6 Stuff.num: 10 Stuff.num: 11 error try it.

What are values of x and y? static int x; static int y = 2; static int methodA( int y) { x = y + 1; return x + y; } public static void main(String []args) { int x = 5; y = methodA( x); System.out.println("x="+x+ " y="+y); x=5 y=11 x=3 y=2 x=5 y=2 x=3 y=11 try it.

Instance vs. Class (static) Methods method definition has “static” modifier use name of class when calling Math.max( 10, 20); Instance (non-static) Methods method definition does Not have “static” modifier use instance of class when calling Random randGen = new Random(); randGen.nextInt( 5);

What is the answer? AP? String s1 = "An important programming tool."; String s2 = s1.substring( 9, 10); String s4 = new String( "?"); if ( s1.contains( "gram")) { s4 = s1.substring( 2, 4).trim(); } char c3 = s1.charAt( s1.indexOf('g') -3); String answer = (s2 + c3 + s4).toUpperCase(); AP? api ANP IM API try it.

Object-Oriented Programming ArrayLists - specific example of use Concepts - Big Picture Grouping related data and methods User Defined Data Type (class) Creating Instances from a Class Creating a Class

Arrays fixed number of same data type (contiguous in memory) What would we like to do with arrays? Add elements, remove elements...

How many elements in an Array vs ArrayList? static void methodA(int [] arr) { //how to find length of array? } static void methodB(ArrayList list) { //how to find length of list? arr.length list.size() list.length() arr.size correct: A (try it)

What happens when array grows automatically array index out of bounds error who knows? You try to add more elements than will fit into an array? B and technically C since B is an error. (try it)

What happens when ArrayList grows automatically ArrayList index out of bounds error who knows? You try to add more elements than will fit into an ArrayList? A (try it)

What happens when you have to make sure there is enough room and move all the elements down and then insert it. Insertion is handled automatically You want to insert an element in an array at a specific index? A is the best answer.

What happens when you have to make sure there is enough room and move all the elements down and then insert it. Insertion is handled automatically You want to insert an element in an ArrayList at a specific index? B is the best answer

Data members Instance data members (variables) should be ______ unless there is a very good reason. private public protected static A is the best answer

Instance methods Instance methods should be ______ . private public depends static depends is the best answer. Instance methods that are private can only be called from within the class. private methods can be helpful and since they are not public code outside the class cannot call them and so they can potentially be changed (improved) over time (fewer dependencies). All calls to the private methods must be in the same class.

Can primitive values be stored in an ArrayList? yes no If they are boxed int yes, char no C is the best answer. Wrapper classes are utilized to create instances in order to put primitive type values in data structures such as an ArrayList.

Will this work? Integer i = 10; yes no sometimes error yes, it is an example of auto-boxing.