Problem Set 2: Inheritance and Linked Lists


Problem Set

Repository link: https://github.com/CSE-116/ProblemSet-2

To submit your project, run problem.Zipper, which will create a zip file containing the problem set, and submit it to Autolab.


Specification


For this Problem Set, you will implement the following functionality. Note that all of the methods are public and non-static.

  • Monster (5 points): Create an abstract class in the problem package named Monster. This class should implement the following methods:
    • Constructor: create a constructor which takes a String and an int representing the name and maximum health of the monster. The current health of the monster should initially be set to the maximum amount.
      • Ex: if a Monster is created with a max health of 10, the initial health would also be 10. The max health should never change, even if the current health does.
    • getName: create a method named getName which takes no parameters and returns a String. This method should return the name of the Monster.
    • setName: create a method named setName which takes a single String and returns void. This method should set the name of the Monster to the input value.
    • getHP: create a method named getHP which takes no parameters and returns an int. This method should return the current health of the Monster.
    • setHP: create a method named setHP which takes a single int and returns void. This method should set the current health of the Monster to the input value.
    • getMaxHP: create a method named getMaxHP which takes no parameters and returns an int. This method should return the maximum health of the Monster.
    • attack: create an abstract method named attack which takes a reference to a Monster object and returns void.
  • Skeleton (5 points): Create a class in the problem package named Skeleton. This class should extend the Monster class described above. This class should implement the following methods:
    • Constructor: create a constructor which takes a String and two ints representing the name, maximum health, and damage of the skeleton, respectively.
    • getDamage: create a method named getDamage which takes no parameters and returns an int. This method should return the damage value of the Skeleton.
    • setDamage: create a method named setDamage which takes a single int and returns void. This method should set the damage value of the Skeleton to the input value.
    • reanimate: create a method named reanimate which takes no parameters and returns void. If the Skeleton's health is less than or equal to 0, it should set it to the maximum health of that object. If its health is greater than 0, it should do nothing.
    • attack: override the abstract method of the same name from the Monster class. It should take a reference to a Monster object and return void. This method should decrease the health of the Monster parameter by the damage of that Skeleton.
      • For example, if a Skeleton was created with a damage of 5, and a Monster with a health of 8 was passed in, its health would be decreased to 3.
      • The health of the passed in object is allowed to become negative. For example, if a Skeleton was created with a damage of 5, and a Monster with a health of 1 was passed in, its health would be decreased to -4.
  • Creeper (5 points): Create a class in the problem package named Creeper. This class should extend the Monster class described above. This class should implement the following methods:
    • Constructor: create a constructor which takes no parameters. The name should always be "creeper" and the max hp should always be 100.
    • attack: override the abstract method of the same name from the Monster class. It should take a reference to a Monster object and return void. This method should set the hp of the monster and the creeper to 0.
  • StringList (5 points): Create a class in the problem package named StringList. This class will contain methods for using and manipulating LinkedListNodes of Strings. You must use the LinkedListNode class provided, which can be found in problem.datastructures. This class should implement the following methods:
    • getList: create a method named getList which takes no parameters and returns a LinkedListNode of Strings. This method should return the entire linked list of strings.
    • setList: create a method named setList which takes a LinkedListNode of Strings as a parameter and returns void. This method should set the linked list of strings to the input value.
    • addElement: create a method named addElement which takes a single String as a parameter and returns void. This method should append the input String to the end of the linked list.
  • getMidpoint (10 points): create a method in the StringList class named getMidpoint which takes no parameters and returns a String. This method should return the value at the middle index of the linked list.
    • If the list has an odd number of elements, it should return the middle element. For example, calling getMidpoint with the list ["cat","dice","lemon","juicebox","elephant"] would return "lemon", since it is at the middle index.
    • If the list has an even number of elements, it should return the two middle elements concatentated together, separated by a space. For example, calling getMidpoint with the list ["cat","dice","lemon","juicebox","elephant","apple"] would return "lemon juicebox", since indices 2 and 3 are equally close to the midpoint of the list.
    • If the list is empty, this method should return the empty String.
  • deleteElement (10 points): create a method in the StringList class named deleteElement which takes a String as a parameter and returns void. This method should remove the first occurrence of the input String from the list.
    • If the list is empty, or does not contain the input value, this method should do nothing.
    • If the list contains multiple of the value being deleted, this method should only remove the first. For example, calling delete("dice") on the list ["dice","cat","dice"] would result in the list ["cat", "dice"].
    • When comparing two Strings for equality, always use the .equals method. Do not compare them using ==.
  • zipperMerge (10 points): create a method in the StringList class named zipperMerge which takes a StringList as a parameter and returns void. This method should zipper merge the parameter list with this list. The zipper should start with this list and merge the two lists by alternating values from the lists. The list from the parameter should remain unchanged.
    • If the end of one list is reached before the zipper ends, the rest of that list must be added to the end of the list.
    • Either list might be empty when this method is called. Be sure to handle these cases
    • Example: If the list that called the method is ("a", "b", "c", "d") and the method is called with a parameter of ("e", "f", "g", "h"), the calling objects list should be ("a", "e", "b", "f", "c", "g", "d", "h").
    • Example: If the list that called the method is ("a", "b") and the method is called with a parameter of ("e", "f", "g", "h"), the calling objects list should be ("a", "e", "b", "f", "g", "h").
    • Example: If the list that called the method is ("a", "b", "c", "d") and the method is called with a parameter of ("e", "f"), the calling objects list should be ("a", "e", "b", "f", "c", "d")