26
Jun 20, 2014 IAT 265 1 IAT 265 Strings, Java Mode Solving Problems

Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Embed Size (px)

Citation preview

Page 1: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 1

IAT 265

Strings, Java ModeSolving Problems

Page 2: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 2

Topics

g Stringsg Java Mode

Page 3: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 3

Types

g 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 4: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 4

String details

g 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)

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

Page 5: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 5

String methods

g 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-1

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

Page 6: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 6

String concatenation

g Concatenation means – appending another string on the end

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

g 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 7: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 7

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

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

g 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 8: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 8

Strings and Arrays

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

g 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 9: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 9

Special characters

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

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

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

I am on one line.

I am on another.

( \ tells the computer to look to the next character to

figure out what to do that's special.)

Page 10: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 10

We started with Processing in…

// any code here, no methods

line(0,0,20,20);

// methods!

// global vars

int a;

// methods

void 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 11: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 11

Processing is actually a Java Class

// Java-Mode!!!

class Uneasy extends PApplet {

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

//methods

//classes and subclasses

}

Page 12: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 12

Java Mode

g 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 areg In Java mode, create a class that extends

PApplet– Normally, all Processing applets extend PApplet behind the

scenes

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

Page 13: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 13

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 14: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 14

Why use Java-mode?g 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

g 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 15: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 15

Libraries!

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

(e.g., import video.*; )g 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 16: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 16

Who cares?

g When you want to:– Solve the problem once and forget it– Reuse the solution elsewhere– Establish rules for use and change of data

g The principle:– Information hiding– By interacting only with an object's methods,

the details of its internal implementation remain hidden from the outside world.

Page 17: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 17

Principle: Code re-use

g If an object already exists, you can use that object in your program.

g Specialists build, you use

Page 18: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 18

Principle: Define the Interface

g Define the interface:– The list of methods with Defined

Operationg The interface is the thing that other

people useg If you have the same interface with

the same meaning– You can plug in a better

implementation!

Page 19: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 19

Define the Interface

g If you have the same interface with the same meaning– You can plug in a better

implementation!– You can plug in a More Interesting

implementation!

Page 20: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 20

Summary of principles

g Hide unnecessary detailsg Clearly define the interfaceg Allow and support code re-use

g Build on the work of others

Page 21: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 21

How do we build on other work?

g Divide and conquer– Cut the problem into smaller pieces– Solve those smaller problems– Aggregate the smaller solutions

g Two approaches:– Top-down– Bottom-up

Page 22: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 22

Top Down

g Take the big problem– Cut it into parts

• Analyze each part– Design a top-level solution that

presumes you have a solution to each part

g then…– Cut each part into sub-parts

Page 23: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 23

Bottom-up

g Cut the problem into parts, then sub-parts, then sub-sub parts…– build a solution to each sub-sub-part

• aggregate sub-sub solutions into a sub-solution

Page 24: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 24

How do we build on other work?

g Recognize the problem as another problem in disguise– It’s a sorting problem!– It’s a search problem!– It’s a translation problem!– It’s an optimization problem!

Page 25: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 25

The challenge

g Software design is typically done top-down

g Software implementation is typically done bottom-up

Page 26: Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

Jun 20, 2014 IAT 265 26

Recap

g Stringsg Methods and

concatenationg Strings and Arraysg Code Reuseg Solving Problems