1 / 17

Words

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

Download Presentation

Words

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Words

  2. 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 = "" ;

  3. 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

  4. Odd things to do println('A' + 5) ; println("A" + 5) ; String s = "" ; for (char c = '0'; c < 'Z'; c++) { s = s + c ; } println(s) ;

  5. '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'

  6. '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 can v = 30 ;

  7. Conversion methods boolean • Converts zero to false • Converts non-zero to true byte char int float str • Converts to String

  8. Conversion example println(boolean(5)) ; println(float(5)) ; println(str(5)) ; println(int(5.7)) ; println(str(5.7)) ; println(int('5')) ; println(int('#')) ;

  9. Unicode and cut-and-paste char rquote = '‘' ; char lquote = '’' ; char singleQuote = '\''; String dquotes = "“”" ; char oe = 'œ' ; println(int(rquote)) ; println(int(oe)) ;

  10. 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)

  11. 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

  12. 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) ;

  13. 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); }

  14. 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); }

  15. 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

  16. 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); } }

  17. 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

More Related