Question
Problem - Java Programming Build a Java GUI application that implements a very simple, stack-oriented calculator for twos complement, 16-bit binary integers . The JPanel
Problem - Java Programming
Build a Java GUI application that implements a very simple, stack-oriented calculator for twos complement, 16-bit binary integers. The JPanel gui is composed of a text box JTextField TOSDisplay and eleven command buttons: JButton bit0, JButton bit1, JButton function0,, JButton function11.The calculators window displays the calculators stack contents; specifically, from left-to-right, the one-line window display contains
-the top-of-stack element (TOS) that is the only visible integer stored on the stack;
-followed by one '*' for each integer on the stack underneath TOS (the '*'s indicate the depth of the invisible or unseen part of the stack); and
-ended by "!" only when an error occurred as a result of the last command button press. Note The error is automatically cleared by the next command button press unless, of course, the command button press causes yet another error to occur.
The application always begins with TOS = 0000000000000000. The calculator always displays a value for TOS because TOS may not be popped-off the stack. As a result, the stack is never empty; that is, it always contains at least the one integer TOS
Take screenshot of it compiled which should try to look like the sample one provided.
//---------------------------------------------------------
// Chapter #14, Problem #1
// Problem1.java
//---------------------------------------------------------
import javax.swing.*;
public class Problem1
{
public static final int DEPTH = 58;
public static int WIDTH = 16;
public static void main(String[] args)
{
GUI gui = new GUI(WIDTH,DEPTH);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(800,600);
gui.setVisible(true);
}
}
//---------------------------------------------------------
// Class GUI for Chapter #14, Problem #1
// GUI.java
//---------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//---------------------------------------------------------
public class GUI extends JFrame
//---------------------------------------------------------
{
private int stack[];
private int size;
private int TOS;
private boolean error;
private int WIDTH;
private int DEPTH;
private JTextField TOSDisplay;
private JButton bit0;
private JButton bit1;
private JButton function0;
Student provides missing code to define references to the remaining
calculator GUI components function1,function2,...,function11
private JButton function12;
private Font font = new Font("Courier",Font.PLAIN,12);
//----------------------------------------------------
public GUI(int WIDTH,int DEPTH)
//----------------------------------------------------
{
super("Chapter #14, Problem #1");
setLayout( null );
TOSDisplay = new JTextField();
TOSDisplay.setBounds(20,20,300,40);
TOSDisplay.setFont(font);
TOSDisplay.setEditable(false);
add(TOSDisplay);
bit0 = new JButton("0");
bit0.setBounds(20,100,80,40);
bit0.setFont(font);
add(bit0);
bit1 = new JButton("1");
bit1.setBounds(100,100,80,40);
bit1.setFont(font);
add(bit1);
function0 = new JButton("CLR");
function0.setBounds(340,20,80,40);
function0.setFont(font);
add(function0);
Student provides missing code to place the remaining calculator GUI components
function12 = new JButton("Pop");
function12.setBounds(340,100,80,40);
function12.setFont(font);
add(function12);
//----------------------------------------------------
// Treat JButton bit0 events specially!
//----------------------------------------------------
bit0.addActionListener(new Bit0ButtonHandler());
//----------------------------------------------------
// Treat remaining JButton events as a group.
//----------------------------------------------------
ButtonHandler handler = new ButtonHandler();
bit1.addActionListener(handler);
function0.addActionListener(handler);
Student provides missing code to establish the event handler for the remaining calculator GUI components
function12.addActionListener(handler);
this.WIDTH = WIDTH;
this.DEPTH = DEPTH;
stack = new int [ DEPTH ];
size = 0;
TOS = 0; stack[size++] = TOS;
error = false;
UpdateTOSDisplay();
}
//----------------------------------------------------
private void UpdateTOSDisplay()
//----------------------------------------------------
{
final int MASK = 0X01;
String TOSString = "";
Student provides missing code to build the contents of the calculator
JTextField. The format of the String TOSString is
XX...X **...* !
where
XX...X is the WIDTH binary digits used to represent the value of TOS
**...* is the (size-1) *s used to represent the integers in
the ?hidden? part of the stack
! is displayed only after an error has occurred
TOSDisplay.setText(TOSString);
}
//----------------------------------------------------
private class Bit0ButtonHandler implements ActionListener
//----------------------------------------------------
{
//----------------------------------------------------
@Override
public void actionPerformed(ActionEvent event)
//----------------------------------------------------
{
error = false;
TOS = stack[size-1] = (TOS
UpdateTOSDisplay();
}
}
//----------------------------------------------------
private class ButtonHandler implements ActionListener
//----------------------------------------------------
{
//----------------------------------------------------
@Override
public void actionPerformed(ActionEvent event)
//----------------------------------------------------
{
error = false;
try
{
// Remember, bit0 JButton event handled by Bit0ButtonHandler class
if ( event.getSource() == bit1 )
{
TOS = stack[size-1] = (TOS
}
else if ( event.getSource() == function0 ) // CLR
{
TOS = stack[size-1] = 0;
}
Student provides missing code to handle the events associated with the
remaining calculator function buttons function1,function2,...,function11
else if ( event.getSource() == function12 ) // Pop
{
if ( size == 1 )
throw new Exception("Stack underflow");
else
TOS = stack[--size-1];
}
else // SHOULD NOT HAPPEN!!!
{
throw new Exception("Unknown user-interface action");
}
}
catch (Exception exception)
{
error = true;
}
UpdateTOSDisplay();
}
}
}
Lil Chapter #14, Problem #1 CLR Push Pop OR AND Lil Chapter #14, Problem #1 CLR Push Pop OR ANDStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started