Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

\'m having a problem with my study guide question for my Java class with excepti

ID: 3836313 • Letter: #

Question

'm having a problem with my study guide question for my Java class with exceptions and Stacks.My test is in a few days any help quickly would be appreciated. This is the prompt : A stack is a type of data collection on which things can be “pushed” and “popped”. For example, a stack of plates can have a plate added (or “pushed”) onto the stack, and a plate removed (or “popped”) from the stack. Plates are pushed onto or popped off of the stack one at a time. Notice that the last plate pushed onto the stack is the first plate popped off of the stack. This type of structure is known as “LIFO” – last in, first out. In this project, you will create a stack that will hold objects of type Person. The stack itself will be in a class named PersonStack. When manipulating the stack, three types (or classes) of exceptions can be thrown. Each Exception class will contain two constructors, one which provides a default message for the exception, and one which takes a String as an argument and sets the exception message to that String. The three Exception classes are: StackFullException – thrown when trying to add (or “push”) an object onto the stack which already has reached its maximum size. The default exception message is “PersonStack is full – object not added.” StackEmptyException – thrown when trying to remove (or “pop”) an object from the stack when the stack is empty. The default exception message is “PersonStack is empty – no object to return.” IllegalObjectTypeException – thrown when trying to add an object to the stack that is not an instance of the correct type of object which the stack can hold. (In this project, the stack holds objects of type Person.) The default exception message is “Object is not a Person – object not pushed.” Each class above extends Exception, so it is “checked”, and must appear inside a try-catch statement. Next, define a class called PersonStack, which will implement the stack containing an array of Person objects. (Use the Person and Date classes provided on Blackboard). Details: The stack is implemented using an array that can hold a maximum of 5 Person objects. Initially, the stack (the array) will be empty, that is, it will contain no objects Create an int variable which points to the “next available stack position” in the array. Initialize this variable to 0, meaning that the stack is empty. The class will provide a method named push(), which will take one argument, an object of type Object. If the object is null, or if the object is not an instance of the Person class, then this method will throw an exception of type IllegalObjectTypeException, but the push method will not catch the exception. If the stack is full, then this method will throw an exception of type StackFullException, but will not catch it. Otherwise, the method will typecast the object to a Person, place the object on top of the stack (the element in the array referenced by the “next available stack position” variable), and increment that variable. The class will provide a method named pop(): If the stack is empty, this method will throw an exception of type StackEmptyException, but will not catch it Otherwise, the method will decrement the variable referencing the “next available stack position”, remove the Person object currently occupying that position from the stack (array), and return the Person object that was just removed from the stack. The class will provide a method named toString(), which lists all of the Person objects currently residing on the stack. You do not need to include an equals method for the PersonStack class Finally, you will need to write a main “tester” program. This program will: Instantiate an object of the PersonStack class – aka the “stack”. Inside a “try” block: Instantiate and “push” 5 different Person objects onto the stack. Print the contents of the stack (use the stack’s toString method). Attempt to “push” a 6th Person object onto a stack, which should throw a StackFullException. Inside a second “try” block “Pop” five Person objects from the stack. As each is “popped”, print a message showing which object was returned. This should empty the stack. Try to “pop” a 6th object from the stack, which should throw a StackEmptyException. In a third “try” block: Instantiate an object of any type other than Person. (For example, you could use a Date or a String object.) Try to “push” this object onto the stack. This should throw an IllegalObjectTypeException After each “try” block above, you will need to add “catch” blocks to handle each of the types of exceptions which can be thrown by methods inside the “try” block, since those methods do not handle the exceptions themselves The “push” method can throw a StackFullException and an IllegalObjectTypeException. The “pop” method can throw a StackEmptyException. In each “catch” block, print the exception message, but do not stop. A sample dialog might look like: Just added five people onto the stack Here are the contents of the stack: Name: Harry Potter born: July 31, 1997 Name: Beyoncé born: September 4, 1981 Name: James T. Kirk born: March 22, 2233 Name: Tom Brady born: June 1, 1989 Name: Oprah Winfrey born: March 25, 1975 Trying to push one more onto the stack: PersonStack is full - object not added Popping the contents of the stack plus one. Popped: Name: Oprah Winfrey born: March 25, 1975 Popped: Name: Tom Brady born: June 1, 1989 Popped: Name: James T. Kirk born: March 22, 2233 Popped: Name: Beyoncé born: September 4, 1981 Popped: Name: Harry Potter born: July 31, 1997 PersonStack is empty - no object to return Trying to add an Object object to the stack. Object not a Person - object not pushed. I'm having trouble making the stack and moving it. I'm also having trouble using the 2 files our teacher gave us date and person class.

Explanation / Answer

PROGRAM CODE:

Person.java

package stack;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Person {

   private String firstName;

   private String lastName;

   private Date date;

  

   public Person(String fname, String lname, Date date) {

       firstName = fname;

       lastName = lname;

       this.date = date;

      

   }

  

   public String getFirstName() {

       return firstName;

   }

  

   public String getLastName() {

       return lastName;

   }

  

   public String getDate()

   {

       DateFormat format = new SimpleDateFormat("MMMM dd, YYYY");

       return format.format(date);

   }

  

   @Override

   public String toString() {

       return " Name: " + firstName + " " + lastName + " born: " + getDate();

   }

}

