These labs deal with basic inheritance structures. Please submit all labs to your google drive folder by 11:59 pm, Thursday, February 1st.
A video version of this lesson can be watched here: https://www.youtube.com/watch?v=jYxh02zBLDE and https://www.youtube.com/watch?v=m655Fw48AW8 and https://www.youtube.com/watch?v=Y5uWFfG1ejg
1. Copy my Vehicle.java program (found here: https://drive.google.com/drive/folders/1VuTI2RXdQ3o-Ri8YX4W04Q0ciuo4GIXo?usp=sharing) into your BlueJ project. You will turn this in. Done.
2. Now write a class called Car.java which extends my Vehicle class. Your Car class should have the following added/modified functionality by overriding certain methods:
-your class should contain two new String instance variables for the make and model of your car
-the constructor should input an int, and then two Strings representing the year, make and model of the car in that order. For instance, to make my car I would make a new Car(2000,"Volvo","S70"); Remember to call super!
-canGo() and canTurn() should now always return true instead of false
-write a method called canGoFast() which should return true if the car’s year is 2000 or greater OR if the make of the car is "Ferrari"
3. Now, write a class called Convertible.java which extends Car (yes, you can do this!). Your constructor will take in the same inputs in the same order (remember what you need to do in the first line of the constructor!). Now make the following modifications:
-add a boolean instance variable (boolean isTopDown) to represent whether the top is down. The default value should be false (meaning that the top is not down) after the constructor is called.
-the canGoFast() method should always return true now
-add a topDown() method, an accessor that takes no inputs and returns a boolean representing whether the top is down. This function should not take any inputs.
-write two void methods, raiseTop() and lowerTop() which toggle the boolean variable - raiseTop() sets it to be false, lowerTop() sets it to be true. Neither method should take any inputs.
4. Create a class called Item.java – this class should represent an item in some sort of video game. Each Item should have two instance variables (make these private and then access them through mutator/accessor methods)):
-a double representing the weight of the item
-a String representing the name of the item
Your item class should have the following methods
-Item(String iName, double iWeight) – a constructor
-getWeight
-getName //accessors with no inputs
-changeName(String s) – a void mutator method
-toString() – your toString method should return the name of the object and the weight in that order (with a space or dash in between if you like)
5. Now, you will create a class named Weapon.java – this class should extend Item. In addition, it should have the following instance variables
-an int representing the minimum damage the weapon can do
-an int representing the maximum damage the weapon can do
You should also write the following methods (remember that all the methods from Item are implicitly included!):
-a constructor which takes in the name, the weight, and then the min damage, then the max damage (in that order - String, double, int, int) and then sets up your object. Remember to use super in your constructor!
-getMinDamage() and getMaxDamage() - accessor methods which return ints
-attackDamage() – this method takes no inputs and returns an int representing a random number between the minimum damage and the maximum damage. Make sure that it can actually return every possible number in between. If your minimum damage is 12 and the maximum is 18, make sure this method could return anything from 12-18, not just 12-17 or something else.
-toString() – override the toString() method from item to also include the minimum and maximum damage. The keyword super can save you some time by calling Item's toString() and just returning something like super.toString() + minDamage + " - " + maxDamage
6. Now create a class named Armor.java – this class will also extend Item and has two additional instance variables:
-an int representing the armor class of the suit of armor, this tells you how strong the armor is
- a double representing the durability of the armor
Also write the following methods:
-a constructor that takes in three inputs - the name (String), the weight (a double), and the armor class (an int) in that order. Again, remember super! It should set the initial durability to be the armor class times (*) the weight of the armor (note this durability is not one of the inputs to your constructor).
-getArmorClass()
-getDurability() //accessor methods
-isBroken() – this method should check to see if the durability is < or = to 0 and return a boolean representing that. If it would return true, it also sets the armor class to be 0.
-getAttacked( ) //this method should input an int representing an amount of damage from a weapon and return an int as well. The method will return the amount of damage - (minus) the armor class of the armor. If this number would be negative, have it return zero. This method should also decrease the durability by that difference.
For example, if you have an Armor object with armor class of 20 and durability of 75, and you call getAttacked(25), it would return 5 (25-20), and decrease the durability by 5 to 70.
If you have an Armor object with armor class of 20 and durability of 75, and call getAttacked(11), 11-20 = -9, have the method return 0 and the durability should remain unchanged.
-toString() – again, override and include (via super) the toString() from item and also include the armor class
7. Write a class called ItemDriver.java which has the following methods. Note that every method in this class should be static! This class has several methods dealing with arrays and ArrayLists of Items. You should test everything via a main method:
Write the following methods:
-totalWeight( ) – this method inputs an array of items, then sums the weight of all of the items and returns that as a double.
-attack( ) – this method inputs a weapon and an armor in that order, and returns an int representing the damage done by a random attack from the weapon attacking the armor. It would be a good idea to call the getAttacked method that you wrote.
-countWeapons( ) – this method inputs an array of items (Item[] – so they can be items, weapons, or armor) and counts how many weapons there are, then returns that int
-countPotions( ) – this method inputs an ArrayList (NOT an array) of items and counts how many of them have the word “potion” somewhere in their name (so "potion", "potion of healing" and "mana potion of death" would all count) then returns that number as an int. Use a for each loop!
-averageAttack(Weapon w, int reps) - this method inputs a Weapon and an int. It should return a double. The method will then call the attackDamage() method on that Weapon reps times (so if you call averageAttack(w, 100), it will call attackDamage() 100 times on that weapon) and computer return the average number returned from each of those method calls. For instance, if you call the attackDamage 3 times and it deals 12,14,15 damage, this method should return 13.6666666666 (or so). This is a good way to test your attackDamage() method - if reps is > 100 or so, the averageAttack() should be very close to the median of min and max damage. You may assume reps will be a positive number.
-heavyArmor( ) - this method inputs an array of Items and will return an Item object. Specifically, it will return the heaviest Armor - the Item object with the highest weight that is also an Armor. Note that if the heaviest item is a weapon, this method should NOT return it - it must return an Armor. If there are no Armor objects in the array, it should return null.
TEST ALL OF YOUR METHODS THOROUGHLY! Then submit Item.java, Weapon.java, Armor.java, ItemDriver.java, Vehicle.java (just submit mine, no changes needed), Car.java and Convertible.java
8. Want some extra credit?? This will not involve more inheritance, but go back to Character.java and Fight.java from last semester. Then put them in the same project as your Item, Weapon and Armor, then give Armor and Weapons to both of the characters in your fight! If you choose this option, make sure to email me to let me know that you have done this and tell me what you named your files (gborish (at) hartdistrict.org). Worth up to 2 extra credit points