3^2*(4-2)+6
Operators and parentheses are pushed on the stack as wrapped chars. In the example here, pushed will be: ^ * ( - ) +.
Here is the RPN: 32^42-*6+
34*23^8+/
Use the technique that was presented in class to evaluate the resulting RPN,
showing step-by-step the use of a stack.Actions: push 3; push 4; pop 4 and 3, push 3*4 = 12; push 2; push 3; pop 3 and 2, push 2^3 = 8; push 8; pop 8 and 8, push 16; pop 16 and 12, push 12/16 = 0.75.
// StackTest: Use Java's library Stack class (legacy)
// Read and push integers, then pop them
import java.util.*; // for the Stack class
public class StackTest {
public static void main(String[] args) {
// class to use for input of integers
GetData getData = new GetData();
// create a stacks of Objects
// part a
Stack s = new Stack();
// read ints using getNextInt() until a zero is read.
// wrap each int in an Integer object and
// push each int on the stack
// part b (three things to do)
int nextInt;
while ((nextInt = getData.getNextInt()) != 0) {
Integer nextIntWrap = new Integer(nextInt);
s.push(nextIntWrap);
}
// pop ints off the stack,
// print them as they are popped, and
// continue until the stack is empty
// part c (three things to do)
while (!s.empty())
System.out.println(s.pop());
}
}
Output is:
2 3 5 7 11 13 17 19 23 0 23 19 17 13 11 7 5 3 2
// Person: base class adapted from Weiss's book
public class Person implements Comparable {
private String name; // name of person
private String ssn; // SSN of person
private int age; // age of person
public Person (String n, String s, int a) {
name = n; ssn = s; age = a;
}
public String getName() { return name;}
public String getSsn() { return ssn;}
public int getAge() { return age;}
public String toString() {
return "Name: " + name + ", SSN: " + ssn + ", Age: " + age;
}
// compareTo: the method in Comparable interface
public int compareTo(Object obj) {
if (age < ((Person)obj).age) return 1;
else if (age > ((Person)obj).age) return -1;
else return 0;
}
}
// Employee: class that extends Person
public class Employee extends Person {
private double salary;
public Employee (String n, String ssn, int a, double sal) {
super(n, ssn, a); salary = sal;
}
public double getSalary() { return salary;}
public String toString() {
return super.toString() + ", Salary: " + salary;
}
}
// Staff: class that extends Employee
public class Staff extends Employee {
private int percentTime;
public Staff (String n, String ssn, int a, double g, int pct) {
super(n, ssn, a, g); percentTime = pct;
}
public int getPercentTime() { return percentTime;}
public String toString() {
return super.toString() + ", Percent Time: " + percentTime;
}
}
// Student: class that extends Person
public class Student extends Person {
private double gpa;
public Student (String n, String ssn, int a, double g) {
super(n, ssn, a); gpa = g;
}
public double getGpa() { return gpa;}
public String toString() {
return super.toString() + ", GPA: " + gpa;
}
}
// Persons: use the classes Person, Student, Employee, and Staff
public class Persons {
public static void main(String[] args) {
CompList list = new CompList();
Comparable p;
p = new Person("Williams, John", "534-54-2344", 23);
list.add(p);
p = new Employee("Johnson, Jane", "232-33-3343", 43, 16000.0);
list.add(p);
p = new Student("Wilkins, Jean", "123-89-5688", 19, 3.5);
list.add(p);
p = new Employee("Jacobs, Joe", "112-21-4023", 58, 47000);
list.add(p);
p = new Student("Wilson, James", "003-00-8755", 21, 2.75);
list.add(p);
p = new Staff("Wheeler, Jason", "458-67-2100", 34, 17000, 75);
list.add(p);
while (!list.isEmpty())
System.out.println(list.removeMin());
}
}
Along with List.java and CompList.java from
the CompList class.Here is the output of a run. Notice that it is in decreasing order by age.
Name: Jacobs, Joe, SSN: 112-21-4023, Age: 58, Salary: 47000.0 Name: Johnson, Jane, SSN: 232-33-3343, Age: 43, Salary: 16000.0 Name: Wheeler, Jason, SSN: 458-67-2100, Age: 34, Salary: 17000.0, Percent Time: 75 Name: Williams, John, SSN: 534-54-2344, Age: 23 Name: Wilson, James, SSN: 003-00-8755, Age: 21, GPA: 2.75 Name: Wilkins, Jean, SSN: 123-89-5688, Age: 19, GPA: 3.5