I need help with my getDisplayValue() method. I am trying to iterate though an Array to display a value in a calculator, but I doesn't work. I keep on getting these weird magical numbers at the end of the iteration. Note that this is done in BlueJ. Thank you for all your help in advance.
Calculator -> UserInterface -> CalcEngine and Calculator -> CalcEngine.
Next class:public class CalcEngine { //Instance variables used. //These are all the instance variables I used to implement //a complete calculator solution. private int finalNumber; //Holds results of calculations to date. private int[] digitHold; //holds numbers pressed until they are operated on private int indexed; //holds index of last number added to digitHold array int currentOp; //indicates operation waiting to be calculated(0=none, 1=plus, 2=minus) /** * Create a CalcEngine instance. Initialise its state so that it is ready * for use. */ public CalcEngine() { int finalNumber = 0; int indexed = 0; int currentOp = 0; digitHold = new int[10]; } /** * A number button was pressed. Do whatever you have to do to handle it. * The number value of the button is given as a parameter. */ public void numberPressed(int number) { digitHold[indexed] = number; indexed++; } /** * Return the value that should currently be displayed on the calculator * display. THIS ONE!!:confused::confused: */ public int getDisplayValue() { int placeValue = 1; for(int index = digitHold.length-1; index >= 0; index--) { finalNumber += digitHold[index] * placeValue; placeValue = placeValue * 10; } return finalNumber/10; //for compilation purposes only } /** * The 'plus' button was pressed. */ public void plus() { } /** * The 'minus' button was pressed. */ public void minus() { // doesn"t work sequentially. } /** * The '=' button was pressed. */ public void equals() { } /** * The 'C' (clear) button was pressed. */ public void clear() { finalNumber = 0; indexed = 0; currentOp = 0; for(int index = 0; index < 10; index++) { digitHold[index] = 0; } getDisplayValue(); } /** * Return the title of this calculation engine. */ public String getTitle() { return "Basic Calculator"; } /** * Return the author of this engine. This string is displayed as it is, * so it should say something like "Written by H. Simpson". */ public String getAuthor() { return "Written by "; } /** * Return the version number of this engine. This string is displayed as * it is, so it should say something like "Version 1.1". */ public String getVersion() { return "Version 1.1"; } }
Next class:import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class UserInterface implements ActionListener { private CalcEngine calc; private boolean showingAuthor; private JFrame frame; private JTextField display; private JLabel status; /** * Create a user interface for a given calcEngine. */ public UserInterface(CalcEngine engine) { calc = engine; showingAuthor = true; makeFrame(); frame.setVisible(true); } /** * Make this interface visible again. (Has no effect if it is already * visible.) */ public void setVisible(boolean visible) { frame.setVisible(visible); } /** * Make the frame for the user interface. */ private void makeFrame() { frame = new JFrame(calc.getTitle()); JPanel contentPane = (JPanel)frame.getContentPane(); contentPane.setLayout(new BorderLayout(8, 8)); contentPane.setBorder(new EmptyBorder( 10, 10, 10, 10)); display = new JTextField(); contentPane.add(display, BorderLayout.NORTH); JPanel buttonPanel = new JPanel(new GridLayout(4, 4)); addButton(buttonPanel, "7"); addButton(buttonPanel, "8"); addButton(buttonPanel, "9"); addButton(buttonPanel, "C"); addButton(buttonPanel, "4"); addButton(buttonPanel, "5"); addButton(buttonPanel, "6"); addButton(buttonPanel, "?"); addButton(buttonPanel, "1"); addButton(buttonPanel, "2"); addButton(buttonPanel, "3"); buttonPanel.add(new JLabel(" ")); addButton(buttonPanel, "0"); addButton(buttonPanel, "+"); addButton(buttonPanel, "-"); addButton(buttonPanel, "="); contentPane.add(buttonPanel, BorderLayout.CENTER); status = new JLabel(calc.getAuthor()); contentPane.add(status, BorderLayout.SOUTH); frame.pack(); } /** * Add a button to the button panel. */ private void addButton(Container panel, String buttonText) { JButton button = new JButton(buttonText); button.addActionListener(this); panel.add(button); } /** * An interface action has been performed. Find out what it was and * handle it. */ public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); if(command.equals("0") || command.equals("1") || command.equals("2") || command.equals("3") || command.equals("4") || command.equals("5") || command.equals("6") || command.equals("7") || command.equals("8") || command.equals("9")) { int number = Integer.parseInt(command); calc.numberPressed(number); } else if(command.equals("+")) calc.plus(); else if(command.equals("-")) calc.minus(); else if(command.equals("=")) calc.equals(); else if(command.equals("C")) calc.clear(); else if(command.equals("?")) showInfo(); redisplay(); } /** * Update the interface display to show the current value of the * calculator. */ private void redisplay() { display.setText("" + calc.getDisplayValue()); } /** * Toggle the info display in the calculator's status area between the * author and version information. */ private void showInfo() { if(showingAuthor) status.setText(calc.getVersion()); else status.setText(calc.getAuthor()); showingAuthor = !showingAuthor; } }
public class Calculator { private CalcEngine engine; private UserInterface gui; /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); } /** * In case the window was closed, show it again. */ public void show() { gui.setVisible(true); } }