iat 800

23
Oct 15, Fall 2009 IAT 800 1 IAT 800 ArrayList, Text, Java Libraries

Upload: mattox

Post on 06-Jan-2016

47 views

Category:

Documents


1 download

DESCRIPTION

IAT 800. ArrayList, Text, Java Libraries. Topics. ArrayLists ArrayLists + Polygons Some words on Strings. ArrayLists. Pros: Are great if you don’t know how many things are going to go into the array. Automatically resizes when you add to a full ArrayList. Cons: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: IAT 800

Oct 15, Fall 2009 IAT 800 1

IAT 800

ArrayList, Text, Java Libraries

Page 2: IAT 800

Oct 15, Fall 2009 IAT 800 2

Topics

ArrayLists– ArrayLists + Polygons

Some words on Strings

Page 3: IAT 800

Oct 15, Fall 2009 IAT 800 3

ArrayLists

Pros:– Are great if you don’t know how many things

are going to go into the array. – Automatically resizes when you add to a full

ArrayList. Cons:

– Only stores objects, not primitive variables (int, float…). Need to make objects from those.

– Only returns things of the Object type. Need to explicitly cast back to the type you put in there.

Page 4: IAT 800

Oct 15, Fall 2009 IAT 800 4

ArrayLists

Remember, an ArrayList is just like an array:

If we decide we need to add another element to the array, we need to make a NEW, bigger array, and copy all the elements into it.

What a pain!

Page 5: IAT 800

Oct 15, Fall 2009 IAT 800 5

ArrayLists

ArrayLists do this for us! We don’t even have to declare a size when we first create it, only deciding how many elements we’ll have when we actually add them.– ArrayList a = new ArrayList();– a.add(Object o) – adds an object at next index. – a.get(int i) – returns object at i index.– a.size() – returns number of items in the

ArrayList.

Page 6: IAT 800

Oct 15, Fall 2009 IAT 800 6

Simple Example - ArrayList

Let’s make a little class called Point, which simply stores an X and Y value associated with a point on the screen.

class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; }}

Page 7: IAT 800

Oct 15, Fall 2009 IAT 800 7

Simple Example - ArrayList

ArrayList of Points instead of two arrays of x and y points.

ArrayList pointList = new ArrayList();pointList.add(new Point(45, 50));pointList.add(new Point(79, 23));

// let’s draw a circle at our second point.Point p2 = (Point)pointList.get(1);ellipse(p2.x, p2.y, 50, 50);

Page 8: IAT 800

Oct 15, Fall 2009 IAT 800 8

Simple Example - ArrayList

We could extend this so that every time we click, that point gets added to our ArrayList.

void mouseReleased() { pointList.add(new Point(mouseX, mouseY));}

Page 9: IAT 800

Oct 15, Fall 2009 IAT 800 9

Simple Example - ArrayList

We could then do anything with these points as they accumulate. Here’s an example:

void draw() { background(255); if(pointList.size() > 1) { beginShape(); for(int i = 0; i < pointList.size(); i++) { Point p1 = (Point)pointList.get(i); vertex(p1.x, p1.y); } endShape(CLOSE); }}

Every time we click, thepolygon will re-draw, usingthe new point we’ve added.

Page 10: IAT 800

Oct 15, Fall 2009 IAT 800 10

Types

You may recall when we talked about types– Primitives

• int, float, byte• boolean• char

– Objects (composites)• Array• ArrayList• PImage• (any object you create)• Strings

Page 11: IAT 800

Oct 15, Fall 2009 IAT 800 11

String details

A string is almost like an array of chars– char someletter = 'b';– String somewords = "Howdy-do, mr. jones?";– Note the use of double-quotes (vs. apostrophes)

Like the objects we've created with classes, it has several methods, too…

Page 12: IAT 800

Oct 15, Fall 2009 IAT 800 12

String methods

From http://processing.org/reference/String.html

– length() • returns the size of the String (number of letters)

– charAt(number) • returns the char at an index number

– toUpperCase() and toLowerCase()• returns a copy of the String in UPPERCASE or lowercase

respectively.

– substring(beginIndex, endIndex)• returns a portion of the String from beginIndex to

endIndex-1String howdy = "Hello!"; String expletive = howdy.substring(0,4);

Page 13: IAT 800

Oct 15, Fall 2009 IAT 800 13

