CS 300

Second semester class projects.

CS 300

CS 300 Projects


Overview

Second semester computer science was CS 300. This class expanded my coding skills with the introduction of many techniques to make programs more efficient. These included using object in different classes, how to handle exceptions, inheritance and strategies for organizing data.


Object-Oriented Projects

The first object-oriented project was a game where the wolves would eat the bunnies. In this project I learned the difference between an Is-a relationship and a Has-a relationship. For instance, the bunnies could use anything from the animal class a bunny is an animal but the carrots could not.

Is-a vs. Has-a

To get access to the animal code I had to add extends to the class to tell the code that it has access to the animal attributes. Now all the attributes given to animals like the ability to move, and the ability to eat were given to the bunnies. This taught me a lot about how to derive from different classes and the importance of the class heirarchy.

    import java.io.File;
import java.util.Objects;

public class Rabbit extends Animal{
  private static final String RABBIT = "images"+ File.separator + "rabbit.png";
  private static final String TYPE = "R"; // A String that represents the rabbit type
  private static int hopStep = 70; // one hop step
  private static int scanRange = 175; // scan range to watch out for threats
  private static int nextID = 1; // class variable that represents the identifier
  // of the next rabbit to be created
  private final int ID; // positive number that represents the order of this rabbit
  /**
  * Creates a new rabbit object located at a random position of the display window
  */
  public Rabbit() {
  // Set rabbit drawing parameters
  super(RABBIT);
  // Set rabbit identification fields
  ID = nextID;
  this.label = TYPE + ID; // String that identifies the current rabbit
  nextID++;
  } 
Recursion

In this class I also learned about recursion through several projects including the movie project that organized movies based off of different criteria. In the code below I used recursion to recursively order the movies in String representation. Through this project I learned how recursion can be used to make code more efficient when repeating the same steps.

 @Override
  public String toString() {
    if(size == 1) {
      return this.root.getData().toString();
    }
    return toStringHelper(root);
  }

  /**
   * Recursive helper method which returns a String representation of the BST rooted at current. An
   * example of the String representation of the contents of a MovieTree is provided in the
   * description of the above toString() method.
   * 
   * @param current reference to the current movie within this BST (root of a subtree)
   * @return a String representation of all the movies stored in the sub-tree rooted at current in
   *         increasing order with respect to the result of Movie.compareTo() method (year, rating,
   *         name). Returns an empty String "" if current is null.
   */
  protected static String toStringHelper(BSTNode current) {
    String result = "";
    if(current == null) {
      return "";
    }
    //add left node
    if(current.getLeft() != null ) {
      result += toStringHelper(current.getLeft());
    }
    result += current.getData().toString() + "\n";
    //add right node
    if(current.getRight() != null) {
      result += toStringHelper(current.getRight());
    }
    return result; // remove this statement.
  }