Deprecated: Creation of dynamic property wpdb::$categories is deprecated in /var/www/twodee/blog/wp-includes/wp-db.php on line 668

Deprecated: Creation of dynamic property wpdb::$post2cat is deprecated in /var/www/twodee/blog/wp-includes/wp-db.php on line 668

Deprecated: Creation of dynamic property wpdb::$link2cat is deprecated in /var/www/twodee/blog/wp-includes/wp-db.php on line 668

Deprecated: Return type of WP_Theme::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-theme.php on line 554

Deprecated: Return type of WP_Theme::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-theme.php on line 595

Deprecated: Return type of WP_Theme::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-theme.php on line 535

Deprecated: Return type of WP_Theme::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-theme.php on line 544

Deprecated: Return type of WP_REST_Request::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/rest-api/class-wp-rest-request.php on line 960

Deprecated: Return type of WP_REST_Request::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/rest-api/class-wp-rest-request.php on line 980

Deprecated: Return type of WP_REST_Request::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/rest-api/class-wp-rest-request.php on line 992

Deprecated: Return type of WP_REST_Request::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/rest-api/class-wp-rest-request.php on line 1003

Deprecated: Return type of WP_Block_List::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 151

Deprecated: Return type of WP_Block_List::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 175

Deprecated: Return type of WP_Block_List::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 164

Deprecated: Return type of WP_Block_List::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 186

Deprecated: Return type of WP_Block_List::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 138

Deprecated: Return type of WP_Block_List::offsetExists($index) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 75

Deprecated: Return type of WP_Block_List::offsetGet($index) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 89