String concatenation

Concatenation is just a fancy word for slapping together to make one!

With Strings, this is done using the + symbol So, if you have:

You'll get out:

String s1 = "She is the "; String s2 = "programmer.“ ;

String sentence = s1 + "awesomest " + s2;

println(sentence); // sentence == "She is the awesomest programmer."

// outputs: She is the awesomest programmer.

Page 14: IAT 800

Oct 15, Fall 2009 IAT 800 14

MORE String concatenation You can also add in numbers, too!

There is also a function called nf() which can format your numbers (it stands for number format)

It has siblings! nfs(); nfp(); nfc(); Consult the reference.

String anothersentence = s1 + "#"+ 2 + " " + s2;// "She is the #2 programmer."

anothersentence = s1 + nf(7,3) + " " + s2;// nf( integer, number of digits )// "She is the 007 programmer."

anothersentence = s1 + nf(3.14159,3,2) + " " + s2;// nf( float, digits before decimal, digits after decimal )// "She is the 003.14 programmer."

Page 15: IAT 800

Oct 15, Fall 2009 IAT 800 15

Strings and Arrays

Did you know that you can take an Array of Strings and join it into one String?

Did you also know that you can split a String into an Array?

String[] a = { "One", "string", "to", "rule", "them", "all…" };

String tolkien = join(a, " ");// tolkien == "One string to rule them all…"

String b = "Another string to bind them…“ ;

String[] tolkien2= split(b, " ");// tolkien2 == { "Another", "string", "to", "bind", "them…" }

Page 16: IAT 800

Oct 15, Fall 2009 IAT 800 16

Special characters

Split based on spaces (" ")– tab: "\t"– new line: "\n"

– other escape characters include "\\" "\""

String twolines = "I am on one line.String twolines = "I am on one line.\n\n I am I am \t\ton another."on another."

I am on one line.I am on one line.I am on I am on another.another.

( \ tells the computer to look to ( \ tells the computer to look to the next character to figure out the next character to figure out what to do that's special.)what to do that's special.)

Page 17: IAT 800

Oct 15, Fall 2009 IAT 800 17

We started with Processing in…

// any code here, no methods

line(0,0,20,20);// methods!

// global varsint a;

// methodsvoid setup(){

}

void draw(){

}

// …with classes

// (all of the above and then)class Emotion {

//fields

//constructor

//methods

}

// …and subclasses!

// (ALL of the above, and…)

class Happy extends Emotion {

//new fields

//constructor

//methods

}

Page 18: IAT 800

Oct 15, Fall 2009 IAT 800 18

Processing is actually a Java Class

// Java-Mode!!!

class Uneasy extends PApplet {

// void setup() and void draw() as normally …

//methods

//classes and subclasses

}

Page 19: IAT 800

Oct 15, Fall 2009 IAT 800 19

Java Mode

Allows you to program in pure Java– Can import classes that aren’t normally imported into a

Processing app– Importing means making a classes available to your

program – the Java API docs tell you where classes are In Java mode, create a class that extends

PApplet– Normally, all Processing applets extend PApplet behind the

scenes

setup(), draw(), etc. are methods of the class extending PApplet

Page 20: IAT 800

Oct 15, Fall 2009 IAT 800 20

A Java-mode programclass MyProgram extends PApplet {

void setup() { … }void draw() { … }

void myTopLevelMethod() { … }

class Text { // Text is just an example int xPos, yPos;String word;…

}}

Notice that any classes you define are inside the top class

Page 21: IAT 800

Oct 15, Fall 2009 IAT 800 21

Why use Java-mode? Java-mode gives you access to the entire Java

SDK– We need access to some SDK classes for HTML parsing

that Processing doesn’t make visible by default

Java-mode helps you to understand how Processing is built on-top of Java– All those “magic” functions and variables are just

methods and fields of PApplet that your program inherits

Page 22: IAT 800

Oct 15, Fall 2009 IAT 800 22

Libraries!

Libraries are other classes (in .java or .jar files )– Use import nameoflibrary.nameofmethod;

(e.g., import video.*; ) Now with Java-mode, you can ALSO put your

programs in multiple files– A file for each class– Create new tabs (files) with that button in the

upper right

Page 23: IAT 800

Oct 15, Fall 2009 IAT 800 23

Recap

Strings Methods and

concatenation Strings and Arrays