I want the codes for part 1 and part 2. I do not want the report.
COMP122 Assessment 4 Worth 25% of the final course mark. Submission Deadline Wednesday, 16 May 2018, 5:00pm (Wednesday of Week 13) Learning Outcomes. This assessment addresses the following learning outcomes of COMP122: • Describe object hierarchy structure and how to design such a hierarchy of related classes. • Describe the concept of object polymorphism in theory and demonstrate this concept in practice. • Demonstrate concepts of event-driven programming and be able to design simple GUI to demonstrate this understanding. • Identify and describe the task and issues involved in the process of developing interactive products for people, and the techniques used to perform these tasks. Part I (50% of assessment mark) Introduction The goal here is to develop a relatively simple GUI to demonstrate that you understand the general techniques used. The Vending Machine. Obviously there’s no actual vending machine that will be used here, but you will develop a frame that shows nine products, each having a stock level and a price. The idea is that when pressing a button, the corresponding item will have its stock decremented by one item, and the total sales that is being tracked will increase by the amount of the item that has been “sold”. The GUI will also include another button that will open up a “Vendor’s Window”. This window will show the total amount of sales (since the last time this button was pressed). This window will also have a button that will reset the stock levels to their start values, and will reset the total sales to 0. Requirements Your goal is to implement this GUI with the functionality described above. You should note the following: (1) (Requirement) Design a main class that extends the JFrame Java class. You can model this on the examples discussed in the course notes, for example. There are nine products to be “sold” in this vending machine. Each product will have its own button element that, when pressed, will decrement the stock of the particular item and will add its price to the total sales of the vending machine. So you need to implement an appropriate ActionListener that will respond to these button presses. The GUI that I have developed looks like the following: 1 Please note that the exact look and feel of your program will depend upon the operating system you are using, as well as (possibly) the version of Java, the libraries, etc. The important things to notice are the prices of the items, and the initial stock of each item. The exact design is left to you, but it should be clear what the price of each item is, as well as the stock level of each item. Each item is on a JButton element here, and clicking on that button will decrease the stock of the item and add the price to a running total of sales (which can be displayed as described later). The stock levels of items should be updated on the main window (you can edit text contained in JLabels, for example, by using the setText method). (2) (Requirement) Note that there is an additional button in the bottom right hand corner. Obviously this type of information won’t be available to the “buyers” who would use this vending machine, but we put this here for convenience. Pressing this button should bring up another separate window, that shows the total sales, and has a button to reset the stock levels (back to the starting values of 4). This “reset” button should also reset the total sales to 0. As I stated, this should be in another window. You can see this (very small) window over top of the main one in the picture below. (3) (Requirement) The other functionality that your GUI should have is that when a user presses a button for an item that has no stock remaining, they should get a window that tells them that there are no items remaining. (Obviously, stock shouldn’t go negative, and no value will be added to the total sales in this case.) As example of this type of message is also shown below: 2 (4) (Requirement) Closing the main window should exit your application. Closing the “Vendor Informa- tion” window should *not* close the application, nor should closing the other “no stock left” message windows. Sample output Sample output is essentially shown above. As stated, stock levels should be updated on the main screen as they decrease or are reset by the “reset” button. Users should get a message box if they select an item that has 0 stock remaining. Hints 1. You could use several classes or one class, that choice is up to you. 2. As stated, you will need at least one ActionListener to capture events as buttons are pressed. In my implementation, I actually only had one action listener that worked for all buttons. You can get the source of the button press by using the getSource method of an event, which will return a JButton element (we know that we will get a JButton element since that is all we are using in this case. And then you can compare that element to see if it is equal to another JButton. For example, if e is an ActionEvent, we can get the source of the button press (since we know it will have been generated from a JButton element in this case) with this code: JButton pressed = (JButton) e.getSource(); If button is another (already declared) JButton, we can test if the event was generated by that element with this type of test: if (pressed == button) ... 3. Using appropriate tests, I actually had *one* ActionListener that I attached to all button elements. You can use one or several such listeners. If you use several, you will likely need to implement one or more of them as external classes (similar to how I implemented the CloseButtonListener class, i.e. have a class that implements the apprpriate interface, define a constrcutor that could take parameters as input, etc,. 4. I would suggest using Java arrays. Make an array of strings for the button labels. Make an array of costs for the items that are being sold. Make an array of JButton elements for the nine items. Make an array of JLabel items for the “stock levels”. And so forth... Use for loops to initialize many of these elements and insert them in the frame. With a little thought, you can use such for loops, rather than creating and inserting these elements individually. As stated before, a JLabel element has a setText method that can be used to set (alter) the text that a label is displaying. 5. Use a for loop to identify which button has been pressed for an item that has been “sold”. As stated previously, you can get the source of an event, and then compare it to (previously defined) button variables to see if they are the same (to identify which button fired the event). 3 6. The arrays you create should probably be class variables so that you can access them and/or change them as appropriate. Declare them outside of the constructor, but create them inside the constructor. Then other methods can access those variables. 7. Likely you should create the main code to draw the GUI first (or at least the main window), and then add the code to get the functionality you want after that. 8. If you haven’t figured this out by now, code incrementally. In other words, write a small bit of code and test that. Fix any problems before trying to write more code. 9. Check the examples given in class, and the couple of practicals given about GUIs and events. Some small additional amount of coding If you like, choose a (friendly to the eyes color scheme) for the buttons in the GUI. For example, make all of the “Jigglypuff” buttons have the same color. Or, have all the “Chocolate” buttons have the same color. This color shouldn’t obfuscate the text on the buttons. If the stock of an item drops to 0, change the background color of the corresponding JLabel field to indicate this to the user. Again, this background color shouldn’t obfuscate the label text. Be certain to change the background color back to “normal” when the item gets restocked by the “reset” button. Part II Doors I see lots of doors. . . Now we’re going to consider a situation where there are people who like to open and shut doors in possibly strange fashions. What we are going to be interested in is how many doors are open when one (or more) of them is done with their task. The setup For a given integer N ≥ 1, we have a set of N + 1 doors labelled (say, left to right) 0, 1, 2, . . . , N . For the purposes of this problem, we are going to have 1 ≤ N ≤ 1000000 = 106. This will allow us to use a Java array to store the status of a door, being either closed or open. Note: I would actually suggest an array of boolean values, say where false means the door is closed and true means the door is open. I am going to use the word “toggle” to mean that someone changes the status of a door, i.e. if it’s closed, they open it, and if it’s open, they close it. The players We have three different people we are going to consider for this problem. These people have a sequence of actions that will mean they open and/or close doors as appropriate. Ginny Ginny will toggle doors based on the “greatest common divisor” of two numbers. In case you don’t know/re- member what the “greatest common divisor” of two integers is, it’s the largest integer that evenly divides both of them. We will use “gcd(a, b)” to denote the greatest common divisor of two integers a and b, where at least one of a and b is not equal to 0. So, for example gcd(12, 5) = 1, gcd(36, 28) = 4, and gcd(240, 36) = 12. Thankfully, it is relatively easy to find the gcd of two numbers. In fact, it’s the so-called “Euclidean algorithm” that does this. (I know that Prof. Wong has told you that lots of algorithms have people’s names associated with them. This is another such example.) 4 Pseudocode for the Euclidean algorithm is given below (recall that “a mod b” is the remainder when a is divided by b): GCD(a,b) (The Euclidean Algorithm) Input Two nonnegative numbers a and b, not both 0. Output The greatest common divisor of a and b. If b==0: return a else: return (b, a mod b) For a given