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 (cash
anddrinkPrice
, both double) and a constructor that will receive thedrinkPrice
and initializecash
to 0.0. It will also implement a method calledpayDrinks(int numOfDrinks)
that will add to thecash
variable the payment (number of drinks * price).- A class called
LiquorStore
that will extendStore
class. It will implement thewelcome
method to show the message “Welcome to the liquor store”, and it will have a new private field calledtax
(an integer). Its constructor will receive thedrinkPrice
andtax
values, 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
Main
class with themain
method. This method will:
- Create a
LiquorStore
object 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
Store
using an anonymous class. The drink price will be 8.95€ and thewelcome
method will say “Welcome to anonymous store! Our drink price is XX€” (where XX will be the value of drinkPrice attribute, with 2 decimals). Call thewelcome
method 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
Main
containing themain
method. You must create a list (ArrayList
) of 10Character
objects (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!
...