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
» CS1: Lecture 9 – Methods with Return Values

teaching machines

CS1: Lecture 9 – Methods with Return Values

September 23, 2019 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
cs1, fall 2019, lectures.

Dear students,

Today we dive deeper into our discussion of writing our own methods. We’ve actually been using methods all semester long; it’s just that now we’re starting to write our own. Before we dive in, let’s revisit our friend String.

Stringo

With Strings at our disposal, our programs 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.substring(3) is all numbers
 7: s.substring(2, 4).charAt(0) is not a letter
 8: s.indexOf('e') + 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('t') >= 0
13: s.indexOf(s.charAt(2)) is 2
14: s.charAt(i % 2) is a consonant

Methods

Last time we saw how methods turn a sequence of code into a reusable component. Methods have some really nice advantages:

That insulation is our concern for today. Most programming languages have a notion of insulation, which leads us to our next decree.

Decree 11: Data has a lifetime and a “lifespace.”

When we declare a variable, it’s scope begins. That variable is only accessible until the closing curly brace of the block in which it was declared. Methods have independent scopes and are therefore insulated from each other. But if methods can’t access each other’s data, how do they share stuff across the Wall of Braces? Through two mechanisms: return values and parameters.

I like to think of methods as vending machines. From the perspective of a customer, I drop some things in (the parameters), and some unhealthy food pops out (the return value). What happens in between is largely a mystery to me.

We will be more than customers, however. We will design our own vending machines.

Let’s discuss return values first.

Return Values

We saw earlier that we can “mail” data to other code. Guess what? The other code can reply!

Decree 12: Named code can mail data back to its caller.

Mailing data back is called returning. To write a method that returns a result to the customer or client or caller of the method, we must do a couple of things:

  1. Announce the type of data that will be sent back. This is announced right before the method’s name, where we’ve been writing void up to this point.
  2. Add a statement that catapults the data back to the caller, clearing the Wall of Braces.

We’ll now start to see this pattern in our code:

public static RETURN-TYPE methodName() {
  ...
  return SOME-EXPRESSION-HAVING-RETURN-TYPE;
}

Let’s write a method that generates a random letter, perhaps used to assign someone to a group. We could write it like this:

public static char getRandomLetter() {
  Random generator = new Random();
  int ordinal = generator.nextInt(26);
  char letter = (char) ('A' + ordinal);
  return letter;
}

Or we could generate a random index into a String holding the alphabet:

public static char getRandomLetter() {
  Random generator = new Random();
  String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  char letter = alphabet.charAt(generator.nextInt(26));
  return letter;
}

Parameters

Generally, we add parameters to let the caller influence the behavior of the method. In a vending machine, for example, we give the customer some choice of what will be dispensed. They might enter C7 to get Reese’s Peanut Butter Cups—which is the only unhealthy food worth eating.

We add parameters by declaring—but not assigning—variables in the method’s parentheses:

public static RETURN-TYPE methodName(TYPE1 id1, TYPE2 id2, ...) {
  ...
  return SOME-EXPRESSION-HAVING-RETURN-TYPE;
}

These variables are now available to the statements of the method. In the case of getRandomLetter, it’d be nice to generalize the method so that the user could decide alphabet to choose from. We do this like so:

public static char getRandomLetter(String alphabet) {
  Random generator = new Random();
  char letter = alphabet.charAt(generator.nextInt(alphabet.length()));
  return letter;
}

Now in the caller we must supply the value for alphabet:

System.out.println(getRandomLetter("ABCDEFGHIJKLMNPOQRSTUVWXYZ"));
System.out.println(getRandomLetter("AB"));
System.out.println(getRandomLetter("HT"));
System.out.println(getRandomLetter("HHHHHHHHHHHHHHHT"));

Let’s write another method, one that yields the number of digits in a number. What is the return type? int. What parameters must we accept? A single int. There are several ways to answer this. We’ll convert the number to a String:

public static int getDigitCount(int n) {
  String nAsString = "" + Math.abs(n);
  return n.length();
}

We could also have solved this with logarithms. Or a loop that chisels away at the number digit by digit until we get to 0.

TODO

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

See you next class!

Sincerely,

P.S. It’s time for a haiku!

A magic 8-ball
Without question, it answers
That’s some good magic

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

Scope.java

package lecture0923.cs145;

public class Scope {
//  public static void methodA() {
//    int x = 10;
//  }
//
//  public static void methodB() {
//    System.out.println(x);
//  }
}

RandomLetter.java

package lecture0923.cs145;

import java.util.Random;

public class RandomLetter {
//  public static RETURN-TYPE name() {
//    ...
//    return SOME-EXPRESSION-OF-RETURN-TYPE;
//  }
//
//  public static int name() {
//    return 5;
//    return nPeople;
//    return generator.nextInt();
//  }

  public static char getRandomLetter(String alphabet) {
    Random lucky = new Random();
    int i = lucky.nextInt(alphabet.length());
    return alphabet.charAt(i);
  }

  public static char getRandomLetter() {
    String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    Random lucky = new Random();
    int i = lucky.nextInt(alphabet.length());
    return alphabet.charAt(i);
  }

  public static char getRandomLetter2() {
    Random lucky = new Random();
    int i = lucky.nextInt(26);
    char c = (char) ('a' + i);
    return c;
  }

  public static void main(String[] args) {
    System.out.println(getRandomLetter("HT"));
    System.out.println(getRandomLetter("HTTTTTTTTTTTTTT"));
  }
}

RandomLetter.java

package lecture0923.cs148;

import java.util.Random;

public class RandomLetter {
//  public static RETURN-TYPE name() {
//    ...
//    return SOME-EXPRESSION-OF-RETURN-TYPE;
//    return 6;
//    return 6 * 6;
//    return generator.nextInt();
//  }

  public static char getRandomLetter(String alphabet) {
    Random rando = new Random();
    int i = rando.nextInt(alphabet.length());
    return alphabet.charAt(i);
  }

  public static char getRandomLetter() {
    Random rando = new Random();
    int i = rando.nextInt(26);
    String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    return alphabet.charAt(i);
  }

  public static char getRandomLetter2() {
    Random rando = new Random();
    int i = rando.nextInt(26);
    char c = (char) (i + 'A');
    return c;
  }

  public static void main(String[] args) {
    for (int i = 0; i < 10; ++i) {
      System.out.println(getRandomLetter("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHT"));
    }
  }
}