Colorado State University

Recitation R5 - Methods and Data
Summer 2016

CS160: Foundations in Programming


Preview

The goal of this lab is:

Practice Quiz 1

  1. Follow the instructions on the sheet of paper handed to you by your TA
  2. Download QuizInterface.java into the src folder for Q1
  3. Once you complete the quiz, you can start on the Hackerrank problems

Practice Problems


Getting Started: McDonalds Program

Create a R5 project and class, there is no need to create a main method because we will be copying one in.
  1. Save McDonalds.java in the src folder of your R5 Project.
  2. Copy the following code into R5.java
        // Entry point with test code
        public static void main(String[] args) {
            
    	// Instantiating and initializing an object of type McDonalds called yourMeal
            McDonalds yourMeal = new McDonalds();
            yourMeal.chooseDrink(McDonalds.SodaFlavor.Coke, McDonalds.SodaSize.Medium);
            yourMeal.chooseMeal(McDonalds.Sandwich.QuarterPounder, McDonalds.SideOrder.OnionRings);
    
    	// Instantiating and initializing an object of type McDonalds called specialMeal
            McDonalds specialMeal = new McDonalds();
            specialMeal.chooseDrink(McDonalds.SodaFlavor.Coke, McDonalds.SodaSize.Medium);
            specialMeal.chooseMeal(McDonalds.Sandwich.BigMac, McDonalds.SideOrder.FrenchFries);
    
    	// Instantiating and initializing an object of type McDonalds called noDrink
            McDonalds noDrink = new McDonalds();
            noDrink.chooseDrink(McDonalds.SodaFlavor.Nothing, McDonalds.SodaSize.Nothing);
            noDrink.chooseMeal(McDonalds.Sandwich.FiletOfFish, McDonalds.SideOrder.FrenchFries);
    
    	// Printing the results from myMeal, specialMeal, and noDrink objects!
    	// Since you created a toString method, these Objects will use that method!
    	// Try commenting the toString method out in McDonalds and see what happens!
    	System.out.println(yourMeal);
    	System.out.println(specialMeal);
            System.out.println(noDrink);
    
    	// create your own object of type McDonalds called myMeal 
        }
    
  3. Your TA will explain the code that you are given, including the constructor
  4. All places you must fill in are marked by comments with !!!
  5. Define the following instance (non-static) data in McDonalds.java:
        private SodaFlavor flavor;
        private SodaSize size;
        private Sandwich meal;
        private SideOrder side;
        
  6. Define and implement the chooseDrink and chooseMeal methods to set the corresponding instance variables

  7. Implement the calculateCost method to get the price of a meal, based on the following pricing:
  8. Implement the toString method using the String.format method (works exactly like the printf method) to return a String that looks like the output below:
        Drink Flavor: Coke
        Drink Size: Medium
        Sandwich Type: QuarterPounder
        Side Order: OnionRings
        Cost of Order: $6.67
        
  9. Check to make sure your preliminary results are correct by submitting McDonalds.java to Checkin

  10. If correct, create your own personalized order. Add the following Enumerations:
  11. Add prices for these new items to the calculateCost method
  12. Go back to R5 and create a specialized order called myMeal using these items
  13. Submit your final results to Checkin

Once finished, sign up for https://www.hackerrank.com/midterm-practice.
The problems from today's practice quiz are posted there as well.

Submit your McDonalds.java program to R5 on the Checkin tab for automated grading.

© 2016 CS160 Colorado State University. All Rights Reserved.