CSU Banner

CS 150 - Spring 2018

Lab 5 - If Conditionals and More Scanner Practice

Wednesday, February 14, 2018


Lab 4 Regrade: If you need a regrade for last week's lab, please open Lab 4 requirements and show one of the TAs your completed work within the first 5 minutes of class. The TAs will not accept regrades after this time. Additionally, if you forgot to take the quiz last week for the single attendance point, your maximum possible grade for the previous lab is now 3 out of 4.

Objectives of this Lab

  1. Review equality, relational, and logical operators,
  2. develop a better understanding of how conditionals work in Java, and
  3. utilize an if decision block to produce specific output based on user input.

Starting the Assignment

Instructions

All of the Java code you write should be contained in the main method. For today's lab, your TA will start by discussing 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.

    Part One
  1. Initialize 4 variables of type double to 0.0 named q1Purchases, q2Purchases, q3Purchases, and qAverage.
  2. Instantiate a Scanner object to read from the keyboard.
  3. Don't forget to import the Scanner library into your program in order to be able to use your scanner.
  4. 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.
  5. Repeat step 4 two more times for q2Purchases and q3Purchases and alter the user prompt appropriately.
  6. After this, print a blank line to the console for better readability.
  7. You can now run your program to verify your prompts are correct by clicking the green "Play" shaped button on the toolbar.
  8. Please run your programs using the green play circle ("Run"), not the green circle within a square ("Launch").
  9. Note that the output will appear in the Console window at the bottom or side of your Eclipse window.
  10. You should see the program print your first prompt to the console. At this point your program is waiting for you to enter a value in to the console. Once you do so and press enter, the program will continue its execution.

  11. Part Two
  12. First, write an if conditional to check if less than $500.00 was spent during any of the three quarters. Write your conditional utilizing both relational and logical operators. In English, the if statement might read as: if q1Purchases is less than 500.0 or q2Purchases is less than 500.0 or q3Purchases is less than 500.0.
  13. Inside the if conditional, print "You spent less than $500.00 during one quarter." to the console.
  14. Following this, calculate the purchase average by writing a statement that assigns qAverage the value that results from adding each quarter together and then dividing by 3. Remember to use parentheses to enforce math precendence.
  15. Then, using printf, print the average to 2 decimal places after the prompt, "Three quarter spending average: ".

  16. Part Three
  17. Determine a customer's spending level with an if/else-if/else-if/else block.
  18. First, write an if conditional that will execute if the average is greater than or equal to 12000.0. If so, print the following to the console: "Congratulations. You've earned platinum status!".
  19. Next, write an else-if that will execute if the average is greater than or equal to 8000.0 and less than 12000.0. If so, print the following: "Congratulations. You've earned gold status!".
  20. Write another else-if that will execute if the average is greater than or equal to 4000.0 and less than 8000.0. If so, print the following: "Congratulations. You've earned silver status!".
  21. To finish the if block, write an else that prints the following: "Great job managing your money wisely!"

Questions to consider:
- We could have omitted the second condition in both of the else-ifs and the program would still have behaved correctly. Why is this so?
- We could have written the if/else-if/else block as four if statements. However, for the program to execute correctly, we would need to keep the conditionals as written above. Why is this? (Hint: How is the behavior of multiple if statements slightly different than if/else-if/else?

Program Structure

import java.util.Scanner;

//Assignment: Lab5
//Author: Jess Cobb
//Date: 14 February 2018
//Class: CS150
//Email: jesscobb@rams.colostate.edu

public class Lab5 {

    public static void main(String[] args) {
	// Declare program variables. 
		
	// Declare Scanner object.
	    
	// User prompts, value assignments to quarter purchase variables.
		
	// Check if less than $500.00 was spent during one quarter.
		
	// Calculate and print average.
		
	// If block to determine customer level.
		
	// Close Scanner.
		
    } // end main
} // end class Lab5

Sample Output (Two separate runs shown)

User input is shown in blue.

Quarter one purchases: 15.99 
Quarter two purchases: 5642 
Quarter three purchases: 123.88 

You spent less than $500.00 during one quarter.
Three quarter spending average: 1927.29
Great job managing your money wisely!
------------------------------------------------
Quarter one purchases: 9599 
Quarter two purchases: 6523.44 
Quarter three purchases: 5702.33 

Three quarter spending average: 7274.92
Congratulations. You've earned silver status!



© 2018 CS150 Colorado State University. All Rights Reserved.
CS Banner
CS Building