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 8 – Methods

teaching machines

CS1: Lecture 8 – Methods

September 20, 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,

Last time we distinguished between syntax (the grammatical rules) and semantics (the meanings) that guide our programs. When we invoke a method on an object—as in object.method()—we must know or learn the semantics of method if we want our program to do the right thing.

With semantics in mind, let’s look at one more What’s Wrong with This?

Scanner in = new Scanner(System.in);

System.out.print("# of repetitions: ");
int i = in.nextInt();

System.out.print("Line to be repeated: ");
String line = in.nextLine();

String repeated = line.repeat(i);
System.out.println(repeated);

Scanner

Mixing nextLine and nextInt (or the other single token next methods) can cause trouble if you don’t know the underlying semantics of these methods. Suppose we enter this input into the program above:

5¶
foo¶

When the Scanner first starts up, it positions a “cursor” at the beginning of the input.

|5¶
foo¶

On calling nextInt, the Scanner advances the cursor along until it hits the first whitespace character. It consumes the digits, but not the whitespace. Thus after the call, the cursor is sitting here:

5|¶
foo¶

On calling nextLine, the Scanner advances the cursor along until it hits the first linebreak, which it does immediately. It returns the empty string and consumes the linebreak:

5¶
|foo¶

The text "foo" is never read.

If you are mixing token- and line-oriented input, you may need to insert a call to nextLine to advance the cursor. None of the programs in homework 1 require you to perform line-oriented input, so you shouldn’t need to do this in your code. However, some of you are seeing this weird behavior in lab.

Random

Let’s a new data type to our repertoire: a random number generator. If I tell you that the name of this type is Random, could you create a new variable that is capable of generating random numbers?

You should be able to. We can tell by its capitalization that it is a class, rather than a primitive, and its should be declared and assigned like this:

Random generator = new Random();

Okay, we have a random number generator. Now how do we use it? In particular, how do we find information on to use it? I can think of a few ways:

  1. Search the web for java CLASSNAME.
  2. Create a variable of the desired type, then use IntelliJ’s autocompletion to see what methods are available.
  3. Click inside the name of a class in IntelliJ, and click View / External Documentation to bring up the documentation. This only works if you’ve added the URL to the documentation in File / Project Structure / Platform Settings / SDKs / Documentation Paths.

We will use Random many times this semester, because random behavior adds an element of surprise to otherwise boring programs. But now we move on to the next big idea of this class. We enter the Computer as Chef chapter of our lives together.

Computer as Chef

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.

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>");

Once we get that working, let’s add a second random circle. And a third. And a fourth. And so on.

System.out.println("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">");
Random generator = new Random();

// First circle
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);

// Second circle
originX = generator.nextInt(1000);
originY = generator.nextInt(800);
radius = generator.nextInt(600);
r = generator.nextInt(256);
g = generator.nextInt(256);
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>");

We’ll find that the code quickly gets away from us if we simply copy and paste. That leads us to…

Decree 9: Code can be named.

Instead of repeating ourselves, 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! We’ll move the code outside of main into a helper method. That helper method will have a very narrow purpose: generate one circle. Then in main, we’ll call our own method a bunch of times.

public class Circles {
  public static void main(String[] args) {
    System.out.println("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">");
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    generateCircle();
    System.out.println("</svg>");
  }

  public static void generateCircle() {
    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);
  }

This is much better. There’s a mantra in software development that’s worth repeating here: don’t repeat yourself.

One thing that’s less than awesome, however, is that every time we invoke generateCircle, we create a brand new Random. That’s wasteful. The designers of Java anticipated this and established this rule:

Decree 10: Code can have its own data, or it can borrow data from its caller.

Borrowed data comes to a method in the way of parameters.

Parameters

Let’s seems smarter for us to just create one up front, and then share it. But this brings in an issue of variable scope. We’ll solve it using parameters.

Parameters are variable declarations. They declare data that the method does not have. If we rewrite generateCircle to not have a Random, we have this:

public static void generateCircle() {
  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);
}

