1 / 26

IAT 265

IAT 265. Strings, Java Mode Solving Problems. Topics. Strings Java Mode. Types. You may recall when we talked about types Primitives int, float, byte boolean char Objects (composites) Array ArrayList PImage (any object you create) Strings. String details.

sirvat
Download Presentation

IAT 265

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. IAT 265 Strings, Java Mode Solving Problems IAT 265

  2. Topics • Strings • Java Mode 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) • Strings IAT 265

  4. 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… 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 beginIndexto endIndex-1 String howdy = "Hello!"; String expletive = howdy.substring(0,4); 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. 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." 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…" } IAT 265

  9. Special characters • Split based on spaces (" ") • tab: "\t" • new line: "\n" • other escape characters include"\\" "\"" ( \ 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. IAT 265

  10. We started with Processing in… // any code here, no methods line(0,0,20,20); // methods! // global varsint a; // methodsvoid 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 } 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 } 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 PApplet 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 class 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 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 right 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. IAT 265

  17. Principle: Code re-use • If an object already exists, you can use that object in your program. • Specialists build, you use 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! 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! IAT 265

  20. Summary of principles • Hide unnecessary details • Clearly define the interface • Allow and support code re-use • Build on the work of others 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-up 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-parts 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-solution 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! IAT 265

  25. The challenge • Software design is typically done top-down • Software implementation is typically done bottom-up IAT 265

  26. Recap • Strings • Methods and concatenation • Strings and Arrays • Code Reuse • Solving Problems IAT 265

More Related