February 8, 2018
Deadlines: I strongly recommend that you complete this lab before the first exam.
Your friend learned that you now know how to program, and would like to know if you could develop the following program:
The idea would be that your program would ask me how much the check is, how many we are, and what percentage we want to leave as a tip. Your program would then print how much we need to pay (assuming we split equitably).
What would be great is if you could round things up (I often pay cash, so I don’t want to have to carry small change with me): so for instance, if we are two and the check is $23, plus 15% tip, $23 + ($23 × 0.15) = $26.45, we need to pay $13.225 each, but I’d rather have us both pay $14 and leave a small extra tip. Oh, and can you make it look nice, and print all the information? How much we left as a tip, what percentage it makes, etc.
Think about your friend’s demands, and how you could write this program (for now, you can think of it as being entirely in a Main
method, you don’t have to construct a class). Write on a piece of paper the steps needed. The general structure should be:
Try to decompose those three steps further: what messages you need to print to gather the data, how many numbers, or variables, you need to manipulates, what is the exact computation, what you need to print at the last step, etc. Make sure you understand how to do the computation, without thinking about the tools you actually have in C#.
Now is time to think about the actual code you need to write. You should have all the tools you need to complete the steps you identified at the previous exercise. The “rounding part” could be problematic, but you have two different tools to solve this question: you can either truncate (using explicit conversion) and then add one, or use the Math
class, that was introduced in last lab’s “Pushing Further”.
Your tip calculator works great and your friend is happy. You now realize that you could make one step further and actually write a class to perform those tasks. Think about the specifications of this class (how many attributes, what methods) and include a ToString
method in it.