Strings, Java Mode

Download Report

Transcript Strings, Java Mode

IAT 265
Strings, Java Mode
Solving Problems
July 16, 2015
IAT 265
1
Topics
 Strings
 Java
July 16, 2015
Mode
IAT 265
2
Types

You may recall when we talked about types
– Primitives
• int, float, byte
• boolean
• char
– Objects (composites)
•
•
•
•
•
July 16, 2015
Array
ArrayList
PImage
(any object you create)
Strings
IAT 265
3
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…
July 16, 2015
IAT 265
4
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-1
String howdy = "Hello!";
July 16, 2015
String expletive = howdy.substring(0,4);
IAT 265
5
String concatenation



Concatenation means – appending another string on the
end
With Strings, this is done using the + symbol
So, if you have:
String s1 = "She is the ";
String s2 = "programmer." ;
String sentence = s1 + "awesomest " + s2;

You'll get out:
println(sentence); // sentence == "She is the awesomest programmer."
// outputs: She is the awesomest programmer.
July 16, 2015
IAT 265
6
MORE String
concatenation

You can also add in numbers, too!
String anothersentence = s1 + "#"+ 2 + " " + s2;
// "She is the #2 programmer."

There is also a function called nf() which can
format your numbers (it stands for number
format)
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."

It has siblings! nfs(); nfp(); nfc(); Consult the
reference.
July 16, 2015
IAT 265
7
Strings and Arrays
 Did
you know that you can take an Array
of Strings and join it into one String?
String[] a = { "One", "string", "to", "rule", "them", "all…" };
String tolkien = join(a, " ");
// tolkien == "One string to rule them all…"
 Did
you also know that you can split a
String into an Array?
String b = "Another string to bind them…" ;
String[] tolkien2= split(b, " ");
// tolkien2 == { "Another", "string", "to", "bind", "them…" }
July 16, 2015
IAT 265
8
Special characters
 Split
based on spaces (" ")
– tab: "\t"
– new line: "\n"
( \ tells the computer to look to the
next character to figure out what to
do that's special.)
String twolines = "I am on one line.\n I am \ton another."
I am on one line.
I am on
another.
– other escape characters include "\\" "\""
July 16, 2015
IAT 265
9
We started with Processing
in…
// any code here, no methods
// methods!
line(0,0,20,20); // …with classes
// global vars
// …and subclasses!
int a;
// (all of the above and then)
// (ALL
class Emotion
{ of the above, and…)
// methods
void setup(){
class Happy extends Emotion {
//fields
}
//new fields
//constructor
void draw(){
//methods //constructor
}
July 16, 2015
}
//methods
IAT 265
}
10
Processing is actually a
Java Class
// Java-Mode!!!
class Uneasy extends PApplet {
// void setup() and void draw() as normally …
//methods
//classes and subclasses
}
July 16, 2015
IAT 265
11
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
July 16, 2015
IAT 265
12
A Java-mode program
class 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
July 16, 2015
IAT 265
13
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
July 16, 2015
IAT 265
14
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
July 16, 2015
IAT 265
15
Who cares?

When you want to:
– Solve the problem once and forget it
– Reuse the solution elsewhere
– Establish rules for use and change of data

The principle:
– Information hiding
– By interacting only with an object's methods, the
details of its internal implementation remain hidden
from the outside world.
July 16, 2015
IAT 265
16
Principle: Code re-use
 If
an object already exists, you can use
that object in your program.
 Specialists build, you use
July 16, 2015
IAT 265
17
Principle: Define the Interface
 Define
the interface:
– The list of methods with Defined Operation
 The
interface is the thing that other people
use
 If you have the same interface with the
same meaning
– You can plug in a better implementation!
July 16, 2015
IAT 265
18
Define the Interface
 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!
July 16, 2015
IAT 265
19
Summary of principles
 Hide
unnecessary details
 Clearly define the interface
 Allow and support code re-use
 Build
July 16, 2015
on the work of others
IAT 265
20
How do we build on other work?
 Divide
and conquer
– Cut the problem into smaller pieces
– Solve those smaller problems
– Aggregate the smaller solutions
 Two
approaches:
– Top-down
– Bottom-up
July 16, 2015
IAT 265
21
Top Down
 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
 then…
– Cut each part into sub-parts
July 16, 2015
IAT 265
22
Bottom-up
 Cut
the problem into parts, then subparts, then sub-sub parts…
– build a solution to each sub-sub-part
• aggregate sub-sub solutions into a sub-solution
July 16, 2015
IAT 265
23
How do we build on other work?
 Recognize
the problem as another
problem in disguise
– It’s
– It’s
– It’s
– It’s
July 16, 2015
a sorting problem!
a search problem!
a translation problem!
an optimization problem!
IAT 265
24
The challenge
 Software
design is typically done top-
down
 Software implementation is typically done
bottom-up
July 16, 2015
IAT 265
25
Recap
Strings
 Methods and
concatenation
 Strings and Arrays
 Code Reuse
 Solving Problems

July 16, 2015
IAT 265
26