A5 • Inheritance, abstract, static, enum, file i/o, exceptions

The goals for this assignment are to provide you with experience in doing the following:

  • Creating enumerated classes
  • Creating classes with static members
  • Creating classes with exception handling
  • Reading from files
  • Working with ArrayLists
  • Getting user input from the command line

1. Create 2 directories in your repository named, one named pokemon and one named a5.  Add Makefiles to each directory.

2. Add an enumerated class named AttackNature that has the following named instances: Adamant, Brave, Lonely, and Naughty.  The class should be in the pokemon package.

3. Add an enumerated class named DefenseNature that has the following named instances: Bold, Lax, Relaxed, and Impish.    The class should be in the pokemon package.

4. Add to your pokemon directory a class named Pokemon that meets the following specifications.

  • The class should be within the pokemon package.
  • The class should be abstract (i.e. add the abstract keyword before public class Pokemon).
  • The class should have the following private fields
    • attack (type: AttackNature)
    • defense (type: DefenseNature)
    • health (type: int)
  • The class should have getters and setters for attack, defense, and health.
  • The class should have an abstract method with the following signature: abstract public String getName();
  • In the setter for health, if the value is outside the interval [0,100] the program throws a build-in Java exception named IllegalArgumentException and found in java.lang.

5. Add to your pokemon directory classes named Bulbasaur, Caterpie, Squirtle, Charmander, and Charizard. Each class should meet the following specifications.

  • The class should be within the pokemon package.
  • The class should extend Pokemon.
  • The class should have a static field named name whose value is the equal to the name of the class and remains constant.
  • The class should have a method named getName that returns the value in the name field.
  • The class should have a static method named getAsciiArt() that returns a string containing the ASCII art for the respective character.  You can use the ASCII art found at https://www.ascii-code.com/ascii-art/video-games/pokemon.php or any other freely available ascii art.  You can escape the backslashes with this tool: https://www.freeformatter.com/java-dotnet-escape.html.

6. Add to your a5 directory a file named team1.txt that contains the description of 0 or more Pokemon.  One Pokemon is described on each line.  A Pokemon description contains a colon separated list of Pokemon statistics in the following order:

  • name  (Bulbasaur, Caterpie, Squirtle, Charmander, or Charizard)
  • attack (Adamant, Brave, Lonely, or Naughty)
  • defense (Bold, Lax, Relaxed, and Impish)
  • health (1-100)

An example description of a Pokemon in team1.txt is shown below.

Bulbasaur:Adamant:Bold:50

7. Add to your a5 directory an executable class named Pokedex that meets the following specifications.

  • The class should be in the default package.
  • The program creates a continuous loop and while in the loop does the following:
    • Displays a menu with the following options:
      1. Load Team
      2. Display Team
      3. Exit
    • If the user choose 1 the program does the following.
      • It asks the user for a file name, reads the file name from the keyboard, and attempts to open the file.
      • If successful, the program parses the Pokemon descriptions in the file.  For each description read, the program creates an instance of a the Pokemon described and adds it to an ArrayList named team1.  After all descriptions have been parsed, the program closes the file reader.
      • If the file can not be opened the program prints to the console “Can not open file” and continues at the top of the loop.
      • If a parse error occurs, the program prints to the console “Error parsing file”, closes the file, and continues at the top of the loop.
    • If the user chooses 2, the program prints to the console all of the information available in each object of the ArrayList.  This information should be well formatted and easy to read.
    • If the use chooses 3, the program terminates.

When complete, delete the .class files in both directories and push your changes to GitHub.  This assignment is due at 5:00 pm on Tuesday, February 13.

© 2018 – 2019, Eric. All rights reserved.