CSU Banner

CS 150, Fall 2017

Lab 3 - Conditionals, More Scanner Practice

Objectives of this Lab

  1. Develop a better understanding of how conditionals work in Java,
  2. by utilizing an if decision block to produce specific output based on user input.

  3. Note: This lab is titled Lab 3 so it is appropriately labeled for future semesters. Please do not worry that you do not have an R2 project and proceed as instructed.

Getting Started

Log in to your zyBooks account. Part of your attendance points today come from showing the TAs that you have correctly entered your EID into zyBooks. This will be checked once you have completed R3 and are ready to take the Lab 3 Canvas attendance quiz. Next, create a new Java Project named R3, and make a class named R3 with a main method. The TA will then go over how to make a default header for your future programs (instructions in Lab 1 specifications) if needed.

This program simulates a decision structure that determines what credit card level a customer is at after three quarters of spending. The levels available to customers are platinum, gold, and silver. Your program will tell a customer what level they are rated at, based on the average amount spent per quarter.

Conditional Based Program

Your TA will explain comparison operators, logical operators, and if statements. After that you will code. A sample main with organizational comments is provided below for you to use if you would like. See the sample output below for an example of what should be printed. Following the instructions below to practice designing a program with conditionals.
    Part One
  1. Declare a variable named q1Purchases of type double, with an initial value of 0.0.
  2. Declare a variable named q2Purchases of type double, with an initial value of 0.0.
  3. Declare a variable named q3Purchases of type double, with an initial value of 0.0.
  4. Declare a variable named qualifyingAmount of type double, with an initial value of 500.0.
  5. Declare a variable named quarterAverage of type double, with an initial value of 0.0.
  6. Declare a Scanner object and initialize it to read from the keyboard.
  7. Don't forget to import the Scanner library into your program in order to be able to use your scanner. To do this, you'll need to include the statement
    import java.util.Scanner; at the top of your program before your R3 class block begins.

  8. Part Two
  9. Next prompt the user as follows: "Quarter one purchases: " and assign q1Purchases a value from the keyboard by calling the nextDouble() method on your Scanner object.
  10. Using an if conditional, check to see if q1Purchases is below qualifyingAmount.
  11. If this is case (i.e. true), print "Amount spent this quarter is below qualifying amount." to the console.
  12. Repeat steps 8 - 10 for q2Purchases and q3Purchases variables, adjusting the user prompt as necessary.
  13. Calculate the purchase average by writing a statement that assigns quarterAverage the value that results from adding each quarter together and then dividing by 3. Remember to use parentheses to enforce math precendence.
  14. Using printf, print the average to 2 decimal places after the string literal, "Three quarter spending average: ".

  15. Part Three
  16. Determine a customer's spending level with an if/else-if/else-if/else block located within an if conditional.
  17. First, write an if conditional to verify that every quarter's purchase amount was above the qualifying amount. We could write three nested if conditionals to do this but it would be hard to follow. How could we check all three quarters in one if conditional using a logical operator instead?
  18. If all purchases are greater than the qualifying amount, then write the next four steps inside the current if. Otherwise, do not print anything else.
  19. If the average is greater than or equal to 12000.0, print the following to the console: "Congratulations. You've earned platinum status!".
  20. If the average is greater than or equal to 8000.0 and less than 12000.0, print the following: "Congratulations. You've earned gold status!".
  21. If the average is greater than or equal to 4000.0 and less than 8000.0, print the following: "Congratulations. You've earned silver status!".
  22. Otherwise, print "Great job managing your money wisely!".

Sample Main (if needed, to help you create a well-organized program)

public static void main(String[] args) {
	// Declare program variables. 
	// Declare Scanner object.
	// User prompts, value assignments to quarter purchase variables and single if statements.
	// Calculate and print average.
	// If block to determine customer spending level.
} // end main

Two Sample Output Runs (user input shown in blue)

    Quarter one purchases: 464.55
    Amount spent this quarter is below qualifying amount.
    Quarter two purchases: 9522.6
    Quarter three purchases: 5805.31
    Three quarter spending average: 5264.15 
Quarter one purchases: 1500.2 Quarter two purchases: 8722.85 Quarter three purchases: 3301.19 Three quarter spending average: 4508.08 Congratulations. You've earned silver status!

CS Banner
CS Building