Exercise 1
Create a project named Stores, and implement the following classes:
- An abstract class called
Store. It will have an abstract method calledwelcome, two private fields (cashanddrinkPrice, both double) and a constructor that will receive thedrinkPriceand initializecashto 0.0. It will also implement a method calledpayDrinks(int numOfDrinks)that will add to thecashvariable the payment (number of drinks * price).- A class called
LiquorStorethat will extendStoreclass. It will implement thewelcomemethod to show the message “Welcome to the liquor store”, and it will have a new private field calledtax(an integer). Its constructor will receive thedrinkPriceandtaxvalues, calling the parent’s constructor when necessary. This class will also overridepayDrinks(int)calling first the parent’s method (pay without tax), and then adding the tax value to the cash field.- A
Mainclass with themainmethod. This method will:
- Create a
LiquorStoreobject with drinkPrice = 8.95€ and tax = 20%. Pay for 10 drinks and print the cash to see if its value is 107.40€ (print 2 decimal numbers).- Instantiate a
Storeusing an anonymous class. The drink price will be 8.95€ and thewelcomemethod will say “Welcome to anonymous store! Our drink price is XX€” (where XX will be the value of drinkPrice attribute, with 2 decimals). Call thewelcomemethod and also pay for 10 drinks. Show the resulting cash (should be 89.50€).- Implement getters and setters when needed
Exercise 2
Create a project named KillEnemies, and implement the following classes:
- Interface
Character: With a method calledisEnemy()that returns a boolean.- Class
Friend: ImplementsCharacter.isEnemy()returns false.- Class
Enemy: ImplementsCharacter.isEnemy()returns true. Also implements a method calledkill()that shows this message: “Ahhhggg, you killed me, bastard!”.- Class
Maincontaining themainmethod. You must create a list (ArrayList) of 10Characterobjects (5 friends and 5 enemies), then, usingCollections.shuffle(List), randomize the order of the items. You have to travel through all characters checking if they are enemies, and if they are, you kill them (callkill()method).An example of execution message would be:
Character 0 is a friend! :-)
Character 1 is a friend! :-)
Character 2 is an enemy! kill it!
Ahhhggg, you killed me, bastard!
...