teaching machines

CS 148: Lab 3 – String and Math

Welcome to lab 3!

If you have checkpoints from the last lab to show your instructor or TA, do so immediately. No credit will be given if you have not already completed the work, nor will credit be given after the first 10 minutes of this lab.

Work with a partner that you have not worked with before.

Our goal today is acquaint ourselves with methods of the Math and String classes.

Checkpoint 1

Person A types. Make a lab03 package in your Eclipse project.

Complete three of the following problems:

  • In class Circler, prompt the user for four numbers: an x-coordinate for point A, a y-coordinate for point A, an x-coordinate for point B, and a y-coordinate for point B. Print the parametric equation of the circle whose perimeter passes through these two points. The equation should follow this pattern:
    (x - originX)^2 + (y - originY)^2 = radius^2
    For example, if point A is (0, 0) and point B is (1, 0), print the following equation:
    (x - 0.5)^2 + (y - 0)^2 = 0.5^2
  • In class Clamper, write a main method that asks the user for a single number. Clamp the number to the range [lo, hi] and print the result. That is, if the number is less than lo, print lo. If it is greater than hi, print hi. Otherwise, the number is already within the range and can be printed as is. Use Math.min and Math.max to solve this. Do not use if statements, even if you’re familiar with them. Do not prompt the user for lo and hi. You, the developer, should define them directly in the code according to your whimsy.
  • In class Range, prompt the user to enter a low number and a high number, both integers. Generate a random integer in that range, inclusively. Use only Math.random, not the Random class. Test your code with a small range, like 20 through 21, and make sure you see all numbers in this range.
  • You enroll at wizarding school and the sorting hat wants to assign you a username. It asks you for your name (first and last), retrieves it with only a single Scanner method call, and then magically prints out your username, which is the first letter of your first name followed by your complete last name, all lowercase. For example, if the user enters “Robin Steele”, the username is “rsteele”. In class Usernamer, write a main method that accomplishes this task.
  • In class Ticker, prompt the user to enter two team names and two integer scores. Print them using a single call to printf (or print and String.format) such that the team names are left-aligned to fill 15 columns and the scores are right-aligned to fill 4 columns, with a | character separating the columns. For example:
    Heffalumps     |  17
    Woozles        |   9
    See this reference on the format string protocol.

Checkpoint 2

Person B types.

Write an anagram puzzler. Generate a random word via Random Word API, jumble the letters, prompt the jumbled form to the user, grab their unscrambled form from the keyboard, and print whether or not they descrambled it correctly.

Reading from the Web

To read data from the web in Java, use URLConnection:

URL url = new URL("insert your URL here");
URLConnection connection = url.openConnection();
InputStream inputStream = connection.getInputStream();

You can then feed inputStream into a Scanner—instead of using System.in.

The code will potentially generate some exceptions. For the time being, don’t try to handle them. Just let them be thrown by adding a throws clause on the main method:

public static void main(String[] args) throws MalformedURLException, IOException {
  ...
}

Constraining Word Length

The Random Word API documentation describes how you can tailor the length of the randomly-generated word. Append to your URL this query: ?len=N, replacing N with your desired word length.

Scrambling

To scramble a collection of data, I recommend you use Collections.shuffle. But to use this, you must turn the String into something that Java considers a Collection. ArrayList is a good choice. So, I’d follow this pseudocode:

convert String to ArrayList
shuffle list
convert ArrayList to String

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *