exercise 1
play

Exercise 1 You need to create a class to manage preferences. In - PowerPoint PPT Presentation

Exercise 1 You need to create a class to manage preferences. In order to maintain consistency, there should only ever be one instance of this class. In order to maintain consistency, there should only ever be one instance of this class. Why


  1. Exercise 1 You need to create a class to manage preferences. In order to maintain consistency, there should only ever be one instance of this class. ● In order to maintain consistency, there should only ever be one instance of this class. Why ? What could happen ? ● How can you ensure that only one instance of a class is instantiated? ● Write the Java va code of the class. 2

  2. public class PrefManager { private PrefManager instance; private Map<String,Boolean> prefList; private PrefManager() {} public static synchronized PrefManager getInstance() { if (instance == null ) instance = new PrefManager(); prefList = new HashMap<String,String>(); That belongs in prefList.put("fullscreen", false ); prefList.put("sound", true ); the constructor prefList.put("cheats", false ); return instance; } public boolean getPreferenceForKey(String key) { return prefList.get(key); } public void addPreference(String key, boolean val) { prefList.put(key,val); } } 3

  3. public class PrefManager { private PrefManager instance; private Map<String,Boolean> prefList; GOOD private PrefManager() { prefList = new HashMap<String,String>(); prefList.put("fullscreen", false ); prefList.put("sound", true ); prefList.put("cheats", false ); } public static synchronized PrefManager getInstance() { if (instance == null ) instance = new PrefManager(); return instance; } public boolean getPreferenceForKey(String key) { return prefList.get(key); } public void addPreference(String key, boolean val) { prefList.put(key,val); } } 4

  4. Exercise 2 You are implementing a menu that has a recursive structure for a restaurant. Their menu contains (sub)menus and/or menu items. Each (sub)menu has (sub)menus and/or menu items. You want to be able to represent this hierarchy, and you want to be able to easily perform operations on the whole menu, or any of its parts. Draw a UM UML L class ss di diagr gram am for the sy syst stem 5

  5. Not bad except for inheritance instead of realization 12

  6. Exercise 3 Draw a UML class diagram for the system on the following slide for ordering Pizza. Once you’re finished drawing the UML diagram, write the Java code to implement the system. 13

  7. Exercise 3 1. interface PizzaOrder { boolean isVegan(); //true if it has no cheese or meat boolean hasMeat(); ArgoUML double price(); //dollars } //Don’t need a hasCheesemethod… 2. BasicMediumPizza – A regular pizza, nothing special, costs $10.00 – This pizza includes cheese by default (in real life), no need to additionally specify cheese (in the code) 3. NoCheese:Modifies a pizza so no cheese is used at no additional charge 4. Pepperoni : Modifies a pizza so pepperoni is added, for $1.00 5. Client : Creates a basic medium pizza, no cheese, with pepperoni. Checks the price and checks if it is vegan. 14

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend