Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

What This Assignment Is About: Arrays Classes Searching Use the following Coding Guidelines: Give identifiers semantic meaning and make them easy to read (examples numStudents,

What This Assignment Is About:

Arrays

Classes

Searching

Use the following Coding Guidelines:

Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc).

User upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables, methods, objects).

Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent.

Use white space to make your program more readable.

Part 1: Programming

Design a Candy Machine class that can keep track of the type of candies and their quantities. Use the following UML class diagram to design the class CandyMachine.

CandyMachine

-SELECTION_NUM : final int = 6

-candyNames : String[] -quantities : int[]

+CandyMachine( )

+getCandyName(index : int) : String

+setCandyName(index : int, newCandy : String): void

+getQuantity(index : int) : int

+setQuantity(index : int, numOfCandies : int): void

+restock(index : int, numOfCandies : int): void

+decrementQuantity(index : int) : boolean

+totalQuantity( ) : int

+displayCandies( ) : void

+searchIndexOfCandyName(candyName : String) : int

Program requirements and/or constraints:

You will need to create a file to represent this class CandyMachine.java.

The size of the arrays to be used will be 6, and you can use a constant SELECTION_NUM to express this. In your class, you declare it as:

public class CandyMachine

{

private final int SELECTION_NUM = 6;

}

The constructor of the CandyMachine class needs to initialize the content of two arrays, candyNames and quantities. They should have the following content:

 -------------------------------- Candy Type Quantity -------------------------------- 1. Snickers 5 2. Twix 5 3. Milky Way 5 4. Almond Joy 5 5. Skittles 5 6. M&M's 5 

Thus, the array candyNames contains Snickers, Twix, etc., and the array quantities contains 5 for all of them. Note that the array index starts with 0.

Write the definitions of the methods of CandyMachine: set and get methods for candyNames array and quantities array. The getCandyName method takes an index of the array as its parameter, and returns the string at that index. The setCandyName method takes an index of the array and a candy name (String) to be set, then sets that candy name to the array element of that index. The similar functionalities apply to setQuantity and getQuantity methods.

The decrementQuantity method takes an index as its parameter, and if the number of the candy name in the quantities array at that index is at least one, then decrement it and return true. It should return false otherwise.

The totalQuantity method computes the total number of cans by going through the quantities array, and returns it.

The displayCandies method prints out the content of the arrays in the following format (note that candy names and their quantities can change depending on the array contents):

 -------------------------------- Candy Type Quantity -------------------------------- 1. Snickers 5 2. Twix 5 3. Milky Way 5 4. Almond Joy 5 5. Skittles 5 6. M&M's 5 

The searchIndexOfCandyName method takes a candy name (String) as its parameter and searches at which index where candy name is located in the candyNames array. It should return -1 if it is not found in the array.

Your assignment is to write a complete Java program that will produce the sample run shown.

You also need to create Assignment8.java file that contains a main method. Your main program will do the following: Declare and instantiate a CandyMachine object.

You will first display a menu to access the vending machine. Below are the menus and the starting quantities of candies.

Candy Vending Machine Menu ---------------------------- A. Get a bag of candies - $ .70 B. Change Candy Selection C. Restock Candies D. Exit 

Ask a user to choose one of A, B, C, or D and read in the choice, then perform their corresponding functionality.

The following is an explanation of each menu choice:

A. Get a bag of candies This menu choice displays the candy machine. It lists out the 6 different candies available and how many are available.

-------------------------------- Candy Type Quantity -------------------------------- 1. Snickers 5 2. Twix 5 3. Milky Way 5 4. Almond Joy 5 5. Skittles 5 6. M&M's 5 

Then ask a user to choose a choice of candies between 1 and 6. After a selection is made, then you need to decrease the quantity of that candy name. If there is no candy left for that candy name (e.g., in the above example, Snickers), then display the message such as No Snickers left. Make another selection., and ask a user to choose another selection between 1 and 6. This needs to be repeated until a user enters existing candy name with some quantity left.

B. Change Candy Selection Display the candy selection again, just like the choice A. This allows the user to swap out one candy name for another. The user will be prompted for the new candy name and the name of the old candy to be replaced. If the user does not type in the exact name of the old candy name, an error message such as Kit Kat is not on the list. Make a different selection. will display, because the program will not be able to match the names. This needs to be repeated until a user enters an existing candy name in the machine. Once an existing candy name and the new candy name are entered, then the new candy name should replace the old one. The new candy will begin with five (5) bags.

C. Candies: The user will type in a candy name to be restocked. If the user does not type in one of the existing names, an error message such as Kit Kat is not on the list. Make a different selection. will display, because the program will not be able to match the names. If there is a candy name matched, the user can restock the candies with the additional quantity of 10.

D. Exit: Leaves the menu and quit the program. The program should the total quantities as Total number of candies: 40 before the program quits.

All user input must be error checked. If it is out of range, an appropriate error message must be generated and the user must be prompted again. String input is case sensitive while character input is case insensitive. (See the Sample Run below.)

Sample runs: User input is represented in bold.