This code complains of an unknown generator. To declare it as coming from the outside world, we add it as a parameter:

public static void generateCircle(Random generator) {
  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);
}

Now, when we call generateCircle back in main, we must supply something that fits the Random type. We could do this:

generateCircle(new Random());
generateCircle(new Random());
generateCircle(new Random());
generateCircle(new Random());

But that would be silly. Let’s make a single, reusable Random:

Random lucky = new Random();
generateCircle(lucky);
generateCircle(lucky);
generateCircle(lucky);
generateCircle(lucky);

TODO

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

See you next time!

Sincerely,

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

Named things live longer
That’s why I named my goldfish
Hope, Les, Lee, Slim, Chance

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

Foofoofoofoofoo.java

package lecture0920.cs145;

import java.util.Scanner;

public class Foofoofoofoofoo {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.print("# of repetitions: ");
    int i = in.nextInt();
    in.nextLine();

    System.out.print("Line to be repeated: ");
    String line = in.nextLine();

    String repeated = line.repeat(i);
    System.out.println(repeated);
  }
}

Circler.java

package lecture0920.cs145;

import java.util.Random;

public class Circler {
  public static void main(String[] args) {
    System.out.printf("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">%n");

    Random lucky = new Random();

    generateCircle(lucky);
    generateCircle(lucky);
    generateCircle(lucky);
    generateCircle(lucky);

    System.out.printf("</svg>%n");
  }

  public static void generateCircle(Random lucky) {
    int centerX = lucky.nextInt(2000);
    int centerY = lucky.nextInt(1100);
    int radius = lucky.nextInt(100);

    int r = lucky.nextInt(56) + 200;
    int g = lucky.nextInt(101);
    int b = lucky.nextInt(101);

    System.out.printf("<circle cx=\"%d\" cy=\"%d\" r=\"%d\" stroke=\"black\" stroke-width=\"0\" fill=\"rgb(%d, %d, %d)\"/>%n", centerX, centerY, radius, r, g, b);
  }
}

Foofoofoofoo.java

package lecture0920.cs148;

import java.util.Scanner;

public class Foofoofoofoo {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.print("# of repetitions: ");
    int i = in.nextInt();
    in.nextLine();

    System.out.print("Line to be repeated: ");
    String line = in.nextLine();

    String repeated = line.repeat(i);
    System.out.println(repeated);
  }
}

Circler.java

package lecture0920.cs148;

import java.util.Random;

public class Circler {
  public static void main(String[] args) {
    System.out.printf("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">%n");

    Random randall = new Random();
    for (int i = 0; i < 4000; ++i) {
      generateCircle(randall);
    }

    System.out.printf("</svg>%n");
  }

  public static void generateCircle(Random generator) {
    int centerX = generator.nextInt(1920);
    int centerY = generator.nextInt(1080);
    int radius = generator.nextInt(100);
    int r;
    int g = generator.nextInt(40);
    int b;
    if (generator.nextBoolean()) {
      r = generator.nextInt(200) + 55;
      b = 255 - r;
    } else {
      b = generator.nextInt(200) + 55;
      r = 255 - b;
    }
    System.out.printf("<circle cx=\"%d\" cy=\"%d\" r=\"%d\" stroke=\"black\" stroke-width=\"2\" fill=\"rgb(%d, %d, %d)\"/>%n", centerX, centerY, radius, r, g, b);
  }
}

Utilities.java

package lecture0920.cs148;

import java.util.Random;

public class Utilities {
  public static void generateCircle() {
    Random randall = new Random();
    int centerX = randall.nextInt(1920);
    int centerY = randall.nextInt(1080);
    int radius = randall.nextInt(100);
    int r = randall.nextInt(256);
    int g = randall.nextInt(256);
    int b = randall.nextInt(256);
    System.out.printf("<circle cx=\"%d\" cy=\"%d\" r=\"%d\" stroke=\"black\" stroke-width=\"2\" fill=\"rgb(%d, %d, %d)\"/>%n", centerX, centerY, radius, r, g, b);
  }
}