2010-03-18 77 views
0

我嘗試創建兩級Java計算器時碰到了一堵磚牆。我有一個接口的代碼工作和顯示好,但創建一個單獨的類'CalcEngine'來做實際的計算已被證明是超越了我。在兩個java類型的計算器中創建計算「引擎」的問題

如果有人可以爲我啓動一些東西並創建一個類calcEngine,它可以與接口類一起工作,並允許從單個按鈕輸入時,即如果在calc上按下一個,然後在屏幕上顯示1,則會很感激。

請注意,我不要求別人做這件事對我來說,因爲我想學習,我相信我能做到,包括加減休息等等。一旦我得到了越來越兩個班的障礙溝通。任何和所有的援助將非常感激。

請參閱下面的calcInterface類代碼:

import java.awt.*; 
import javax.swing.*; 
import javax.swing.border.*; 
import java.awt.event.*; 

/** 
*A Class that operates as the framework for a calculator. 
*No calculations are performed in this section 
*/ 
public class CalcFrame 
implements ActionListener 
{ 
    private CalcEngine calc; 

    private JFrame frame; 
    private JTextField display; 
    private JLabel status; 

    /** 
    * Constructor for objects of class GridLayoutExample 
    */ 
    public CalcFrame() 
    { 
     makeFrame(); 
     //calc = engine; 
    } 

    /** 
    * This allows you to quit the calculator. 
    */ 
    // Alows the class to quit. 
    private void quit() 
    { 
     System.exit(0); 
    } 

    // Calls the dialog frame with the information about the project. 
    private void showAbout() 
    { 
     JOptionPane.showMessageDialog(frame, 
        "Group Project", 
        "About Calculator Group Project", 
        JOptionPane.INFORMATION_MESSAGE); 
    } 


    private void makeFrame() 
    { 
     frame = new JFrame("Group Project Calculator"); 
     makeMenuBar(frame); 

     JPanel contentPane = (JPanel)frame.getContentPane(); 
     contentPane.setLayout(new BorderLayout(8, 8)); 
     contentPane.setBorder(new EmptyBorder(10, 10, 10, 10)); 

     /** 
    * Insert a text field 
    */ 
     display = new JTextField(); 
     contentPane.add(display, BorderLayout.NORTH); 

     //Container contentPane = frame.getContentPane(); 
     contentPane.setLayout(new GridLayout(4, 4)); 
     JPanel buttonPanel = new JPanel(new GridLayout(4, 4)); 
     contentPane.add(new JButton("1")); 
     contentPane.add(new JButton("2")); 
     contentPane.add(new JButton("3")); 
     contentPane.add(new JButton("4")); 
     contentPane.add(new JButton("5")); 
     contentPane.add(new JButton("6")); 
     contentPane.add(new JButton("7")); 
     contentPane.add(new JButton("8")); 
     contentPane.add(new JButton("9")); 
     contentPane.add(new JButton("0")); 
     contentPane.add(new JButton("+")); 
     contentPane.add(new JButton("-")); 
     contentPane.add(new JButton("/")); 
     contentPane.add(new JButton("*")); 
     contentPane.add(new JButton("=")); 
     contentPane.add(new JButton("C")); 

     contentPane.add(buttonPanel, BorderLayout.CENTER); 

     //status = new JLabel(calc.getAuthor()); 
     //contentPane.add(status, BorderLayout.SOUTH); 

     frame.pack(); 
     frame.setVisible(true); 
    } 

    /** 
    * Create the main frame's menu bar. 
    * The frame that the menu bar should be added to. 
    */ 
    private void makeMenuBar(JFrame frame) 
    { 
     final int SHORTCUT_MASK = 
      Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(); 

     JMenuBar menubar = new JMenuBar(); 
     frame.setJMenuBar(menubar); 

     JMenu menu; 
     JMenuItem item; 

     // create the File menu 
     menu = new JMenu("File"); 
     menubar.add(menu); 

     // create the Quit menu with a shortcut "Q" key. 
     item = new JMenuItem("Quit"); 
      item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK)); 
      item.addActionListener(new ActionListener() { 
           public void actionPerformed(ActionEvent e) { quit(); } 
          }); 
     menu.add(item); 

     // Adds an about menu. 
     menu = new JMenu("About"); 
     menubar.add(menu); 

     // Displays 
     item = new JMenuItem("Calculator Project"); 
      item.addActionListener(new ActionListener() { 
           public void actionPerformed(ActionEvent e) { showAbout(); } 
          }); 
     menu.add(item); 
    } 


/** 
    * An interface action has been performed. 
    * Find out what it was and handle it. 
    * @param event The event that has occured. 
    */ 
    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("?")) { 

     } 
     // else unknown command. 

     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. 
    */ 
} 

回答

1

的技巧認識到計算器需要有一個「狀態」。對需要兩個操作數的操作以及需要單個操作數(例如平方根)的操作進行可視化會更容易。因此,添加一個很好的狀態持有者(實例變量),該持有者可以保存實際計算引擎可以訪問的前一個條目。希望這是你所要求的。

說明添加

在每個JButton的添加按鈕,監聽器。有一個實例變量,如double firstOp和另一個實例變量String operationType。在數字按鈕的單擊事件上,請將值附加到文本區域。點擊操作數將textArea中包含的字符串解析爲firstOp。雙類有辦法做到這一點。將操作數值存儲在操作類型字段(PLUS,MINUS等)中。如果它是單個操作數運算符。調用Calc Engine的方法並返回值(在文本字段中設置值)。否則,清除文本字段並繼續追加值直到觸發'=',在這種情況下,使用firstOp,operationType和文本字段中的當前值確定要在Calc Engine上調用的方法。最後返回值。

+0

您好,感謝您的回覆,我怕我在這一個初學者,所以你回答有點高於我的水平,你可能會建議calcEngine類的代碼讓我開始,例如我需要輸入什麼來讓它識別1或任何其他按鈕的事件是否被按下?再次感謝。 – tokee 2010-03-18 20:15:20

1

您可以在CalcFrame構造函數中創建類CalcEngine並使用calc = new CalcEngive()

引擎類可以維持2點長的屬性,當前值和前值和enumated值要記住選擇的運營商。

numberPressed方法通過1​​0相乘的電流值,並增加了數量。當您按下一個操作符時,將當前值複製到先前的值(考慮到枚舉器屬性中存在的任何記憶的操作符)。賦值操作符將操作結果複製回當前值,'C'操作符清除兩個當前和以前的值。

正如你看到的,你可以實現所有的功能存在於由法UI框架方法。

希望這會幫助你開始。一旦你做到了,我相信它會全部落實到位。

更新:

在你空CalcEngine類凝視了一會兒後(弄來CalcFrame源作爲任務的一部分?)接下來要採取的步驟是:

  • CalcFrame源添加上述
  • 外觀提到的屬性,並確定發動機的呼叫; calc.xxxxx()
  • 添加您在上一步中找到的方法和通過編譯
  • 檢查依賴實施方法
+0

嗨,感謝您的輸入,在計算引擎類方面,請問您有什麼需要添加到它,目前我只有一個名爲calcEngine的空白類,我不確定要添加什麼對它...任何/所有的援助將非常感激,因爲我已經把自己綁在筆記上了!提前致謝。 – tokee 2010-03-18 23:22:50