Candy Vending Machine Menu ---------------------------- A. Get a bag of candies - $ .70 B. Change Candy Selection C. Restock Candies D. Exit Enter your menu choice (A - D): A -------------------------------- Candy Type Quantity -------------------------------- 1. Snickers 5 2. Twix 5 3. Milky Way 5 4. Almond Joy 5 5. Skittles 5 6. M&M's 5 Enter your choice (1 - 6): 1 You have chosen Snickers. Now Snickers has the quantity of 4 Candy Vending Machine Menu ---------------------------- A. Get a bag of candies - $ .70 B. Change Candy Selection C. Restock Candies D. Exit Enter your menu choice (A - D): A -------------------------------- Candy Type Quantity -------------------------------- 1. Snickers 4 2. Twix 5 3. Milky Way 5 4. Almond Joy 5 5. Skittles 5 6. M&M's 5 Enter your choice (1 - 6): 6 You have chosen M&M's. Now M&M's has the quantity of 4 Candy Vending Machine Menu ---------------------------- A. Get a bag of candies - $ .70 B. Change Candy Selection C. Restock Candies D. Exit Enter your menu choice (A - D): 2 Invalid Choice. Enter your menu choice (A - D): a -------------------------------- Candy Type Quantity -------------------------------- 1. Snickers 4 2. Twix 5 3. Milky Way 5 4. Almond Joy 5 5. Skittles 5 6. M&M's 4 Enter your choice (1 - 6): 5 You have chosen Skittles. Now Skittles has the quantity of 4 Candy Vending Machine Menu ---------------------------- A. Get a bag of candies - $ .70 B. Change Candy Selection C. Restock Candies D. Exit Enter your menu choice (A - D): b -------------------------------- Candy Type Quantity -------------------------------- 1. Snickers 4 2. Twix 5 3. Milky Way 5 4. Almond Joy 5 5. Skittles 4 6. M&M's 4 Enter the candy name to change: M&M's Enter the new candy name: Jelly Beans Jelly Beans replaces M&M's and has the quantity of 5 Candy Vending Machine Menu ---------------------------- A. Get a bag of candies - $ .70 B. Change Candy Selection C. Restock Candies D. Exit Enter your menu choice (A - D): c Enter the candy name: Skittles Skittles is restocked with 10 more Candy Vending Machine Menu ---------------------------- A. Get a bag of candies - $ .70 B. Change Candy Selection C. Restock Candies D. Exit Enter your menu choice (A - D): a -------------------------------- Candy Type Quantity -------------------------------- 1. Snickers 4 2. Twix 5 3. Milky Way 5 4. Almond Joy 5 5. Skittles 14 6. Jelly Beans 5 Enter your choice (1 - 6): 4 You have chosen Almond Joy. Now Almond Joy has the quantity of 4 Candy Vending Machine Menu ---------------------------- A. Get a bag of candies - $ .70 B. Change Candy Selection C. Restock Candies D. Exit Enter your menu choice (A - D): c Enter the candy name: M&M's M&M's is not on the list. Make a different selection. Enter the candy name to restock: Milky Way Milky Way is restocked with 10 more Candy Vending Machine Menu ---------------------------- A. Get a bag of candies - $ .70 B. Change Candy Selection C. Restock Candies D. Exit Enter your menu choice (A - D): J Invalid Choice. Enter your menu choice (A - D): A -------------------------------- Candy Type Quantity -------------------------------- 1. Snickers 4 2. Twix 5 3. Milky Way 15 4. Almond Joy 4 5. Skittles 14 6. Jelly Beans 5 Enter your choice (1 - 6): 4 You have chosen Almond Joy. Now Almond Joy has the quantity of 3 Candy Vending Machine Menu ---------------------------- A. Get a bag of candies - $ .70 B. Change Candy Selection C. Restock Candies D. Exit Enter your menu choice (A - D): b -------------------------------- Candy Type Quantity -------------------------------- 1. Snickers 4 2. Twix 5 3. Milky Way 15 4. Almond Joy 3 5. Skittles 14 6. Jelly Beans 5 Enter the candy name to change: M&M's M&M's is not on the list. Make a different selection. Enter the candy name to change: Snickers Enter the new candy name: Twizzlers Twizzlers replaces Snickers and has the quantity of 5 Candy Vending Machine Menu ---------------------------- A. Get a bag of candies - $ .70 B. Change Candy Selection C. Restock Candies D. Exit Enter your menu choice (A - D): a -------------------------------- Candy Type Quantity -------------------------------- 1. Twizzlers 5 2. Twix 5 3. Milky Way 15 4. Almond Joy 3 5. Skittles 14 6. Jelly Beans 5 Enter your choice (1 - 6): 1 You have chosen Twizzlers. Now Twizzlers has the quantity of 4 Candy Vending Machine Menu ---------------------------- A. Get a bag of candies - $ .70 B. Change Candy Selection C. Restock Candies D. Exit Enter your menu choice (A - D): d Total number of candies: 46

*********************************************************************************

Helpful hints for doing this assignment:

work on it in steps. Write one method, test it with a test driver and make sure it works before going on to the next method

always make sure your code compiles before you add another method

your methods should be able to be called in any order

*********************************************************************************

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sql All In One For Dummies 7 Books In One

Authors: Allen G Taylor ,Richard Blum

4th Edition

1394242298, 978-1394242290

More Books

Students also viewed these Databases questions

Question

Be relaxed at the hips

Answered: 1 week ago

Question

Write Hund's rule?

Answered: 1 week ago

Question

5. List the forces that shape a groups decisions

Answered: 1 week ago

Question

4. Identify how culture affects appropriate leadership behavior

Answered: 1 week ago