CS 3340 OOP
Exercise 1: Stack: C/C++ to Java
Chapter Readings: TIJ [1] [2] [4] [5]

Goal: To introduce Java classes by comparing to C/C++, and to code a first Java program.


Tutorial

Consider a Person record struct in C: person.h

and corresponding testing program: person.c

Now consider the equivalent Person struct implemented as a class in C++: Person.hpp

with its own testing program: person1.cpp

Lets examine our first Java class: Person.java

Here is a C stack (LIFO) program from Data Structures: stack.c

and implemented in C++: stack1.cpp

All of the above is run in a demo. Note that compilation (javac) in Java requires the .java suffix but the run-time execution (java) does not allow the .class suffix in the compiled code name.


Assignment

Code your own version of the Stack.java. The code should operate on Person.java.

It might be best to copy from stack1.cpp: cp stack1.cpp Stack.java

Note that stack1.cpp works only on Person objects, but we want Stack.java to be re-usable for anything that qualifies as a Java "Object".

Use Person.java as help but your text readings are probably necessary to help with syntax, etc.

Compile and run:

javac Stack.java
java Stack 

Did it print the same as stack1.cpp?

Name 3  30
Name 2  20
Name 1  10

Turn-In: Stack.java, including commented-out listing of the resultant output.