Presentation is loading. Please wait.

Presentation is loading. Please wait.

Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems.

Similar presentations


Presentation on theme: "Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems."— Presentation transcript:

1 Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems

2 Jun 20, 2014IAT 2652 Topics  Strings  Java Mode

3 Jun 20, 2014IAT 2653 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

4 Jun 20, 2014IAT 2654 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…

5 Jun 20, 2014IAT 2655 String methods  From http://processing.org/reference/String.html 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);

6 Jun 20, 2014IAT 2656 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.

7 Jun 20, 2014IAT 2657 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."

8 Jun 20, 2014IAT 2658 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…" }

9 Jun 20, 2014IAT 2659 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 \ton 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.)

10 Jun 20, 2014IAT 26510 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 }

11 Jun 20, 2014IAT 26511 Processing is actually a Java Class // Java-Mode!!! class Uneasy extends PApplet { // void setup() and void draw() as normally … //methods //classes and subclasses }

12 Jun 20, 2014IAT 26512 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

13 Jun 20, 2014IAT 26513 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

14 Jun 20, 2014IAT 26514 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

15 Jun 20, 2014IAT 26515 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

16 Jun 20, 2014IAT 26516 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.

17 Jun 20, 2014IAT 26517 Principle: Code re-use  If an object already exists, you can use that object in your program.  Specialists build, you use

18 Jun 20, 2014IAT 26518 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!

19 Jun 20, 2014IAT 26519 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!

20 Jun 20, 2014IAT 26520 Summary of principles  Hide unnecessary details  Clearly define the interface  Allow and support code re-use  Build on the work of others

21 Jun 20, 2014IAT 26521 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

22 Jun 20, 2014IAT 26522 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

23 Jun 20, 2014IAT 26523 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

24 Jun 20, 2014IAT 26524 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!

25 Jun 20, 2014IAT 26525 The challenge  Software design is typically done top- down  Software implementation is typically done bottom-up

26 Jun 20, 2014IAT 26526 Recap  Strings  Methods and concatenation  Strings and Arrays  Code Reuse  Solving Problems


Download ppt "Jun 20, 2014IAT 2651 Strings, Java Mode Solving Problems."

Similar presentations


Ads by Google