Project 2
Your goal for this project is to give me as many opportunities as possible to give you credit for topics covered in the class, particularly those since the last project. You want me to at least be able to give you credit for good use of while loops, for loops, parallel arrays, firstEmpty, breaking tasks into methods, methods with parameters, and methods that return.
Remember:
- only use concepts we have covered in class up to the end of methods.
- indent lines inside structures like if and else, but line up
if/else/endif
- use double quotes around strings: "yes" is a string, yes is a
variable
- you will lose credit for writing incorrect pseudocode that makes your intentions unclear or ambiguous or difficult to read (remember that you
can add comments to your code to help me understand what you are trying to do)
Throughout this assignment you can assume that arrays know their own size. Think about where while vs for loops are appropriate. Think about where parameters vs user prompting is appropriate. Give all variables meaningful names unless they are just numbers used for counting or math. (hint: break to end a loop is usually bad coding practice, and is NOT needed for any part of this assignment)
For this project, write code to do all actions and caluclations that need to happen.
If you need something done, write a method to do it. It is fine to break tasks into extra methods (with parameters/return),
just as it is fine to add extra local variables. However, if I specify the name, parameters, or return for a method, then
that method should just have those characteristics.
Assume that arrays are passed as parameters using the book's version of "by reference" that is: a method is given the actual array (by address) not just a copy, so any changes it made to the array are still there after the method ends, you don't have to return it.
In a .txt document write pseudocode for the following:
Part A
[30] Write a method shrinkArray that takes an array of ints and returns an array of ints.
In the method, count how many elements of the array are nonzero, make a new array of that size,
copy over the non-zero elements, and return that array, so given {0,2,0,4,0,0,8,9}
the method would return {2,4,8,9}.
Extra Credit +10 In addition to the pseudocode version, write a working Java version of part A.
Part B
[70] You will write a program for grocery shopping. Break it into reasonable methods and have a main to start the program.
- Get from the user how many items there are in the shop, and then read in the name, price, and whether it is on sale for each item.
- Print a message saying the shop is open, and show the user a list of all available items,
each with price, but you should print the price as 15% lower if the item is on sale.
- The user has a cart that can hold up to 10 items. Let the user repeatedly
choose items to buy and add to their cart until they choose to quit or run out of cart space.
If they ask for an item that isn't one of our list of items in the store, print "ERROR" and ignore that
item, but don't print an error after they choose to quit.
-
After they quit, show them the items they bought and tell them the total of the prices for all the items they chose (taking sale into account).