- Methods and Data
CS160: Foundations in Programming
The goal of this lab is:
- Setup the workspace for the P6 assignment
- Write a class with data and methods
- Show how instance data is useful
- Instantiate multiple objects from a class
Phase 1: Getting Started on P6
The setup for P6 is complex enough that you won't want
to do it on your own, so the TA will help you to get started. P6 requires an additional Java
file for the graphical user interface, in addition to several image and maze files. As usual, use Eclipse
to create a P6 project and associated P6 class in P6.java. Copy the image files and maze files
into ../P6, and the Maze.java source file to ../P6/src/.
Copy the following code to the main method in P6.java:
// Create maze
String fileName = args;
Maze maze = new Maze(fileName);
System.err.println("Maze name: " + fileName);
// Get dimensions
int mazeWidth = maze.getSize();
int mazeHeight = maze.getSize();
System.err.println("Maze width: " + mazeWidth);
System.err.println("Maze height: " + mazeHeight);
Once all files are in place, follow these steps:
Note: The initial version of P6.java you wrote in this lab is just enough to get
you started, you will need to do more work to complete the assignment.
- Modify the run configuration to pass Maze1.txt to the program.
- Test your program by calling maze.moveRight() to make the student move right one square.
- The maze object has similar methods to move down, left, and up, try calling each of them.
Phase 2: Methods and Data
Create a McDonalds project and class, and copy the following code into it.
- Create a new project in Eclipse called McDonalds.
- Define a new class called McDonalds.java.
- Copy the code in McDonalds.java.
- Your TA will explain the code that you are given, including the constructor.
- All places you must fill in are marked by comments with !!!.
- Define the following as private class instance (non-static) data in McDonalds.java:
private SodaFlavor sodaFlavor;
private SodaSize sodaSize;
private Sandwich sandwich;
private SideOrder side;
- Define and implement the chooseDrink and chooseMeal methods to set the
corresponing instance variables.
- Implement the calculateCost method to get the price of a meal, based on
the following pricing:
- If exactly a medium drink, Big Mac, and french fries, the cost is $4.39.
- Otherwise charge for the drink, sandwich, and side individually.
- For small drinks, $0.79, for medium drinks, $0.99, for large drinks, $1.19.
- For a Big Mac, $3.79, for a Quarter Pounder, $4.09, for a Filet of Fish, $2.89.
- For any side dish, charge $1.59.
- Do not charge for items not selected, i.e. the enumeration equals Nothing.
- Implement the toString method to print the meal as in the example shown below:
Drink Flavor: Coke
Drink Size: Medium
Sandwich Type: QuarterPounder
Side Order: OnionRings
Cost of Order: $6.67
- Note: You can use the enumeration to print the drink flavor, sandwich type, etc.
- Note: You can tell I haven't been to McDonalds in a long time!
Submit your McDonalds.java program to R10 on the Checkin tab for automated grading.
© 2015 CS160 Colorado State University. All Rights Reserved.