PersonStack.java

package stack;

import student.Person;

public class PersonStack {

  

   Person persons[];

   static int capacity = 5;

   int nextAvailableStackPosition;

  

   public PersonStack() {

       persons = new Person[capacity];

       nextAvailableStackPosition = 0;

   }

  

   public void push(Object obj)

   {

       if(obj == null || !obj.getClass().getSimpleName().equals("Person"))

           throw new IllegalObjectTypeException();

       else if(nextAvailableStackPosition == 5)

           throw new StackFullException();

       else

       {

           Person person = (Person) obj;

           persons[nextAvailableStackPosition++] = person;

       }

   }

  

   public Person pop()

   {

       if(nextAvailableStackPosition == 0)

           throw new StackEmptyException();

       else

       {

           nextAvailableStackPosition--;

           Person person = persons[nextAvailableStackPosition];

           persons[nextAvailableStackPosition] = null;

           return person;

       }

   }

   @Override

   public String toString() {

       String result = "";

       for(int i=0; i<nextAvailableStackPosition; i++)

           result += persons[i].toString() + " ";

       return result;

   }

}

StackFullException.java

package stack;

public class StackFullException extends RuntimeException{

  

   String message;

  

   public StackFullException() {

       message = "PersonStack is full – object not added.";

   }

  

   public StackFullException(String message) {

       this.message = message;

   }

  

   @Override

   public String getMessage() {

       return message;

   }

}

StackEmptyException.java

package stack;

public class StackEmptyException extends RuntimeException{

  

   String message;

   public StackEmptyException() {

       message = "PersonStack is empty – no object to return.";

   }

  

   public StackEmptyException(String message) {

       this.message = message;

   }

  

   public String getMessage() {

       return message;

   }

}

IllegalObjectTypeException.java

package stack;

public class IllegalObjectTypeException extends RuntimeException{

  

   String message;

  

   public IllegalObjectTypeException() {

       message = "Object is not a Person – object not pushed.";

   }

  

   public IllegalObjectTypeException(String message) {

       this.message = message;

   }

  

   public String getMessage() {

       return message;

   }

}

Tester.java

package stack;

import java.text.SimpleDateFormat;

public class Tester {

   public static void main(String[] args) {

       PersonStack stack = new PersonStack();

       try

       {

           Person p1 = new Person("Michael", "Smith", new SimpleDateFormat("dd-MM-YYYY").parse("12-09-1997"));

           Person p2 = new Person("Philip", "Andrews", new SimpleDateFormat("dd-MM-YYYY").parse("28-4-1979"));

           Person p3 = new Person("Floyd", "Anthony", new SimpleDateFormat("dd-MM-YYYY").parse("14-2-2005"));

           Person p4 = new Person("Louis", "Phillip", new SimpleDateFormat("dd-MM-YYYY").parse("11-8-1968"));

           Person p5 = new Person("Blake", "Lively", new SimpleDateFormat("dd-MM-YYYY").parse("08-04-1955"));

           stack.push(p1);

           stack.push(p2);

           stack.push(p3);

           stack.push(p4);

           stack.push(p5);

           System.out.println("Just added five people onto the stack Here are the contents of the stack:");

           System.out.println(stack);

           System.out.println("Trying to push one more onto the stack: ");

           stack.push(p1);

       }catch (Exception e) {

           System.out.println(e.getMessage());

       }

       try

       {

           System.out.println("Popped: " + stack.pop());

           System.out.println("Popped: " + stack.pop());

           System.out.println("Popped: " + stack.pop());

           System.out.println("Popped: " + stack.pop());

           System.out.println("Popped: " + stack.pop());

           stack.pop();

       }catch (Exception e) {

           System.out.println(e.getMessage());

       }

  

       try

       {

           String object = "Just Chill";

           System.out.println("Trying to add an Object object to the stack. ");

           stack.push(object);

       }catch (Exception e) {

           System.out.println(e.getMessage());

       }

   }

}

OUTPUT:

Just added five people onto the stack Here are the contents of the stack:

Name: Michael Smith born: September 12, 1997

Name: Philip Andrews born: April 28, 1979

Name: Floyd Anthony born: February 14, 2005

Name: Louis Phillip born: August 11, 1968

Name: Blake Lively born: April 08, 1955

Trying to push one more onto the stack:
PersonStack is full – object not added.
Popped:
Name: Blake Lively born: April 08, 1955
Popped:
Name: Louis Phillip born: August 11, 1968
Popped:
Name: Floyd Anthony born: February 14, 2005
Popped:
Name: Philip Andrews born: April 28, 1979
Popped:
Name: Michael Smith born: September 12, 1997
PersonStack is empty – no object to return.
Trying to add an Object object to the stack.
Object is not a Person – object not pushed.