CSE 201 - Lab 3 - Flow of Control


Note: This lab assumes that you have completed lab 1, and that you know how to login to your account, and how to open, edit, save your files, as well as compile and run your programs. If you have forgotten any of these techniques, refer back to the lab 1 handout. Be sure to follow the directions very carefully. If you have any problems or questions, be sure to ask your instructor as soon as possible. Points will be deducted if the submitted programs do not have the appropriate comments included.


Table of Contents


Objective

The objective for this lab is to write a couple of Java programs to practice the use of Java control structures.


Overview

This lab assignment has two parts. For the first part, you will be writing a Java program from scratch. The program will allow the user to enter a string and then will report to the user whether the string was empty, or a word, or a number, or something else. For the second part, you will write another Java program that asks a user 3 math questions and determines how many correct answers the user has entered.


Materials Provided

For this lab no other files are necessary.


Part 1 - Set up

Start Eclipse, create a new project, Lab3, and create a new Lab3Part1 class (the corresponding file name will be Lab3Part1.java) in the project. Complete the Lab3Part1 program according to the description below. If you don't remember how to create a project or a class in Eclipse, see Lab 1 for instructions.


Part 1 - Description

The program asks the user to enter a string and inputs one. Then it looks at the string and prints a message stating whether it is the empty string, a word, a number, or something else. For this lab, a string is considered to be a word if it starts and ends with a letter; it is considered to be a number if it starts and ends with a digit; and something else if it is not empty and it is not a word and it is not a number.

For example, here are several sample runs of the program you will write (user inputs are in bold):

Checking if a character is a letter or a digit

To check whether a character is a letter or a digit, you can use the following methods:


Part 2 - Set up

Create a new Lab3Part2 class (the corresponding file name will be Lab3Part2.java) in the Lab3 project. Complete the Lab3Part2 program according to the description below.


Part 2 - Description

The program asks the user to enter two integers. Then it asks three questions: what's the product of the two integers, what's the quotient of the two integers, and what's the remainder of the division of the two integers. It inputs the answers to each of the question from the user, tells the user whether the answer was correct or worng, and finally prints a message that depends on how many correct answers the user provided.

For example, here is a sample run of the program you will write (user inputs are in bold):

    ----------------------
        MATH QUESTIONS
    ----------------------
    Please enter an integer.
    5
    Please enter another integer.
    3

    Answer the following questions:

    1. 5 * 3 = ?
    17
    Wrong!

    2. 5 / 3 = ?
    1
    Correct.

    3. 5 % 3 = ?
    2
    Correct.

    You did OK.
    You got 2 correct.
    That's 66.66667%

Make sure that your program produces output exactly like the sample provided above and that it includes th epercentage of correct answers.

Here are the different messages that should be printed depending on the number of correct answers:

Correct Answers Message
0 You did very bad.
1 You did poorly.
2 You did OK.
3 Good job!


Lab Submission

Make sure your programs compile and run correctly before submitting. To submit, use the Submit tool available on the Widows desktop to submit the Lab3Part1.java and Lab3Part2.java files from the Lab3 project (the location of the project in the file system will be z:\eclipse\workspace\Lab3). Make sure that you select lab3 from the list of Assignments. If you don't remember how to use the Submit tool, see Lab 1 for instructions.