Question: I need some help on a homework problem. This is what I am given: A hexadecimal digit is '0'..'9', or 'a'..'f', or 'A'..'F'. A hexadecimal
I need some help on a homework problem. This is what I am given:
A hexadecimal digit is '0'..'9', or 'a'..'f', or 'A'..'F'. A hexadecimal constant is a sequence of hexadecimal digits. Examples include 3, a, 0d, and FF4e. Use the direct-code technique for implementing an FSM to parse a hexadecimal constant and convert it to a nonnegative integer. The input/output should be similar to that in the figure, with invalid input producing an error message and a valid hexadecimal input string producing the nonnegative integer value.
So, if the user enters 0d the output to the console should be Number = 13 (dec)
To guarantee that the hexadecimal value does not occupy more than two bytes, check that the decimal value is not more than 65535. Note that 00000A is a valid hexadecimal constant even though it is longer than four characters. You are not allowed to use the Java method parseInt() as the purpose of the program is to implement a parser yourself. The code i have so far has the interface and what i need help on is the action, here is the code
package prob0719;
import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class Prob0719Main implements ActionListener {
JFrame mainWindowFrame; JPanel inputPanel; JLabel label; JTextField textField; JPanel buttonPanel; JButton button;
public Prob0719Main() { // Set up the main window. mainWindowFrame = new JFrame("Problem 7.19"); mainWindowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainWindowFrame.setSize(new Dimension(240, 120));
// Lay out the label and text field input panel from top to bottom. inputPanel = new JPanel(); inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.PAGE_AXIS)); label = new JLabel("Enter a number:"); inputPanel.add(label); textField = new JTextField(20); inputPanel.add(textField); inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
// Lay out the button from left to right. buttonPanel = new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS)); buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10)); buttonPanel.add(Box.createHorizontalGlue()); button = new JButton("Parse"); buttonPanel.add(button); buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
// Combine the input panel and the button panel in the main window. mainWindowFrame.add(inputPanel, BorderLayout.CENTER); mainWindowFrame.add(buttonPanel, BorderLayout.PAGE_END);
textField.addActionListener(this); button.addActionListener(this);
mainWindowFrame.pack(); mainWindowFrame.setVisible(true); }
private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); Prob0719Main mainWindow = new Prob0719Main(); }
public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { @Override public void run() { createAndShowGUI(); } }); }
@Override public void actionPerformed(ActionEvent event) { String line = textField.getText(); System.out.printf("help "); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
