CS230

 

Solutions

 

Goals:

In this lab you will get practice with:

  1. Scanner, String, and Random java class,
  2. boolean expressions,
  3. conditionals,
  4. loops (while/do-while),
  5. method definitions and invocations
 

Task 0 PRE-LAB: Use a Scanner

In this task we will write α better version of the Intro class . Start with this version of the Introduction.java program. Make the necessary changes so that the program asks the user for their name, year of graduation, their money and whether they are cs majos or not. It reads the user's answers as they type them in. Finally, the program produces a paragraph, summarizing this information, and presents it to the user.

Use Java's built-in Scanner class. The Scanner class, which is part of the standard Java class library (java API), provides methods for reading input values.

 

Task 1: Download code needed for this lab

Use your ftp client ( Fetch or Cyberduck ) on your mac. From the cs230/download directory, download lab1 folder to your desktop. Rename lab1 so its name contains your and your partner's usernames, for example: lab1_skakavou_jherbst. In BlueJ create a new project, named Lab1Project, where you will add the classes you will create today for this lab.

 

Task 2: The Height Class

In this task, you will define a class named Height. Your program will:

  • ask the user for their height in inches, wich they will type in the keyboard
  • read that input, and calculate the user's height in feet and inches
  • show the results on the screen

As you compile your program, it is likely that some error messages (syntax errors) will be generated. Read each one carefully, as you try to debug your program.

 

Task 3: To Vote or not to Vote

Write a program, named Voting, to:

  1. Ask the user for their first, last name, their year of birth, and if needed their birth month.
  2. Determine their voting eligibility, and produce a relevant message.

Assume that users who have turned 18 in November of this year are considered eligible for voting.

 

Task 4: Roll the Die

Write a class, named Dice, which simulates rolling a die twice. Assume that the possible values when rolling a die are in the range [1..6]. The program should produce an output like this:

    You rolled a 3 and a 5.
 

Task 5A: Produce One User Name

Write a program, named UserName, to:

  1. ask the user for their first and last name.
  2. produce their user name, based on the first and last name, and a random integer. A user name consists of
    • the user's first name initial,
    • followed by the first 5 letters of their last name,
    • followed by a random two-digit integer in the range [10-99]

Assume that the user's last name has length >= 5. This version does not need to deal with duplicate user names.

 

Task 5B: Produce many User Names

Write an other version of the previous program, named UserNames, to continue prompting the user for their name information, and then producing user names until the user chooses to stop the process. Use a loop and a sentinel value to achieve this. Try to write two versions, one using a while and one a do-while loop.

You can start with any design you can come up with. But at the end, try to have the following methods defined and properly invoked:

1. public static String produceFirstPart()
2. public static int produceSecondPart()
3. public static String produceOneUserName() //this method calls the previous two

The main() method, as always, should be high-level and short: It should only contain the top-level loop, which drives the whole process, and make calls to the methods defined earlier.

 

Task 6: Turn in your lab (electronically)

Consult the instructions in last week's lab, as you need. At the end, check to verify that your work has been uploaded properly in both partners accounts.