IAT 265 1 IAT 265 Strings Java Mode Solving Problems July 16 2015 IAT 265 2 Topics Strings Java Mode July 16 2015 IAT 265 3 Types You may recall when we talked about types Primitives ID: 309382
Download Presentation The PPT/PDF document "July 16, 2015" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
July 16, 2015
IAT 265
1
IAT 265
Strings, Java Mode
Solving ProblemsSlide2
July 16, 2015
IAT 265
2
Topics
Strings
Java ModeSlide3
July 16, 2015
IAT 265
3
Types
You may recall when we talked about
types
Primitives
int, float, byte
boolean
char
Objects (composites)
Array
ArrayList
PImage
(any object you create)
StringsSlide4
July 16, 2015
IAT 265
4
String details
A string is
almost
like an array of
char
s
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…Slide5
July 16, 2015
IAT 265
5
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!"; String expletive = howdy.substring(0,4);Slide6
July 16, 2015
IAT 265
6
String concatenation
Concatenation means – appending another string on the end
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.Slide7
July 16, 2015
IAT 265
7
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."Slide8
July 16, 2015
IAT 265
8
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…" }Slide9
July 16, 2015
IAT 265
9
Special characters
Split based on spaces (" ")
tab: "\t"
new line: "\n"
other
escape characters
include
"\\" "\""
String twolines = "I am on one line.
\n
I am
\t
on 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.)Slide10
July 16, 2015
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
}Slide11
July 16, 2015
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
}Slide12
July 16, 2015
IAT 265
12
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 PAppletSlide13
July 16, 2015
IAT 265
13
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 classSlide14
July 16, 2015
IAT 265
14
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 Slide15
July 16, 2015
IAT 265
15
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 rightSlide16
July 16, 2015
IAT 265
16
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. Slide17
July 16, 2015
IAT 265
17
Principle: Code re-use
If an object already exists, you can use that object in your program.
Specialists build,
you useSlide18
July 16, 2015
IAT 265
18
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!Slide19
July 16, 2015
IAT 265
19
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!Slide20
July 16, 2015
IAT 265
20
Summary of principles
Hide unnecessary details
Clearly define the interface
Allow and support code re-use
Build on the work of othersSlide21
July 16, 2015
IAT 265
21
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-upSlide22
July 16, 2015
IAT 265
22
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-partsSlide23
July 16, 2015
IAT 265
23
Bottom-up
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-solutionSlide24
July 16, 2015
IAT 265
24
How do we build on other work?
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!Slide25
July 16, 2015
IAT 265
25
The challenge
Software design is typically done top-down
Software implementation is typically done bottom-upSlide26
July 16, 2015
IAT 265
26
Recap
Strings
Methods and concatenation
Strings and Arrays
Code Reuse
Solving Problems