Deprecated: Return type of WP_Block_List::offsetSet($index, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 110

Deprecated: Return type of WP_Block_List::offsetUnset($index) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 127

Deprecated: Return type of WP_Block_List::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /var/www/twodee/blog/wp-includes/class-wp-block-list.php on line 199

Deprecated: Creation of dynamic property Automattic\Jetpack\Connection\Manager::$error_handler is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/vendor/automattic/jetpack-connection/src/class-manager.php on line 91

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Instagram_Gallery::$is_wpcom is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-instagram-gallery.php on line 23

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Mailchimp::$wpcom_is_wpcom_only_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-mailchimp.php on line 17

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Tweetstorm_Gather::$wpcom_is_wpcom_only_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-tweetstorm-gather.php on line 21

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Tweetstorm_Gather::$is_wpcom is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-tweetstorm-gather.php on line 22

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Gutenberg_Available_Extensions::$wpcom_is_site_specific_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/gutenberg-available-extensions.php on line 20

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Memberships::$wpcom_is_wpcom_only_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/memberships.php on line 23

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Memberships::$wpcom_is_site_specific_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/memberships.php on line 24

Deprecated: Creation of dynamic property WPCOM_REST_API_V2_Endpoint_Subscribers::$wpcom_is_wpcom_only_endpoint is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/subscribers.php on line 15

Deprecated: Creation of dynamic property Automattic\Jetpack\Sync\Queue::$random_int is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/vendor/automattic/jetpack-sync/src/class-queue.php on line 38

Deprecated: Creation of dynamic property Automattic\Jetpack\Sync\Queue::$random_int is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/vendor/automattic/jetpack-sync/src/class-queue.php on line 38

Deprecated: Creation of dynamic property WPCOM_Widget_Goodreads::$shelves is deprecated in /var/www/twodee/blog/wp-content/plugins/jetpack/modules/widgets/goodreads.php on line 33

Deprecated: DateTime::__construct(): Passing null to parameter #1 ($datetime) of type string is deprecated in /var/www/twodee/blog/wp-includes/script-loader.php on line 348

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_classic_block is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 347

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_block is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 349

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_block_side is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 350

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$panels_block is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 351

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$used_block_buttons is deprecated in /var/www/twodee/blog/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 354

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Block_Type::$plan_check is deprecated in /var/www/twodee/blog/wp-includes/class-wp-block-type.php on line 333

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /var/www/twodee/blog/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /var/www/twodee/blog/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /var/www/twodee/blog/wp-includes/class-wp-term.php on line 198
» CS 145 Lecture 7 – String Cont’d and Methods

teaching machines

CS 145 Lecture 7 – String Cont’d and Methods

September 21, 2016 by . Filed under
Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383

Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383

Deprecated: Creation of dynamic property WP_Term::$cat_ID is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 378

Deprecated: Creation of dynamic property WP_Term::$category_count is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 379

Deprecated: Creation of dynamic property WP_Term::$category_description is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 380

Deprecated: Creation of dynamic property WP_Term::$cat_name is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 381

Deprecated: Creation of dynamic property WP_Term::$category_nicename is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 382

Deprecated: Creation of dynamic property WP_Term::$category_parent is deprecated in /var/www/twodee/blog/wp-includes/category.php on line 383
cs145, fall 2016, lectures.

Dear students,

With Strings at our disposal, our programs are going to start to feel human. We can write programs that process and generate words, and that’s pretty amazing! But we need more practice using them before we determine the authenticity of Shakespeare’s works. Let’s play some Stringo!

Generate a String of five characters. Use lowercase letters, numbers, and punctuation—all your choice. Label each character with its index. We’ll refer to your String through identifier s.

I will call out a series of predicates that describe parts of s. If a predicate is true for your String cross off the characters involved in making the predicate true. Write the predicate number next to the characters that it crossed off. First one to cross off all five characters wins a meager prize and enduring fame. Here are the predicates:

 0: s.charAt(0) is equal to s.charAt(s.length() - 1)
 1: s.charAt(3) is a vowel
 2: s.indexOf('a') % 2 is 0
 3: s.charAt(3) >= 'm' and s.charAt(3) <= 'z'
 4: s.indexOf('f') <= 3
 5: s.charAt(i) is punctuation and s.charAt(i + 1) is a letter
 6: s.substr(3) is all numbers
 7: s.substr(2, 4).charAt(0) is not a letter
 8: s.indexOf('a') + 1 < 5
 9: s.indexOf('o') < s.indexOf('k')
10: s.charAt(1) does not equal s.charAt(4)
11: s.charAt(i) is equal to s.charAt(i + 1)
12: s.indexOf('5') >= 0
13: s.indexOf('%') % 3 <= 1
14: s.charAt(i % 2) is a consonant

Next we’ll write a MADLIB, an activity which I enjoyed far too much when I was a kid. We’ll take a piece of text that has had a few words extracted from it, and then we’ll ask the user to replace those words with their own inputs of the same grammatical category.

Finally, we introduce methods. So far, all our has code has been tucked away inside of main. Eventually, we will outgrow this method. Today, in fact.

Sometimes we write code that we want to execute again and again. Let’s see an example of this. We will generate some random circle art using the SVG protocol. An outlined circle looks like this in SVG:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<circle cx="CENTERX" cy="CENTERY" r="RADIUS" stroke="black" stroke-width="2" fill="rgb(RED, GREEN, BLUE)"/>
</svg>

The capitalized words are just placeholders. The first three are in pixel units, and the last three are in [0, 255]. Let’s start by filling them in with random values. We’ll use the Random class this time, because unlike Math.random, it can generate ints for us.

Once we get that working, let’s add a second random circle. And a third. And a fourth. And so on. We’ll find that the code quickly gets away from us if we simply copy and paste. Instead, we factor out the repeated code to a method. For the time being, our programs with multiple methods will have this structure:

public class ClassWithMultipleMethods {
  public static void main(String[] args) {
    // ...
    helperMethodA();
    // ...
    helperMethodB();
    // ...
  }

  public static void helperMethodA() {
    // ...
  }

  public static void helperMethodB() {
    // ...
  }
}

Let’s make a method that generates a random circle and call it a bunch of times!

Here’s your TODO list to complete before next class:

See you next class!

Sincerely,

P.S. Here’s the code we wrote together in class…

Madlib.java

package lecture0921;

import java.util.Scanner;

public class Madlib {
  public static void main(String[] args) {
    String messageWithBlanks = String.format("Desperately looking for my lost NOUN1 which I%nADVERB placed on the NOUN2 of my NOUN3%nand then PASTVERB away. It contains all of my%nNOUN4 (including my thesis), NOUN5, music, documents, and%nwork material. It fell off on Sept 10th probably somewhere%nin the PLACE1 or along Clairemont on the way to%nPLACE2. It was in a ADJECTIVE1 ADJECTIVE2 case and had some%npapers along with it that will prove it belongs to PERSON.%n%nPlease, please, pleeeease let me know if you%nhave seen it or have found it!%n%nThank you!");
    String message = String.format("Desperately looking for my lost computer which I absentmindedly placed on roof of my car and then drove away. It contains all of my school work (including my thesis), photos, music, documents, and work material. It fell off on Sept 10th probably somewhere in the Third Ward or along Clairemont on the way to Minneapolis. It was in a black soft-shell case and had some papers along with it that will prove it belongs to me.%n%nPlease, please, pleeeease let me know if you have seen it or have found it!%n%nThank you!");

    Scanner in = new Scanner(System.in);

    System.out.print("NOUN: ");
    String noun1 = in.nextLine();

    System.out.print("ADVERB: ");
    String adverb = in.nextLine();

    System.out.print("NOUN: ");
    String noun2 = in.nextLine();

    System.out.print("NOUN: ");
    String noun3 = in.nextLine();

    System.out.print("PAST VERB: ");
    String verb = in.nextLine();

    System.out.print("NOUN: ");
    String noun4 = in.nextLine();

    System.out.print("NOUN: ");
    String noun5 = in.nextLine();

    System.out.print("PLACE: ");
    String place1 = in.nextLine();

    System.out.print("PLACE: ");
    String place2 = in.nextLine();

    System.out.print("ADJECTIVE: ");
    String adjective1 = in.nextLine();

    System.out.print("ADJECTIVE: ");
    String adjective2 = in.nextLine();

    System.out.print("PERSON: ");
    String person = in.nextLine();

    messageWithBlanks = messageWithBlanks.replace("NOUN1", noun1);
    messageWithBlanks = messageWithBlanks.replace("ADVERB", adverb);
    messageWithBlanks = messageWithBlanks.replace("NOUN2", noun2);
    messageWithBlanks = messageWithBlanks.replace("NOUN3", noun3);
    messageWithBlanks = messageWithBlanks.replace("PASTVERB", verb);
    messageWithBlanks = messageWithBlanks.replace("NOUN4", noun4);
    messageWithBlanks = messageWithBlanks.replace("NOUN5", noun5);
    messageWithBlanks = messageWithBlanks.replace("PLACE1", place1);
    messageWithBlanks = messageWithBlanks.replace("PLACE2", place2);
    messageWithBlanks = messageWithBlanks.replace("ADJECTIVE1", adjective1);
    messageWithBlanks = messageWithBlanks.replace("ADJECTIVE2", adjective2);
    messageWithBlanks = messageWithBlanks.replace("PERSON", person);

    System.out.println(messageWithBlanks);

    /*
     * NOUN: car ADVERB: quickly NOUN: Chris NOUN: fish PAST VERB: ran NOUN:
     * dog NOUN: Chancellor Jim PLACE: basement PLACE: Mars ADJECTIVE: rusty
     * ADJECTIVE: spooky PERSON: JOHN CENA Desperately looking for my lost
     * car which I quickly placed on the Chris of my fish and then ran away.
     * It contains all of my dog (including my thesis), Chancellor Jim,
     * music, documents, and work material. It fell off on Sept 10th
     * probably somewhere in the basement or along Clairemont on the way to
     * Mars. It was in a rusty spooky case and had some papers along with it
     * that will prove it belongs to JOHN CENA.
     * 
     * Please, please, pleeeease let me know if you have seen it or have
     * found it!
     * 
     * Thank you!
     */
  }
}

Circles.java

package lecture0921;

import java.util.Random;

public class Circles {
  public static void main(String[] args) {
    System.out.println("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">");
    
    Random generator = new Random();
    int originX = generator.nextInt(1000);
    int originY = generator.nextInt(800);
    int radius = generator.nextInt(600);
    int r = generator.nextInt(256);
    int g = generator.nextInt(256);
    int b = generator.nextInt(256);

    System.out.printf("<circle cx=\"%d\" cy=\"%d\" r=\"%d\" stroke=\"black\" stroke-width=\"2\" fill=\"rgb(%d, %d, %d)\"/>%n", originX, originY, radius, r, g, b);
    
    System.out.println("</svg>");
  }
}