Last Updated: 2017-09-24 Sun 11:14

CSCI 1103 Lab03: Conditional Execution

CODE DISTRIBUTION: lab03-code.zip

  • Download the code distribution every lab
  • See further setup instructions below

CHANGELOG:

1 Rationale

Completing this lab will give some basic practice with conditionals: if/else statements in Java. This facility allows programs to react dynamically to user input or to varying conditions in the program. Mastery of conditionals is fundamental to any programming endeavor.

Associated Reading: Eck Chapter 3

Chapter 3 section 5 discusses the basics of conditionals including single if statements, if/else, and chains of if/else if.

2 Download Lab Code and Setup

As in Lab01, download the code pack linked at the top of the page. Unzip this which will create a folder and create your files in that folder.

File State Notes
PARTNERS.txt Edit Fill in your name and the name of your partner in this text file
TextIO.java Provided Allows easy input calls like TextIO.getInt()
TimeAwarenessTests.java Testing Tests for problem 1
SequenceTypeTests.java Testing Tests for problem 2
KTests.java Testing Utility routines for all tests
junit-1103.jar Testing For testing, don't try to edit this one
TimeAwareness.java Edit Create this file for Problem 1
SequenceType.java Create Create this file for Problem 2

3 Problem 1: Time Awareness (TESTED)

The program TimeAwareness.java is provided in the code download for this lab but it is incomplete. The intention of the program is to have students enter basic information how they spend their time in a day and warn them if any of the time expenditures seem too low or high.

The comments present in the provided code indicate where modifications need to be made:

  • Some input is needed from the user in a couple places for the hours they spend on several activities.
  • Some small calculations are needed to compute roughly how many hours per day should be spend on a 4 credit class
  • There are number of places where if() statements should be inserted to check whether input numbers are in a dangerous range
  • There is at least one spot where an else is required

Edit TimeAwareness.java so that it behaves like the demonstrations below.

> javac TimeAwareness.java

> java TimeAwareness
STUDENT TIME AWARENESS SURVEY
-----------------------------
On average, how many hours per day including lecture/lab
do you spend on CSCI 1103? (ex 1.5 hours):
2.0
Sounds like you're applying proper effort

On average, how many hours per day do you spend using
social media such as Facebook/Twitter/Texting (ex 1.5 hours):
0.5

> java TimeAwareness
STUDENT TIME AWARENESS SURVEY
-----------------------------
On average, how many hours per day including lecture/lab
do you spend on CSCI 1103? (ex 1.5 hours):
1.1
Expected hours per day for a 4-credit class: 1.7
See https://policy.umn.edu/education/studentwork
Consider increasing your time spent on CSCI 1103

On average, how many hours per day do you spend using
social media such as Facebook/Twitter/Texting (ex 1.5 hours):
0.5

> java TimeAwareness
STUDENT TIME AWARENESS SURVEY
-----------------------------
On average, how many hours per day including lecture/lab
do you spend on CSCI 1103? (ex 1.5 hours):
1.3
Expected hours per day for a 4-credit class: 1.7
See https://policy.umn.edu/education/studentwork
Consider increasing your time spent on CSCI 1103

On average, how many hours per day do you spend using
social media such as Facebook/Twitter/Texting (ex 1.5 hours):
2.3
Warning: 2 or more hours per day of social media use
has strong correlation with social isolation, morbidity
and mortality (!). Consider reducing you use

Suggestion: Consider shifting some time from Social Media 
use to learning Java: it will pay more in the long run.

> java TimeAwareness
STUDENT TIME AWARENESS SURVEY
-----------------------------
On average, how many hours per day including lecture/lab
do you spend on CSCI 1103? (ex 1.5 hours):
8.4
You may be overdoing it. Seek help from the 1103 staff
or ask someone out on a date (not the course staff).

On average, how many hours per day do you spend using
social media such as Facebook/Twitter/Texting (ex 1.5 hours):
2.5
Warning: 2 or more hours per day of social media use
has strong correlation with social isolation, morbidity
and mortality (!). Consider reducing you use

> java TimeAwareness
STUDENT TIME AWARENESS SURVEY
-----------------------------
On average, how many hours per day including lecture/lab
do you spend on CSCI 1103? (ex 1.5 hours):
0
Expected hours per day for a 4-credit class: 1.7
See https://policy.umn.edu/education/studentwork
Consider increasing your time spent on CSCI 1103

On average, how many hours per day do you spend using
social media such as Facebook/Twitter/Texting (ex 1.5 hours):
0

What are you doing with your time?

4 Problem 2: Number Sequence Types (TESTED)

Create the file SequenceType.java which should do the following.

  • Prompt the user for 4 input numbers
  • Use a series of if/else if conditions to analyze the 4 numbers to determine their "sequence type"
  • Sequence type is one of the following
    • All Equal: all numbers are equal
    • Strictly Increasing: each number is bigger than the previous with no ties
    • Non-Decreasing: each number is bigger than or equal to the previous one
    • Strictly Decreasing: each number is smaller than the previous with no ties
    • Non-Increasing: each number is smaller than or equal to the previous one
    • No particular order: none of the other types apply
  • Each sequence can have only 1 type so you will need to choose the ordering of your if/else if checks carefully.
  • Print out the sequence along with the type. You may wish to use a printf() statement to easily print out all 4 numbers with one go.
  • Note also the formatting: the printed sequence is on the same line a its type

Below are some demonstrations.

> javac SequenceType.java

> java SequenceType
Enter 4 integers (ex: 1 3 2 5):
4 4 4 4
Sequence 4 4 4 4 is: All Equal

> java SequenceType
Enter 4 integers (ex: 1 3 2 5):
1 2 4 8
Sequence 1 2 4 8 is: Strictly Increasing

> java SequenceType
Enter 4 integers (ex: 1 3 2 5):
1 4 5 5
Sequence 1 4 5 5 is: Non-Decreasing

> java SequenceType
Enter 4 integers (ex: 1 3 2 5):
3 -1 -1 -4
Sequence 3 -1 -1 -4 is: Non-Increasing

> java SequenceType
Enter 4 integers (ex: 1 3 2 5):
10 7 2 -1
Sequence 10 7 2 -1 is: Strictly Decreasing

> java SequenceType
Enter 4 integers (ex: 1 3 2 5):
2 4 1 9
Sequence 2 4 1 9 is: No particular order

5 Getting Credit for this Lab

5.1 Demonstrate your code to Lab Staff (40%)

You should feel free at any time to get help from lab staff as you get stuck on the exercises.

By the end of the lab, make sure to demonstrate your code to a staff member. This will ensure that you receive full credit on the lab. Staff might ask you to

  • Change what is printed
  • Compile and run on the command line
  • Run tests in DrJava or on the command line
  • Show how to zip the folder with your programs in it

Be ready to do any or all of these.

5.2 Zip and Submit (60%)

Ensure that file PARTNERS.txt has your name and the name of your partner in it. This fill is located with the other Java files for the lab and can be edited with DrJava.

Once your are confident your code is working, you are ready to submit. Ensure your folder has all of the required files. Create a zip archive of your lab folder and submit it to blackboard.

On Canvas:

  • Click on the Assignments section
  • Click on the appropriate link for this lab
  • Scroll down to "Attach a File"
  • Click "Browse My Computer"
  • Select you Zip file and press OK

Author: Chris Kauffman (kauffman@cs.gmu.edu)
Date: 2017-09-24 Sun 11:14