Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

FormattedInteger.java public class FormattedInteger { public static final int HEX = 0; public static final int PLAIN = 1; public static final int OCTAL =

image text in transcribed

FormattedInteger.java public class FormattedInteger { public static final int HEX = 0; public static final int PLAIN = 1; public static final int OCTAL = 2; private int value; private int format; public FormattedInteger(int f) { format = f; } public int getInt() { return value; } public String getString() { String result = null; if (format == HEX) result = "0x" + Integer.toHexString(value); else if (format == PLAIN) result = Integer.toString(value); else if (format == OCTAL) result = "0" + Integer.toOctalString(value); return result; } public void setInt(int v) { value = v; } public String setString(String s) { if (format == HEX) { if (!s.startsWith("0x")) return "Hex strings must start with \"0x\"."; int i = 2; while (i  '9') return "First char must be a decimal digit" + " or a minus sign."; int i = 0; while (i  

FormattedIntegerDemo.java

import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JApplet; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; /** * This is an applet for demonstrating the FormattedInteger class. We present * a graphical interface that allows the user to enter an integer in one of three * formats: signed decimal, hex, and octal. Whichever format the user enters, * the number is displayed in all three formats. This can be run either as an * applet or as an application. * @author Adam Webber */ public class FormattedIntegerDemo extends JApplet { private static final long serialVersionUID = 0L; /** * Our decimal formatted integers. */ private FormattedInteger decimal = new FormattedInteger(FormattedInteger.PLAIN); /** * Our hex formatted integers. */ private FormattedInteger hex = new FormattedInteger(FormattedInteger.HEX); /** * Our octal formatted integers. */ private FormattedInteger octal = new FormattedInteger(FormattedInteger.OCTAL); /** * A label that reports the last error string. * This is kept in sync with lastError by the update method. * @see update */ private JLabel lastErrorLabel; /** * The text field in which new integers are entered in decimal. */ private JTextField decimalField; /** * The text field in which new integers are entered in hex. */ private JTextField hexField; /** * The text field in which new integers are entered in octal. */ private JTextField octalField; /** * Create our GUI components. */ @Override public void init() { // Boilerplate: create components on event-dispatching thread. try { javax.swing.SwingUtilities.invokeAndWait(new Runnable() { public void run() { createGUI(); } }); } catch (Exception e) { System.err.println("createGUI didn't successfully complete"); } } /** * Create the GUI components. This initializes the applet. */ public void createGUI() { // First, create the Labels, and the TextFields where the user will // enter formatted integers. JLabel decimalLabel = new JLabel("Signed decimal integer: ", JLabel.RIGHT); decimalField = new JTextField(20); decimalField.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent evt) { update(decimal,decimalField.getText()); } }); JLabel hexLabel = new JLabel("Hex integer (with leading 0x): ", JLabel.RIGHT); hexField = new JTextField(20); hexField.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent evt) { update(hex,hexField.getText()); } }); JLabel octalLabel = new JLabel("Octal integer (with leading 0): ", JLabel.RIGHT); octalField = new JTextField(20); octalField.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent evt) { update(octal,octalField.getText()); } }); lastErrorLabel = new JLabel("Hello!", JLabel.CENTER); lastErrorLabel.setForeground(Color.RED); // Create an inner Panel for the Label/TextField lines. JPanel top = new JPanel(new GridLayout(3,0)); top.add(decimalLabel); top.add(decimalField); top.add(hexLabel); top.add(hexField); top.add(octalLabel); top.add(octalField); // Finally, our content Panel: we will use a BorderLayout with the Label/TextField // lines in the north border, and the error Label in the south. Container content = getContentPane(); content.add(top, BorderLayout.NORTH); content.add(lastErrorLabel, BorderLayout.SOUTH); // Get a consistent initial state for the labels and text fields. update(decimal,"0"); lastErrorLabel.setText("Enter a number in any field"); // Initial greeting } /** * A main method, so we can be run as an application, as well as a JApplet. */ public static void main(String[] args) { final JApplet applet = new FormattedIntegerDemo(); applet.init(); final JFrame frame = new JFrame("Formatted Integer Tester"); frame.setContentPane(applet.getContentPane()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } /** * Update our display. We are given one of the FormattedIntegers, and a * String the users wants entered into it. We try to enter the user's * String. If there is an error, this will produce an error message, and * no change to the number stored in the FormattedInteger. We then update * all the FormattedIntegers to the same number, and update our display of * the text for each, and our display of the error text. * @param form the FormattedInteger to enter a string into * @param number the String to enter */ private void update(FormattedInteger form, String number) { // Try to set the formatted integer to the given string. We get an error // string back, which will be the empty string if there is no error. We // display the string. String lastError = form.setString(number); lastErrorLabel.setText(lastError); // Update all the formatted integers to the same integer value now stored // in form. (This will not have changed if there was an error.) Update // our display to match the formatted strings. int val = form.getInt(); decimal.setInt(val); decimalField.setText(decimal.getString()); hex.setInt(val); hexField.setText(hex.getString()); octal.setInt(val); octalField.setText(octal.getString()); } } 
Exercises 315 Although Formatted Integer works, it is ugly. It is not written in a fully object-oriented style. It has no comments. Your job is to replace it with a more beau- tiful implementation of the same thing, using three separate classes three for the different behaviors of FormattedInteger. Your implementation must use a fully object-oriented style (eliminating the enumerations "factor out" redundant code and variables into superclasses, as necessary; provide polymorphism, using a new Formatted Integer as a common superclass or interface for the three classes; be generally beautiful, neat, and well commented; and still work Extensive testing is not necessary, but you might be interested in seeing the code run with a graphical user interface. The class FormattedIntegerDemo, available on this book's Web site, can be run as an application or as an applet. In its current form it runs using the original FormattedInteger cl You should be able to make it run with your classes by changing only the initializers on these three decla rations: private Formatted Integer decimal new Format tedInteger (FormattedInteger. PLAIN) i private Formatted Integer hex new Formatted Integer (FormattedInteger.HEx) private Formatted Integer octal i new Formatted Integer lo make this work you must still use the name FormattedInteger, as a super class or an interface, so that it can still serve as a common type for the three objects and have the declare types R Exercises 315 Although Formatted Integer works, it is ugly. It is not written in a fully object-oriented style. It has no comments. Your job is to replace it with a more beau- tiful implementation of the same thing, using three separate classes three for the different behaviors of FormattedInteger. Your implementation must use a fully object-oriented style (eliminating the enumerations "factor out" redundant code and variables into superclasses, as necessary; provide polymorphism, using a new Formatted Integer as a common superclass or interface for the three classes; be generally beautiful, neat, and well commented; and still work Extensive testing is not necessary, but you might be interested in seeing the code run with a graphical user interface. The class FormattedIntegerDemo, available on this book's Web site, can be run as an application or as an applet. In its current form it runs using the original FormattedInteger cl You should be able to make it run with your classes by changing only the initializers on these three decla rations: private Formatted Integer decimal new Format tedInteger (FormattedInteger. PLAIN) i private Formatted Integer hex new Formatted Integer (FormattedInteger.HEx) private Formatted Integer octal i new Formatted Integer lo make this work you must still use the name FormattedInteger, as a super class or an interface, so that it can still serve as a common type for the three objects and have the declare types R

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

More Books

Students also viewed these Databases questions

Question

Consider this article:...

Answered: 1 week ago

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago