Words

17
Words

description

Words. Characters and Strings. Character A single character inside of single quotes char letter = 'A' ; char digit = '0' ; Strings Zero or more character in double quotes String word = "hello" ; String robot = "r2d2" ; String nothing = "" ;. Why not string ?. String is a class - PowerPoint PPT Presentation

Transcript of Words

Page 1: Words

Words

Page 2: Words

Characters and Strings

• Character– A single character inside of single quotes

char letter = 'A' ;

char digit = '0' ;

– Strings

• Zero or more character in double quotesString word = "hello" ;

String robot = "r2d2" ;

String nothing = "" ;

Page 3: Words

Why not string?

• String is a class– Classes define data types– Names of classes start with capital letters

• A convention, not a rule

• Primitive types– Simplest data types of Java– int, boolean, char, double– Names start with small letters

Page 4: Words

Odd things to do

println('A' + 5) ;println("A" + 5) ;String s = "" ;for (char c = '0'; c < 'Z'; c++) { s = s + c ;}println(s) ;

Page 5: Words

'5' vs 5 vs "5"

• '5' is a character encoded as a number– But given from the ASCII standard– That is, '5' is 35 (in Hexadecimal)

• 5 is the numbers of fingers on one hand• "5" is a String

– With the single character '5'

Page 6: Words

'v' vs v

• 'v' is a character– You cannot do the following

'v' = 30 ;

• v is a variable name– If appropriately declared

int v ;

– You canv = 30 ;

Page 7: Words

Conversion methods

boolean– Converts zero to false– Converts non-zero to true

bytecharintfloatstr

– Converts to String

Page 8: Words

Conversion example

println(boolean(5)) ;println(float(5)) ;println(str(5)) ;println(int(5.7)) ;println(str(5.7)) ;println(int('5')) ;println(int('#')) ;

Page 9: Words

Unicode and cut-and-paste

char rquote = '‘' ;char lquote = '’' ;char singleQuote = '\'';String dquotes = "“”" ;char oe = 'œ' ;println(int(rquote)) ;println(int(oe)) ;

Page 10: Words

class and method

• In object-oriented programming– Classes have methods

• ≈ functions• The operations for that data type

– Retrieve information from objects– Modify objects

– Methods (and fields) use “dot” operator• obj.method(arg1, arg2)

– ≈ method(obj, arg1, arg2)

Page 11: Words

Useful methods for s, a String

s.length()– Number of characters of s

s.charAt(5)– 5th character of s (“first” is 0th)

s.substring(2, 3)– Characters 2 to 4 of s

• And many more

Page 12: Words

Printing on the screen

• Create a font in Processing environment– Tools » Create Font….

• Load font in Processing code– Pfont font = loadFont("……") ;

• Set font– textFont(font, size) ;

• Write with font– text("……", x, y) ;

Page 13: Words

Displaying Text

// Step 1: Create the font using the Tool menu option// Step 2: Declare PFont variable PFont f;

void setup() { size(200,200); // Step 3: Load Font f = loadFont("Albany-48.vlw“, 40); background(255); textFont(f, 16); // Step 4: Specify font fill(0); // Step 5: Specify font color // Step 6: Display Text text("Mmmmm ... Strings ..." , 10, 100); }

Page 14: Words

You try it!

// Step 1: Create the font using the Tool menu option// Step 2: Declare PFont variable PFont f;

void setup() { size(200, 200); // Step 3: Load Font f = loadFont( “you fill this in" ); background(255); textFont(f, “you fill this in”); // Step 4: Specify font fill(0); // Step 5: Specify font color // Step 6: Display Text String message = “you fill this in" ; text (message, 10, 100); }

Page 15: Words

Useful text functions

textWidth("……")– Returns width in pixels of string when printed

textAlign(LEFT)textAlign(RIGHT)textAlign(CENTER)

– Aligns a text string

textSize(n)– Sets size of font

Page 16: Words

Try It Again

PFont f;

void setup() {

size(200,200);

f = loadFont("Arial-BoldMT-40.vlw");

textFont(f);

fill(200);

}

void draw() {

background(255);

if(mousePressed && mouseButton == LEFT) {

textAlign(LEFT);

text ("LEFT", 5, height/2, width, height);

}

else if(mousePressed && mouseButton == RIGHT) {

textAlign(RIGHT);

text ("RIGHT", 5, height/2, width, height);

}

}

Page 17: Words

Lab

• Load an image of a dog and font into your project

• Write a Processing program to display the dog along with its name

• If you have time, animate your dog