In: Computer Science
The purpose of this assignment is to build the business calculator using supporting files built inTopics 4 and 5.Create a Java application file named RPN.java containing a main method by using the ForthStack.java and associated files from Topic 5.The application should have one text box for numeric data entry, one text box for numeric display, one text box for error display, and buttons labeled "+", "-", "*", "/","dup", "2dup", "clr", "pop" and "push." The actions of the controls should be as follows.oThe text box for numeric display should display the top element of the stack, or blank if the stack is empty.oThe text box for numeric data entry should allow the user to type in a valid numeric value.oThe text box for error display should display an error message from the previous operation (if any), or be blank if the last operation was successful.oThe buttons labeled "+", "-", "*", "/", "dup", "2dup", "clr", and pop should invoke the corresponding methods in ForthStack; "pop" should remove and discard the top item on the stack, and "push" should push the numeric value in the numeric data entry box onto the stack and clear the numeric data entry box.oAll button operations should update the display of the top of the stack.The size of the stack used should be four, no more or less, in order to standardize testing.After thoroughly testing the program, submit the AbstractStack.java, ArrayStack.java,Forth.java, ForthStack.java, TestForthStack.java, and RPN.java
STACK CODE:
ArrayStack.java
import java.util.Arrays;
public class ArrayStack extends AbstractStack {
//Attributes
private double[] array;
private int size;
private int num;
//Default constructor
public ArrayStack() {
array = new double[3];
size = 3;
num = 0;
}
//Parametrized Constructor
public ArrayStack(int a){
array = new double[a];
size = a;
num = 0;
}
//Insert a new element
public void push(double a){
if (num < size) {
array[num] = a;
num++;
System.out.println("Success");
}
else {
throw new ArrayIndexOutOfBoundsException("Failure! Stack is
full");
}
}
//Take out last inserted value
public double pop(){
if (num > 0){
num--;
return array[num];
}
else {
throw new ArrayIndexOutOfBoundsException("Stack is empty");
}
}
//Check stack empty or not
public boolean isEmpty(){
return (num == 0);
}
//Get top element from stack
public double peek() {
return peek(num-1);
}
//Get specified index element
public double peek(int n){
try
{
if (num > 0){
if (n < 0 || n >= num)
return -1;
else
return array[n];
}
else{
System.out.println("Stack is empty");
return -1;
}
}catch(Exception e ){
e.printStackTrace();
}
return 0;
}
//Number of elements in stack
public int count(){
return num;
}
public void clear() {
size=0;;
num=0;
}
}
public class FourthStack extends ArrayStack implements
Fourth{
//Parameterized constructor
public FourthStack(int sz) {
super(sz);
}
@Override
//Pop 2 elements from stack and add values
//Push into stack
public void add() {
if(super.count()<2) {
throw new ArrayIndexOutOfBoundsException("Not enough elements to
pop");
}
else {
super.push(super.pop()+super.pop());
}
}
@Override
//Pop 2 elements from stack and subtract second from first
//Push into stack
public void sub() {
if(super.count()<2) {
throw new ArrayIndexOutOfBoundsException("Not enough elements to
pop");
}
else {
super.push(super.pop()-super.pop());
}
}
@Override
//Pop 2 elements from stack and multiply values
//Push into stack
public void mul() {
if(super.count()<2) {
throw new ArrayIndexOutOfBoundsException("Not enough elements to
pop");
}
else {
super.push(super.pop()*super.pop());
}
}
@Override
//Pop 2 elements from stack and divide second from first
values
//Push into stack
public void div() {
if(super.count()<2) {
throw new ArrayIndexOutOfBoundsException("Not enough elements to
pop");
}
else {
super.push(super.pop()/super.pop());
}
}
@Override
//peek an element and make duplicate
//Push into stack
public void dup() {
if(super.count()<1) {
throw new ArrayIndexOutOfBoundsException("Not enough elements to
pop");
}
else {
super.push(super.peek());
}
}
//Peek 2 elements from stack and make their duplicate
//Push into stack in same order
@Override
public void twoDup() {
if(super.count()<2) {
throw new ArrayIndexOutOfBoundsException("Not enough elements to
pop");
}
else {
double first=super.peek();
double second=super.peek(super.count()-2);
super.push(second);
super.push(first);
}
}
}
import java.util.Scanner;
/**
* Test implemented functions
* @author deept
*
*/
public class TestFourthStack {
public static void main(String [] args){
//Variables for input
int choice;
int pek;
double val,poped;
boolean empty;
//Keyboard read
Scanner sc =new Scanner(System.in);
FourthStack as = new FourthStack(20);
//Loop until exit
while(true){
//User choices
System.out.println("1. Enter a Value in stack");
System.out.println("2. Pop a Value");
System.out.println("3. Check If array is Empty");
System.out.println("4. Peek Function");
System.out.println("5. Clear Stack");
System.out.println("6. Add Function");
System.out.println("7. Sub Function");
System.out.println("8. Mul Function");
System.out.println("9. Div Function");
System.out.println("10. Dup Function");
System.out.println("11. TwoDup Function");
System.out.println("0. Exit\n");
choice = sc.nextInt();
//Execute each choice
switch(choice){
case 1:
System.out.print("Enter a value To push : ");
val = sc.nextDouble();
as.push(val);
break;
case 2:
poped = as.pop();
System.out.println("Popped : "+poped);
break;
case 3:
empty = as.isEmpty();
System.out.println("Empty ? "+empty);
break;
case 4:
poped = as.peek();
if(poped != -1)
System.out.println("Peeked Value : "+poped);
else
System.out.println("Oops it was not a valid index this place is
empty");
break;
case 5:
as.clear();
break;
case 6:
as.add();
break;
case 7:
as.sub();
break;
case 8:
as.mul();
break;
case 9:
as.div();
break;
case 10:
as.dup();
break;
case 11:
as.twoDup();
break;
case 0:
System.exit(0);
}
}
}
}
/*
* Pure abstract class without implementation
*/
public abstract class AbstractStack {
public abstract void push(double item);
public abstract double pop();
public abstract boolean isEmpty();
public abstract double peek();
public abstract void clear();
}
/*
* Interface to generate sum additional functions using stack
*/
public interface Fourth {
public void add();
public void sub();
public void mul();
public void div();
public void dup();
public void twoDup();
}
I PROVIDED THE FIVE PREVIOUS STACKS THAT I USED ON MY PREVIOUS WORK... LET ME KNOW IF THERE'S ANYTHING ELSE
SOLUTION:
CONSIDERING THE CONDITIONS AND REQUIREMENTS FROM THE QUESTION.
CODE:
RPN.java
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class RPN
{
private JFrame frameMain; // Main frame
private JTextField txtEntry; // Entry textbox
private JTextField txtResult; // Result display
private JTextField txtError; // Error display
private JTextField txtStack0;
private JTextField txtStack1;
private JTextField txtStack3;
private JTextField txtStack2;
private JTextField txtStack7;
private JTextField txtStack6;
private JTextField txtStack5;
private JTextField txtStack4;
private JTextField txtStack9;
private JTextField txtStack8;
public static void main(String[] args)
{
// Create the event queue
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
RPN window = new RPN();
// Show the main frame
window.frameMain.setVisible(true);
}
catch (Exception e)
{
// Exception handler for the frame
e.printStackTrace();
}
}
});
}
public RPN()
{
initialize();
}
private void initialize()
{
// Create the RPN stack that will hold at most 4 elements
final ForthStack rpnStack = new ForthStack();
rpnStack.ArrayClass(10);
frameMain = new JFrame();
frameMain.setTitle("RPN Calculator");
frameMain.setBounds(100, 100, 799, 499);
frameMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frameMain.getContentPane().setLayout(null);
// Entry textbox label
JLabel lblEntry = new JLabel("ENTRY:");
lblEntry.setFont(new Font("Tahoma", Font.BOLD, 24));
lblEntry.setBounds(15, 16, 107, 44);
frameMain.getContentPane().add(lblEntry);
// Entry textbox field
txtEntry = new JTextField();
txtEntry.setBackground(Color.WHITE);
txtEntry.setForeground(Color.BLACK);
txtEntry.setHorizontalAlignment(SwingConstants.TRAILING);
txtEntry.setFont(new Font("Lucida Console", Font.BOLD, 28));
txtEntry.setBounds(137, 13, 500, 47);
frameMain.getContentPane().add(txtEntry);
txtEntry.setColumns(10);
// Result display label
JLabel lblResult = new JLabel("RESULT:");
lblResult.setFont(new Font("Tahoma", Font.BOLD, 24));
lblResult.setBounds(15, 79, 107, 44);
frameMain.getContentPane().add(lblResult);
// Result display field
txtResult = new JTextField();
txtResult.setBackground(Color.BLACK);
txtResult.setForeground(Color.GREEN);
// Make uneditable
txtResult.setEditable(false);
txtResult.setFont(new Font("Lucida Console", Font.BOLD, 28));
txtResult.setHorizontalAlignment(SwingConstants.TRAILING);
txtResult.setBounds(137, 79, 500, 47);
frameMain.getContentPane().add(txtResult);
txtResult.setColumns(10);
// Error display label
JLabel lblError = new JLabel("ERROR:");
lblError.setFont(new Font("Tahoma", Font.BOLD, 24));
lblError.setBounds(15, 141, 107, 47);
frameMain.getContentPane().add(lblError);
// Error display field
txtError = new JTextField();
txtError.setHorizontalAlignment(SwingConstants.TRAILING);
txtError.setForeground(Color.GREEN);
txtError.setFont(new Font("Lucida Console", Font.BOLD, 28));
// Make uneditable
txtError.setEditable(false);
txtError.setColumns(10);
txtError.setBackground(Color.BLACK);
txtError.setBounds(137, 137, 500, 47);
frameMain.getContentPane().add(txtError);
// CLR button. Calls the clear method when pressed.
JButton btnClear = new JButton("CLR");
btnClear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
// Clear the stack and the displays
rpnStack.clear();
txtError.setText(null);
txtResult.setText(null);
}
});
btnClear.setFont(new Font("Tahoma", Font.BOLD, 24));
btnClear.setBounds(15, 390, 622, 47);
frameMain.getContentPane().add(btnClear);
// DUP button. Calls the dup method when pressed.
JButton btnDup = new JButton("DUP");
btnDup.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
// Perform the dupe
rpnStack.dup();
// Get the result
double peekedValue = rpnStack.peek();
// Show the result
txtResult.setText(String.valueOf(peekedValue));
}
catch (RuntimeException re)
{
txtError.setText(re.getMessage());
}
}
});
btnDup.setFont(new Font("Tahoma", Font.BOLD, 24));
btnDup.setBounds(15, 327, 300, 47);
frameMain.getContentPane().add(btnDup);
// 2DUP button. Calls the twoDup method when pressed.
JButton btn2Dup = new JButton("2DUP");
btn2Dup.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
// Perform the dupe
rpnStack.twoDup();
// Get the result
double peekedValue = rpnStack.peek();
// Show the result
txtResult.setText(String.valueOf(peekedValue));
}
catch (RuntimeException re)
{
txtError.setText(re.getMessage());
}
}
});
btn2Dup.setFont(new Font("Tahoma", Font.BOLD, 24));
btn2Dup.setBounds(337, 327, 300, 47);
frameMain.getContentPane().add(btn2Dup);
// + button. Calls the add method when pressed.
JButton btnAdd = new JButton("+");
btnAdd.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
// Perform the add
rpnStack.add();
// Get the result
double peekedValue = rpnStack.peek();
// Show the result
txtResult.setText(String.valueOf(peekedValue));
}
catch (RuntimeException re)
{
txtError.setText(re.getMessage());
}
}
});
btnAdd.setFont(new Font("Tahoma", Font.BOLD, 24));
btnAdd.setBounds(15, 204, 135, 44);
frameMain.getContentPane().add(btnAdd);
// PUSH button. Calls the push method when pressed.
JButton btnPush = new JButton("PUSH");
btnPush.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
// Get the value in the entry box
String entryValue = txtEntry.getText();
try
{
// Convert the value to a double
double value = Double.parseDouble(entryValue);
// Push the value to the stack
rpnStack.push(value);
// Clear the error box and the entry box
txtError.setText(null);
txtEntry.setText(null);
// Set the display to the top of the stack
double peekedValue = rpnStack.peek();
txtResult.setText(String.valueOf(peekedValue));
// for(int i=0; i < rpnStack.length; i++) {
//ltxtStack0[i+1] = stack[i];
//}
catch (NumberFormatException nfe)
{
txtError.setText("INVALID ENTRY!");
}
catch (RuntimeException re)
{
txtError.setText(re.getMessage());
}
}
});
btnPush.setFont(new Font("Tahoma", Font.BOLD, 24));
btnPush.setBounds(15, 264, 300, 47);
frameMain.getContentPane().add(btnPush);
// POP button. Calls the pop method when pressed.
JButton btnPop = new JButton("POP");
btnPop.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
try
{
// Pop the stack
rpnStack.pop();
// Clear the error box
txtError.setText(null);
// Set the display to the top of the stack
double peekedValue = rpnStack.peek();
txtResult.setText(String.valueOf(peekedValue));
}
catch (RuntimeException re)
{
// Clear the displays
txtError.setText(null);
txtResult.setText(null);
}
}
});
btnPop.setFont(new Font("Tahoma", Font.BOLD, 24));
btnPop.setBounds(337, 264, 300, 47);
frameMain.getContentPane().add(btnPop);
// - button. Calls the sub method when pressed.
JButton btnSub = new JButton("-");
btnSub.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
// Perform the subtraction
rpnStack.sub();
// Get the result
double peekedValue = rpnStack.peek();
// Show the result
txtResult.setText(String.valueOf(peekedValue));
}
catch (RuntimeException re)
{
txtError.setText(re.getMessage());
}
}
});
btnSub.setFont(new Font("Tahoma", Font.BOLD, 24));
btnSub.setBounds(180, 204, 135, 44);
frameMain.getContentPane().add(btnSub);
// * button. Calls the mult method when pressed.
JButton btnMult = new JButton("*");
btnMult.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
// Perform the multiplication
rpnStack.mult();
// Get the result
double peekedValue = rpnStack.peek();
// Show the result
txtResult.setText(String.valueOf(peekedValue));
}
catch (RuntimeException re)
{
txtError.setText(re.getMessage());
}
}
});
btnMult.setFont(new Font("Tahoma", Font.BOLD, 24));
btnMult.setBounds(337, 204, 135, 44);
frameMain.getContentPane().add(btnMult);
// - button. Calls the div method when pressed.
JButton btnDiv = new JButton("/");
btnDiv.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
// Perform the division
rpnStack.div();
// Get the result
double peekedValue = rpnStack.peek();
// Show the result
txtResult.setText(String.valueOf(peekedValue));
}
catch (RuntimeException re)
{
txtError.setText(re.getMessage());
}
}
});
btnDiv.setFont(new Font("Tahoma", Font.BOLD, 24));
btnDiv.setBounds(502, 205, 135, 44);
frameMain.getContentPane().add(btnDiv);
JLabel lblTheStack = new JLabel("The Stack");
lblTheStack.setFont(new Font("Tahoma", Font.BOLD, 14));
lblTheStack.setBounds(683, 79, 73, 14);
frameMain.getContentPane().add(lblTheStack);
txtStack0 = new JTextField();
txtStack0.setEditable(false);
txtStack0.setBounds(683, 102, 63, 20);
frameMain.getContentPane().add(txtStack0);
txtStack0.setColumns(10);
//txtStack0.setText(""+ stack0 +"");
txtStack1 = new JTextField();
txtStack1.setEditable(false);
txtStack1.setColumns(10);
txtStack1.setBounds(683, 128, 63, 20);
frameMain.getContentPane().add(txtStack1);
txtStack3 = new JTextField();
txtStack3.setEditable(false);
txtStack3.setColumns(10);
txtStack3.setBounds(683, 180, 63, 20);
frameMain.getContentPane().add(txtStack3);
txtStack2 = new JTextField();
txtStack2.setEditable(false);
txtStack2.setColumns(10);
txtStack2.setBounds(683, 154, 63, 20);
frameMain.getContentPane().add(txtStack2);
txtStack7 = new JTextField();
txtStack7.setEditable(false);
txtStack7.setColumns(10);
txtStack7.setBounds(683, 286, 63, 20);
frameMain.getContentPane().add(txtStack7);
txtStack6 = new JTextField();
txtStack6.setEditable(false);
txtStack6.setColumns(10);
txtStack6.setBounds(683, 260, 63, 20);
frameMain.getContentPane().add(txtStack6);
txtStack5 = new JTextField();
txtStack5.setEditable(false);
txtStack5.setColumns(10);
txtStack5.setBounds(683, 234, 63, 20);
frameMain.getContentPane().add(txtStack5);
txtStack4 = new JTextField();
txtStack4.setEditable(false);
txtStack4.setColumns(10);
txtStack4.setBounds(683, 208, 63, 20);
frameMain.getContentPane().add(txtStack4);
txtStack9 = new JTextField();
txtStack9.setEditable(false);
txtStack9.setColumns(10);
txtStack9.setBounds(683, 340, 63, 20);
frameMain.getContentPane().add(txtStack9);
txtStack8 = new JTextField();
txtStack8.setEditable(false);
txtStack8.setColumns(10);
txtStack8.setBounds(683, 314, 63, 20);
frameMain.getContentPane().add(txtStack8);
}
}
ForthStack.java
public class ForthStack extends ArrayStack implements Forth
{
public void add()
{
int count = this.count();
if (count < 2)
{
// Throw an exception if there are less than two items.
throw new RuntimeException("LESS THAN 2 ITEMS!");
}
else
{
// Pop the first two elements off of the stack
double secondValue = this.pop();
double firstValue = this.pop();
// Add the two values together
double returnValue = firstValue + secondValue;
// Take negative sign off of zero result
if (returnValue == -0.0d)
{
returnValue = 0.0d;
}
// Push the result back onto the stack
this.push(returnValue);
}
}
public void sub()
{
// Check to see if there are at least two items on the stack
int count = this.count();
if (count < 2)
{
// Throw an exception if there are less than two items.
throw new RuntimeException("LESS THAN TWO ITEMS!");
}
else
{
// Pop the first two elements off of the stack
double secondValue = this.pop();
double firstValue = this.pop();
// Subtract the second value from the first value
double returnValue = firstValue - secondValue;
// Take negative sign off of zero result
if (returnValue == -0.0d)
{
returnValue = 0.0d;
}
// Push the result back onto the stack
this.push(returnValue);
}
}
public void mult()
{
// Check to see if there are at least two items on the stack
int count = this.count();
if (count < 2)
{
// Throw an exception if there are less than two items.
throw new RuntimeException("LESS THAN 2 ITEMS!");
}
else
{
// Pop the first two elements off of the stack
double secondValue = this.pop();
double firstValue = this.pop();
// Multiply the two values
double returnValue = firstValue * secondValue;
// Take negative sign off of zero result
if (returnValue == -0.0d)
{
returnValue = 0.0d;
}
// Push the result back onto the stack
this.push(returnValue);
}
}
public void div()
{
// Check to see if there are at least two items on the stack
int count = this.count();
if (count < 2)
{
// Throw an exception if there are less than two items.
throw new RuntimeException("LESS THAN 2 ITEMS!");
}
else
{
// Pop the first two elements off of the stack
double secondValue = this.pop();
double firstValue = this.pop();
// Multiply the two values
double returnValue = firstValue / secondValue;
// Take negative sign off of zero result
if (returnValue == -0.0d)
{
returnValue = 0.0d;
}
// Push the result back onto the stack
this.push(returnValue);
}
}
public void dup()
{
// Get the size of the array
int size = this.getSize();
// See if the stack is empty
if (this.isEmpty())
{
throw new RuntimeException("STACK EMPTY!");
}
// See if the stack is full
else if (this.count() == size)
{
throw new RuntimeException("OVERFLOW!");
}
else
{
// Perform the dup by peeking then pushing
double value = this.peek();
this.push(value);
}
}
public void twoDup()
{
// Get the size of the array
int size = this.getSize();
// Get the number of elements in the array
int currentCount = this.count();
// See if the stack is empty
if (currentCount < 2)
{
// Less than two items on the stack, so throw an exception
throw new RuntimeException("LESS THAN 2 ITEMS!");
}
// See if the stack can hold two items
else if ((size - currentCount) < 2)
{
throw new RuntimeException("OVERFLOW!");
}
else
{
// Perform the dup by peeking twice, then pushing twice
double firstValue = this.peek();
double secondValue = this.peek(1);
this.push(secondValue);
this.push(firstValue);
}
}
}
ArrayStack.java
public class ArrayStack extends AbstractStack
{
private int size; // Maximum size of the stack
private int top; // Link to the top of the stack
private double[] stack; // The stack itself
public void ArrayClass(int size)
{
// Check for a zero or a negative integer size
if (size <= 0)
{
throw new RuntimeException("INVALID SIZE!");
}
// Check for a non-integer size
else if ((int)size != size)
{
throw new RuntimeException("INVALID SIZE!");
}
// Size is a positive integer
else
{
this.size = size;
// Create the stack
stack = new double[size];
// Top of the stack
this.top = 0;
}
}
public void ArrayClass()
{
this.size = 3;
// Create the stack
stack = new double[size];
// Top of the stack
this.top = 0;
}
public void push(double item)
{
// Check to see if the bounds have been exceeded
if(top < size)
{
// Assign the value
stack[top] = item;
// Move the top pointer
top++;
}
else
{
// Stack exceeds bounds. Print error.
throw new RuntimeException("OVERFLOW!");
}
}
public boolean isEmpty()
{
// Return true if the top pointer is 0
if (top == 0)
{
return true;
}
// Return false otherwise
else
{
return false;
}
}
public double pop(){
// Cannot pop if the stack is empty
if (this.isEmpty())
{
// Throw exception if the stack is empty
throw new RuntimeException("STACK EMPTY!");
}
else
{
// Get the value at the top of the stack
double poppedValue = stack[top-1];
// Clear the value
stack[top-1] = 0.0d;
// Move the pointer
top--;
return poppedValue;
}
}
public double peek(int n)
{
if (n >= this.count())
{
// The element requested doesn't exist
throw new RuntimeException("DOES NOT EXIST!");
}
else if (this.isEmpty())
{
// Stack is empty
throw new RuntimeException("STACK EMPTY!");
}
else if (n < 0)
{
throw new RuntimeException("INVALID ENTRY!");
}
else if (n == 0)
{
// Return the value of the element at the top
return stack[top-1];
}
else
{
return stack[top-1-n];
}
}
public double peek()
{
if (this.isEmpty())
{
// Stack is empty
throw new RuntimeException("STACK EMPTY!");
}
else
{
// Return the top stack element
return stack[top-1];
}
}
public int count()
{
// Count is simply the top pointer
return top;
}
public void clear()
{
// As long as the stack is not empty, pop all the elements
if (!this.isEmpty())
{
for (int i = 0; i < this.count(); i++)
{
stack[i] = 0.0d;
}
this.top = 0;
}
}
public int getSize()
{
return size;
}
}
AbstractStack.java
public abstract class AbstractStack
{
public abstract void push(double item);
public abstract double pop();
public abstract boolean isEmpty();
public abstract double peek();
public abstract void clear();
}
Forth.java
public interface Forth
{
public void add();
public void sub();
public void mult();
public void div();
public void dup();
public void twoDup();
}
SAMPLE OUTPUT:
NOTE: PLEASE UPVOTE ITS VERY
MUCH NECESSARY FOR ME A LOT. PLZZ....