Problem (Fill in the missing code in the designated areas) in the two files loca
ID: 3736988 • Letter: P
Question
Problem (Fill in the missing code in the designated areas) in the two files located below: Problem1.java and GUI.java
Take screenshot of compiled output once complete. (which should like the sample one provided)
Build a Java GUI application that implements a very simple, stack-oriented calculator for two’s 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 calculator’s “window” displays the calculator’s 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. You must use exception handling to deal with user errors.
//--------------------------------------------------------
// 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 << 1 ) | 0;
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 << 1 ) | 1;
}
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();
}
}
}
Sample Graphical User Interface (GUD El Chapter #14, Problem #1 CLR Push Pop AND XORMOT ORExplanation / Answer
Program:
import javax.swing.*;
import java.awt.*;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.