Question
1. Download the following code Age.java Process.java GUI.java GUIListener.java 2. In Eclipse, create a new Java project called Lab 6 , and import the above
1. Download the following code
Age.java
Process.java
GUI.java
GUIListener.java
2. In Eclipse, create a new Java project called "Lab 6 ", and import the above 4 files into a default package.
3. Compile and run the program. Make sure it works.
4. Design and implement a class called SmallInt, which is specified as an ADT: A small integer is defined as an integer k where:
Integer.MIN_VALUE k's value Integer.MAX_VALUE
5. The following operations can be applied on a small integer:
int setValue(String s):
A method that sets the value of a SmallInt object to an integer that is transformed from a string. If the string does not have the appropriate numeric format, or if the integer is smaller than Integer.MIN_VALUE or larger than Integer.MAX_VALUE, the value of the SmallInt object is set to "0".
If the input is invalid, the return value is integer "-1"; otherwise, the return value is integer "0".
int getValue():
A method that returns the value of a SmallInt object.
int add(SmallInt sInt):
A method that returns the sum of the value of the sInt object and the value of this SmallInt object that invokes the method add. If the sum is smaller than Integer.MIN_VALUE or larger than Integer.MAX_VALUE, a value of "0" is returned.
6. Make similar methods for subtract, multiply, and divide as well.
7. Replace the "Age.java" with a new file called "Calculator.java", which prompts a user to input two small integers in the input line and then display the sum of the two small integers in the output text area. Specifically, when the program starts, there will be a prompt in the output area of the window asking you to input the first integer. After you input the first integer to the input line and press the "Enter" key, another prompt will tell you to input the second integer. Once you input the second integer and press the "Enter" key, the result should be displayed in the output text area as follows (you do not have to make the integers right-justified and aligned):
num1 + num2 num3
8. Repeat again for the subtract, multiply, and divide methods as well (it should print out all three results).
9. Now, the input line should be set blank and disabled, and the only action available to the user is to close the GUI window.
10. Submit your completed program (Calculator.java and SmallInt.java)
public class Age implements Process {
protected final String PROMPT = "In the Input line, please enter " + "an age (the sentinel is "; protected final int SENTINEL = -1; protected int highestAge; protected GUI gui;
// Postcondition: This Age has been initialized. public Age() { // default constructor gui = new GUI (this); gui.print(PROMPT + SENTINEL + "): "); }
// Postcondition: The input string s has been processed. public void processInput(String s) { final String HIGHEST_MESSAGE = " The highest age is "; final String CLOSE_WINDOW_PROMPT = " The execution of this project has " + "been completed. Please close this window when you are ready.";
gui.println(s); int age = Integer.parseInt(s); if (age != SENTINEL) { // not the sentinel if (age > highestAge) highestAge = age; gui.print(PROMPT + SENTINEL + "): ");
} else { // sentinel reached gui.println(HIGHEST_MESSAGE + highestAge + CLOSE_WINDOW_PROMPT); gui.freeze( ); } }
public static void main(String argv[]) { Age age = new Age(); }
}
import javax.swing.*;
public class GUI extends JFrame { protected JTextArea outputArea; protected JTextField inputField;
// constructor public GUI(Process process) {
setSize(600, 400); JPanel panel = new JPanel(); getContentPane().add(panel);
JLabel inputLabel = new JLabel("Input: "); panel.add(inputLabel);
inputField = new JTextField(47); panel.add(inputField); inputField.setEditable(true);
JLabel outputLabel = new JLabel("Output: "); panel.add(outputLabel);
outputArea = new JTextArea("", 18, 47); outputArea.setEditable(false); JScrollPane scrollPane = new JScrollPane(outputArea); panel.add(scrollPane);
setVisible(true);
addWindowListener(new GUIListener()); inputField.addActionListener(new GUIListener (inputField, process)); }
// Postcondition: the output area is now blank. public void clear() { outputArea.setText(""); }
// Postcondition: s has been converted to a string and output. public void print(Object s) { outputArea.append(s.toString()); inputField.requestFocus(); }
// Postcondition: s has been converted to a string and output, and // the next line has been advanced to. public void println(Object s) { print(s + " "); }
public void print(long i) { print(new Long (i)); }
public void println(long i) { print(i + " "); }
public void print(double x) { print(new Double (x)); }
public void println(double x) { print(x + " "); }
public void print(char c) { print(new Character (c)); }
public void println(char c) { print(c + " "); }
public void print(boolean b) { print(new Boolean(b)); }
public void println(boolean b) { print(b + " "); }
// Postcondition: the Input Line in this GUI window can no // longer be written to. public void freeze() { inputField.setEditable(false); } }
import java.awt.event.*; import javax.swing.*;
public class GUIListener extends WindowAdapter implements ActionListener { protected JTextField inputField; protected Process process;
// Postcondition: This GUI listener has been initialized. public GUIListener(){}
// Postcondition: This GUI listener has been initialized according to // the values of tf and p. public GUIListener(JTextField tf, Process p) { // constructor inputField = tf; process = p; }
// Postcondition: The window has closed. public void windowClosing(WindowEvent e) { System.exit(0); }
// Postcondition: The input has been processed. public void actionPerformed(ActionEvent e) { inputField.setText(""); inputField.setEnabled(false); process.processInput(e.getActionCommand()); inputField.setEnabled(true); } }
public interface Process {
void processInput (String s); }
Step 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