teaching machines

CS 145 Lab 3 – Using objects

Prelab

  • Complete Self-check 3.14 on the Practice-It website before 8 AM on February 6.

Introduction

Our variables aren’t just simple numbers anymore. We’ve got complex objects like String and Scanner at our disposal. Today we’ll get 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.

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 a filename. Retrieve the filename. Print whether or not it ends with “.exe”.
  4. 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.
  5. Prompt the user for an integral number. Retrieve the number. Print how many digits it contains.

Checkpoint #1. 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” or search the Internet for “java 6 random”) and see how it can help you solve this problem:

  • Write a flashcard arithmetic application for a young relative. Generate a pair of random integers between 0 and 10, ask the player what the two numbers are multiplied together, and print whether or not the answer is correct.

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

TODO

  • Read section 2.3 in your book.

Comments

Leave a Reply

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