In: Computer Science
Using Java
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. 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 in this project are:
"PersonStack is full – object not added."
"PersonStack is empty – no object to return."
"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, you should define a class called PersonStack, which will implement the stack containing an array of Person objects. (Use the Person and Date classes provided on Canvas below.)
The details of this project:
Finally, you will need to write a main “tester” program. (Please put this method in its own class, not with PersonStack.) This program will:
In each “catch” block, print the exception message, but do not stop.
Please do not change the Person or Date classes.
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 |
//first solution
class Person
{
String name,dob;
Person(String name,String dob)
{this.name=name;
this.dob=dob;
}
}
class IllegalObjectTypeException extends Exception{
IllegalObjectTypeException()
{super("default message");
}
IllegalObjectTypeException(String s){
super(s);
}
}
class StackFullException extends Exception{
StackFullException()
{ super("default message");
}
StackFullException(String s){
super(s);
}
}
class StackEmptyException extends Exception{
StackEmptyException(){
super("default message");
}
StackEmptyException(String s){
super(s);
}
}
class PersonStack
{
int capacity=5;
Person p[]=new Person[capacity];
int top=0;
public void push(Object obj) throws Exception
{
if(obj==null||obj instanceof
Person==false)
throw new
IllegalObjectTypeException("Object not a Person - object not
pushed");
else if(top>=capacity)
throw new
StackFullException("PersonStack is full - object not added");
else
p[top++]=(Person)obj;
}
public Person pop() throws Exception
{
if(top<=0)
throw new
StackEmptyException("PersonStack is empty - no object to
return");
else
{
return
p[--top];
}
}
public String toString()
{String s=" Here are the contents of the stack:
\n";
for(int i=0;i<top;i++)
{
s+="Name:
"+p[i].name+" born: "+p[i].dob+"\n";
}
return s;
}
}
class Tester
{
public static void main(String arg[])
{
PersonStack ps=new PersonStack();
try
{
System.out.println("Popping the
contents of the stack plus one.");
Person p1=new Person("Harry
Potter","July 31, 1997");
ps.push(p1);
Person p2=new
Person("Beyoncé","September 4, 1981");
ps.push(p2);
Person p3=new Person("James T. Kirk
","March 22, 2233");
ps.push(p3);
Person p4=new Person("Tom
Brady","June 1, 1989");
ps.push(p4);
Person p5=new Person("Oprah
Winfrey","March 25, 1975");
ps.push(p5);
System.out.println(ps.toString());
Person p6=new Person("Harry
Potter","July 31, 1997");
ps.push(p6);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
try
{Person p=ps.pop();
System.out.println("Popping the
contents of the stack plus one.");
System.out.println("Name:
"+p.name+" born: "+p.dob);
p=ps.pop();
System.out.println("Name:
"+p.name+" born: "+p.dob);
p=ps.pop();
System.out.println("Name:
"+p.name+" born: "+p.dob);
p=ps.pop();
System.out.println("Name:
"+p.name+" born: "+p.dob);
p=ps.pop();
System.out.println("Name:
"+p.name+" born: "+p.dob);
p=ps.pop();
System.out.println("Name:
"+p.name+" born: "+p.dob);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
try
{
String s="Apple";
ps.push(s);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
//if you want Date class Then Second solution
class Date
{
int day,year;
String month;
Date(int day,String month,int year)
{this.day=day;
this.month=month;
this.year=year;
}
public String toString()
{String s="";
s+=this.month+" "+this.day+", "+this.year;
return s;
}
}
class Person
{
String name;
Date dob;
Person(String name,Date dob)
{this.name=name;
this.dob=dob;
}
}
class IllegalObjectTypeException extends Exception{
IllegalObjectTypeException()
{super("default message");
}
IllegalObjectTypeException(String s){
super(s);
}
}
class StackFullException extends Exception{
StackFullException()
{ super("default message");
}
StackFullException(String s){
super(s);
}
}
class StackEmptyException extends Exception{
StackEmptyException(){
super("default message");
}
StackEmptyException(String s){
super(s);
}
}
class PersonStack
{
int capacity=5;
Person p[]=new Person[capacity];
int top=0;
public void push(Object obj) throws Exception
{
if(obj==null||obj instanceof
Person==false)
throw new
IllegalObjectTypeException("Object not a Person - object not
pushed");
else if(top>=capacity)
throw new
StackFullException("PersonStack is full - object not added");
else
p[top++]=(Person)obj;
}
public Person pop() throws Exception
{
if(top<=0)
throw new
StackEmptyException("PersonStack is empty - no object to
return");
else
{
return
p[--top];
}
}
public String toString()
{String s=" Here are the contents of the stack:
\n";
for(int i=0;i<top;i++)
{
s+="Name:
"+p[i].name+" born: "+p[i].dob.toString()+"\n";
}
return s;
}
}
class Tester
{
public static void main(String arg[])
{
PersonStack ps=new PersonStack();
try
{
System.out.println("Popping the
contents of the stack plus one.");
Date dob=new
Date(31,"July",1997);
Person p1=new Person("Harry
Potter",dob);
ps.push(p1);
dob=new
Date(4,"September",1981);
Person p2=new
Person("Beyoncé",dob);
ps.push(p2);
dob=new
Date(22,"March",2233);
Person p3=new Person("James T. Kirk
",dob);
ps.push(p3);
dob=new Date(1,"June",1989);
Person p4=new Person("Tom
Brady",dob);
ps.push(p4);
dob=new
Date(25,"March",1975);
Person p5=new Person("Oprah
Winfrey",dob);
ps.push(p5);
System.out.println(ps.toString());
dob=new Date(31,"July",1997);
Person p6=new Person("Harry
Potter",dob);
ps.push(p6);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
try
{Person p=ps.pop();
System.out.println("Popping the
contents of the stack plus one.");
System.out.println("Name:
"+p.name+" born: "+p.dob.toString());
p=ps.pop();
System.out.println("Name:
"+p.name+" born: "+p.dob.toString());
p=ps.pop();
System.out.println("Name:
"+p.name+" born: "+p.dob.toString());
p=ps.pop();
System.out.println("Name:
"+p.name+" born: "+p.dob.toString());
p=ps.pop();
System.out.println("Name:
"+p.name+" born: "+p.dob.toString());
p=ps.pop();
System.out.println("Name:
"+p.name+" born: "+p.dob.toString());
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
try
{
String s="Apple";
ps.push(s);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}