Lab 04

PLEASE NOTE: All lab exercises are due by Monday June 2nd at 10:00am.
  1. Download files
  2. What you should know
  3. Simple If-Else
  4. Evaluating boolean expressions
  5. Characterizing characters
  6. Tracing code involving complex selection
  7. Request lab solutions

Download files

Download the L04.zip file and extract the files to your cs125 folder in your home directory. You will use these files to complete this week's lab exercises.

What you should know

This lab covers lectures 6 and 7. You are expected to know about Boolean expressions and selection. A quick overview is given here.

Simple if-else statement

Create the Java file IfElse.java, write a program that will read in 2 integers (using the Scanner class) and determine if the two numbers have the same parity (i.e., are they both odd or both even). If they have the same parity, the program should output "Same parity" and otherwise output "One number is odd, the other is even". (Hint: remember %.)


Evaluating boolean expressions

Evaluate the following Boolean expressions. Assume that A, B, C and D are Boolean variables where A is FALSE, B is TRUE , C is TRUE and D is FALSE.

  1. (A && B) || (C || D)
  2. !(C || A) && D
  3. (D || !A) && C
  4. (A && B) || (A && C) || (A && D)
  5. B && (A || C || D)

Paste your answers into the box below. The answers should only be TRUE or FALSE and should be one response to a line.


Characterizing characters

Open CharRecognition.java and write a program that will identify the first character input by the user (you'll need to use a Scanner object, the method nextLine(), and a String method to do that). Your program must identify the character as one of the following:

Note that you can compare characters in the same way as you compare integers using <, <=, >, >=, and ==. The letters 'a' - 'z' are consecutive, the letters 'A' - 'Z' are consecutive, and the digits '0' - '9' are consecutive when comparing character values ('1'-'0' = 1, '2'-'0' = 2, ... '9'-'0' = 9). For example,

char firstLetter = 'a';
char secondLetter = 'b';
char thirdLetter = 'z';
System.out.println(firstLetter < secondLetter); // outputs true
System.out.println(thirdLetter > firstLetter); // outputs true
Sample input Sample output
p letter
W letter
7 digit
* other
whitespace
; other

Once completed paste ALL your code in the below area and submit.


Tracing code involving complex selection

Review the code in the file AcademicStanding.java. Try tracing through the code by hand for the following inputs to predict the output. You can check your answers by running the program and inputting the values given below. Note that, on a written exam, you will not have access to a computer to run code. It is essential that you understand how to trace code, so we strongly suggest that you try tracing through this code before running the code to get the solutions.

CAV MAV numExcluded numCredits Standing?
87 76 0.0 8.0
80 79 1.0 1.5
80 58 0.5 2.0
86 51 1.0 1.5
61 72 2.5 4.5
51 51 2.0 3.0

Submit the standings you get below with each standing being seperated by a comma.


Income Taxes

The Canada Revenue Agency (CRA) collects income taxes. This question is based on Schedule 1 of the T1 tax form. If you would like to see the document it is available here. To eliminate some possible confusion, you do not NEED to fully understand the TD1 document to complete this question, all the information you need to be successful is given below.

The income tax you pay depends upon your earned income and on different rates as follows.

  1. If your income is below $20,000 your income tax is 19% of your earnings.
  2. If your income is between $20,000.00 and $50,000 (Inclusive), your income tax is all tax owing below $20,000, plus 27% of the income above $20,000.
  3. If your income is $50,000.01 or higher, your income tax is all tax owing below $50,000, plus 42% of the income above $50,000.

As an example, if your income were $70,000 you would owe 19% of the first $20,000 ($3,800), 27% of the next $30,000 ($8,100) and 42% of the remaining $20,000 ($8,400) giving total taxes of $20,300.

Write a program that will ask the user for his or her income and then report the income and taxes. Below are examples of Earned income values, and the correct tax values. You may use these as a guide to check the accuracy of your program. You may assume the user inputs only the numeric value of their income, and not the "$".


Earned income: $10000.0
Taxes: $1900.0

Earned income: $20000.0
Taxes: $3800.0

Earned income: $30000.0
Taxes: $6500.0

Earned income: $50000.01
Taxes: $11900.004200000001

Earned income: $70000.0
Taxes: $20300.0


Place your solution in a file called "TaxesEarnedIncome.java" and (after testing) submit it below.


Request lab solutions

If you request solutions for this lab you will be sent an email that contains your answers as well as the expected answers. The email will be sent out to all interested students once the deadline for completing the exercises has passed for all students.