teaching machines

CS 145 Lab 3

September 16, 2011 by . Filed under cs145, fall 2011, labs.

Our variables aren’t just simple numbers anymore. We’ve got complex objects like String and Scanner at our disposal. Today we’ll give you some more practice at using objects that others have made available.

Reminder

Be sure to get your checkpoints from lab 2 checked off in the first 20 minutes of this lab. They should already be working and ready for approval.

The preassignment is due before Friday. Late work is not graded.

Self-check

Checkpoint #1: please take this short self-check multiple choice quiz to see how your grasp of the Java language is coming along. Your answers will not be graded, but your completion of the quiz counts as this checkpoint. You do not need to let your instructor or TA know you’ve completed it.

Patterns

When learning any language, you’ve got to work with it enough to see patterns before generating new ideas comes very easily. With objects, we make them do something using code like this:

thing.action(outside information needed for action);

In object-oriented programming, our instructions read a bit like English sentences. Our object is the subject, the method is the verb, and the arguments modify the action (adverbs? direct objects?). A sentence like “Barbara eats quickly” might translate into Java like this:

Human barbara = new Human();
barbara.eat(9); // on a scale of 1-10

Some methods give back information that we can print, store in a variable, or use in an expression, but the pattern is more or less the same:

String nickname = barbara.getNickname();

Follow the pattern and you will go far.

String

Create a package named lab3 in your cs145 project.

Complete three of the following five tasks. We suggest you create separate classes and main methods for each. You’ll need to browse through the String class documentation to find methods suitable to your needs. To see the documentation, declare a String variable, place your cursor in the type, and hit Shift-F2.

  1. Prompt the user for a filename. Retrieve the filename. Replace any spaces in the filename with underscores and print it to the screen.
  2. Prompt the user for a password. Retrieve the password. Prompt the user for the password again. Retrieve the password. Print whether or not the same password was entered.
  3. Prompt the user for her name. Retrieve the name. Print at most the first 10 characters of the person’s name. See Math.min as one way to accomplish this.
  4. Prompt the user for a filename. Retrieve the filename. Print whether or not it ends with “.exe”.
  5. Prompt the user for an integral number. Retrieve the number. Print how many digits it contains.

Checkpoint #2. Hit Control-Shift-F in Eclipse to format your code. Show your instructor or TA your three solutions.

Flashcard

The Random class is a source of random numbers. Such numbers have a lot of use, not the least of which is in games. You make a random number generator like you make any object:

Random generator = new Random();

Check out the documentation for Random (Shift-F2 with the cursor inside “Random”) and see how it can help you solve this problem:

Checkpoint #3. Hit Control-Shift-F in Eclipse to format your code. Show your TA or instructor your code.

TODO