- 相關(guān)推薦
java計算器實驗總結(jié)
java計算器實驗總結(jié),做完實驗你會有所發(fā)現(xiàn),怎么寫總結(jié)呢?下面是小編帶來的java計算器實驗總結(jié),歡迎閱讀!
java計算器實驗總結(jié)【1】
GDOU-B-11-112
廣東海洋大學學生實驗報告書(學生用表)
實驗名稱 JAVA計算器程序設計
學院(系)
學生姓名
課程名稱 專業(yè) JAVA程序設計 班級 課程號 完成日期 學號 實驗地點
實驗JAVA計算器程序設計
1) 實驗總結(jié):剛做之前,勉強還能做些小程序,但在進行了計算器程序設計的時候,才發(fā)現(xiàn)很多語句還不能聯(lián)系運用,大部分還是通過百度和書籍才勉勉強強完成,看來實踐才是硬道理
2) 實驗目的
通過計算器程序設計,能夠更加深入的了解JAVA,熟悉圖形用戶的設計原理和程序結(jié)構(gòu)等
4) 實驗內(nèi)容和步驟
實驗內(nèi)容
應用JAVA軟件設計一個簡易的計算器,實現(xiàn)基本的四則運算及括號
設計流程
1. 分析該計算器需要完成的功能。
用戶能夠完成添加括號和負號的四則混合運算,并且計算器能夠自動識別運算符的優(yōu)先級,根據(jù)用戶輸入的運算表達式,自動計算出相應的結(jié)果。
同時還完成了計算器中C按鈕清屏功能和Backspace退格鍵。
2. 考慮異常處理。
(1)當輸入的表達式中出現(xiàn)除零的操作,顯示框?qū)@示“Infinity(無窮大)”。
(2)當輸入的表達式錯誤時,將彈出提示框顯示“表達式錯誤請重新輸入”
(3)當計算器的顯示文本框里為沒有輸入內(nèi)容時直接點擊等號按鈕,將彈出提示框顯示“對不起,您沒有任何輸入,請重新輸入:”。
(4)當?shù)谝淮屋斎氤颂栨I,除號鍵,右括號,小數(shù)點,求余鍵,等號鍵,ce鍵,Backspace鍵時提示錯誤。
4. 考慮數(shù)據(jù)的處理
目標計算表達式通過點擊計算器界面上的按鈕進行輸入,當按下等號之后,計算器通過調(diào)用JAVA中script這個包中的相關(guān)類和方法來實現(xiàn)數(shù)據(jù)的處理。
5. 編碼實現(xiàn)計算器的功能。
(1)新建相關(guān)的文件。
(2)引入JAVA中相關(guān)的包。
(3)新建類Jsq,并繼承了類Frame,而且實現(xiàn)了接口ActionListener
(4)定義相關(guān)的變量,創(chuàng)建相關(guān)組件,并對組件的屬性進行設置。
(5)對所創(chuàng)建的組件進行布局,完成界面的實現(xiàn)。
(6)為各個組件添加事件偷聽。
(7)重寫事件接口ActionListener的方法public void actionPerformed(ActionEvent e)。
(8)為各個組件編寫事件代碼,完成每個按鈕的不同功能。
4、程序
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.List;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Calculator extends Frame
{
public static void main(String[] args) { final Frame window = new Frame("Calculator"); window .setSize(380, 337); int width = Toolkit.getDefaultToolkit().getScreenSize().width; window .setResizable(false); int height = Toolkit.getDefaultToolkit().getScreenSize().height; window.setLocation(width / 2 - 200, height / 2 - 150); window .setLayout(null); /
final JTextField text = new JTextField(); //text.setEditable(false); text.setEditable(true); text.setBounds(10,40,360,50); window .add(text); text.setForeground(Color.blue); text.setBackground(Color.LIGHT_GRAY ); text.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { ScriptEngineManager sem1 = new ScriptEngineManager(); ScriptEngine se1 = sem1.getEngineByName("js"); try{ int leng10 = text.getText().length(); if(leng10 == 0) { JOptionPane.showMessageDialog(null, "對不起,您沒有任何
text.requestFocusInWindow(); text.setHorizontalAlignment(JTextField.RIGHT); 輸入,請重新輸入:");
}
else { } String reslut = text.setText(text.getText()+"="+reslut); text.requestFocus(); se1.eval(text.getText()).toString(); }catch (ScriptException evt){ JOptionPane.showMessageDialog(null,"表達式錯誤,請重新輸入! text.setText(""); //給文本框賦值為
text.requestFocus();
} "); 空達到清屏作用
} }); /***********設置0鍵***************/ final JButton b0 = new JButton("0"); //新建按鈕0 b0.setBounds(10,283,72,50); //設置0鍵的位置 window.add(b0); //把文本框添加到窗口中 b0.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) text.setText(text.getText()+"0"); text.requestFocus(); { } }); /********設置1鍵****************/ final JButton b1 = new JButton("1"); window.add(b1); b1.setBounds(10,233,72,50); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) text.setText(text.getText()+"1"); text.requestFocus();
} {
}); /***********設置2鍵*****************/ final JButton b2 = new JButton("2");
b2.setBounds(82,233,72,50); window.add(b2); b2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) text.setText(text.getText()+"2"); text.requestFocus();
} {
}); /********設置3鍵****************/ final JButton b3 = new JButton("3"); b3.setBounds(154,233,72,50); window.add(b3); b3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e)
} text.setText(text.getText()+"3"); text.requestFocus(); {
});
/*********設置4鍵***************/ final JButton b4 = new JButton("4"); b4.setBounds(10,183,72,50); window.add(b4); b4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e)
} text.setText(text.getText()+"4"); text.requestFocus(); {
}); /*********設置5鍵*************/ final JButton b5 = new JButton("5"); b5.setBounds(82,183,72,50); window.add(b5); b5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) text.setText(text.getText()+"5"); text.requestFocus(); { }
}); /********設置6鍵***************/ final JButton b6 = new JButton("6"); b6.setBounds(154,183,72,50); window.add(b6); b6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) text.setText(text.getText()+"6"); text.requestFocus(); { } }); /*********設置7鍵**************/ final JButton b7 = new JButton("7"); b7.setBounds(10,135,72,50); window.add(b7); b7.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) text.setText(text.getText()+"7"); text.requestFocus(); { } }); /**********設置8鍵***************/ final JButton b8 = new JButton("8"); b8.setBounds(82,135,72,50); window.add(b8); b8.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) text.setText(text.getText()+"8"); text.requestFocus(); { } }); /***********設置9鍵**************/ final JButton b9 = new JButton("9"); b9.setBounds(154,135,72,50); window.add(b9); b9.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
} text.setText(text.getText()+"9"); text.requestFocus(); }); /***********設置加法鍵**************/ final JButton Add = new JButton("+"); Add.setBounds(154,283,72,50); window.add(Add); Add .addActionListener(new ActionListener() { {
text.setText(text.getText()+"+");
text.requestFocus(); public void actionPerformed(ActionEvent e)
} }); /***********設置減法鍵**************/ final JButton Sub = new JButton("-"); Sub.setBounds(298,183,80,50); //設置減法鍵的位置 window.add(Sub); //把減號鍵添加到窗口中 Sub.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e)
text.setText(text.getText()+"-"); text.requestFocus(); {
} }); /***********設置乘法鍵**************/ final JButton Multi = new JButton("*"); Multi.setBounds(226,183,72,50); window.add(Multi); Multi.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e)
int leng3 = text.getText().length(); if(leng3 >= 1 )
{
}
else {
JOptionPane.showMessageDialog(null,"您輸入的第一個字符為乘號,不text.setText(text.getText()+"*"); text.requestFocus(); {
符合要求請重新輸入:");
} }); } text.requestFocus(); /***********設置除法鍵**************/ final JButton Div= new JButton("/"); Div.setBounds(226,135,72,50); window.add(Div); Div.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e)
int leng4 = text.getText().length();
if(leng4 >= 1 )
{
}
else {
JOptionPane.showMessageDialog(null,"您輸入的第一個字符為除號,不text.setText(text.getText()+"/"); text.requestFocus(); {
符合要求請重新輸入:");
text.requestFocus();
} }); /***********設置等號鍵**************/ final JButton equal = new JButton("="); equal.setBounds(226,283,153,50); window.add(equal); equal .addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) ScriptEngineManager sem = new ScriptEngineManager(); ScriptEngine se = sem.getEngineByName("js"); try{ int leng5 = text.getText().length(); if(leng5==0) { JOptionPane.showMessageDialog(null, "對不起,您沒有任何
text.requestFocus(); { } 輸入,請重新輸入:");
} else { String reslut =
se.eval(text.getText()).toString();
}
} text.setText(text.getText()+"="+reslut); text.requestFocus(); }catch (ScriptException evt){ JOptionPane.showMessageDialog(null,"表達式錯誤,請重新輸入! text.setText(""); text.requestFocus(); } ");
}); /***********設置小數(shù)點鍵**************/ final JButton point = new JButton("."); point.setBounds(82,283,72,50); window.add(point); point.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e)
int leng6 = text.getText().length();
if(leng6 > 0)
{
}
else {
JOptionPane.showMessageDialog(null,"您輸入的第一個字符為小數(shù)點,text.setText(text.getText()+"."); text.requestFocus(); {
不符合要求請重新輸入:");
text.requestFocus();
} }); /***********設置左括號鍵**************/ final JButton left = new JButton("("); left .setBounds(226,233,72,50); //設置左括號鍵的位置 window.add(left); //把左括號鍵添加到窗口中 left.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) text.setText(text.getText()+"("); text.requestFocus(); { } } });
/***********設置右括號鍵**************/ final JButton right = new JButton(")"); right.setBounds(298,233,80,50); //設置右括號鍵的位置 window.add(right); //把右括號鍵添加到窗口中 right.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e)
int leng7 = text.getText().length();
if(leng7 > 0)
{
}
else
{
JOptionPane.showMessageDialog(null,"您輸入的第一個字符為右括號,text.setText(text.getText()+")"); text.requestFocus(); {
不符合要求請重新輸入:");
text.requestFocus();
} }); /***********設置清屏鍵**************/ final JButton ce = new JButton("CE"); ce.setBounds(190,90,186,45); window.add(ce); ce.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e)
int leng8 = text.getText().length();
if(leng8 > 0 )
{
}
else
{
} JOptionPane.showMessageDialog(null,"當前以經(jīng)為空,無需清屏!"); text.requestFocus(); text.setText(""); text.requestFocus(); { }
} }); /***********設置退格鍵**************/ final JButton back = new JButton("Backspace");
back.setBounds(10,90,180,45); window.add(back); back.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) String message; int leng = text.getText().length(); message=text.getText(); if(leng==0) {
}
else
{
text.setText(message.substring(0,leng-1)); text.requestFocus(); JOptionPane.showMessageDialog(null, "對不起,您沒有任何輸入無法 text.requestFocus(); { 退格:");
} } }); final JButton Air = new JButton("%"); Air.setBounds(298,135,80,50); window.add(Air); Air.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int leng9 = text.getText().length(); { if(leng9 > 0) { } else { JOptionPane.showMessageDialog(null,"您輸入的第一個字符為求text.setText(text.getText()+"%"); text.requestFocus(); 余數(shù),不符合要求請重新輸入:");
text.requestFocus();
} }
} }); /*********關(guān)閉按鈕***********/ window .addWindowListener(new WindowAdapter() { {
System.exit(0); } public void windowClosing(WindowEvent e)
} } });
程序截圖
1.初始運行界面
2.運行測試
成績 指導教師
日期 注:請用A4紙書寫,不夠另附紙。
第 頁,共 頁
java計算器實驗總結(jié)【2】
終于搞定了這份實訓報告,受益匪淺啊
一開始想做計算器那個課題,原因很簡單,就是因為我感覺那個課題很簡單,在百度里一搜就搜到了一模一樣的源程序。
只是感覺沒什么意思,那個界面也不是很好看,又不好玩。
所以就做了現(xiàn)在這個猜數(shù)游戲。
一直想編個游戲程序,于是就到網(wǎng)上去搜代碼,一下子就搜到了,而且代碼很少,簡單易懂 。
程序是弄好了,那么簡短的程序?qū)懫饒蟾婢头鸽y了!不得不把程序再添加些模塊,充實充實。
這也讓我感覺實訓遠比想像的難度大,不是網(wǎng)上隨便搜一下就可以交差的!不僅需要自己看懂代碼,還需要根據(jù)自己的需要添加模塊,充實代碼。
從網(wǎng)上搜的代碼都是字符界面的,老師說既然字符界面的沒挑戰(zhàn)性,那就做圖形界面的好了。
做圖形界面就有點犯難了,首先沒有什么程序可以參照了,必須自己充分理解字符界面的源代碼,然后逐步修改完善。
我依據(jù)課本上的GUI章節(jié)設計了此次的java簡單程序,基本實現(xiàn)了課題的要求,還添加了圖片以增加視覺效果,以及確定按鈕方便用戶確定輸入數(shù)字。
通過這次實訓我的體會歸納為以下4點:
1. 態(tài)度第一 擺正自己的心態(tài),不要以為什么東西上網(wǎng)搜搜就可以了,一定要有自己的東西。
只有自己付出過,當程序運行成功時的那種喜悅才會令自己有一種莫名的自豪感。
態(tài)度決定一切!
2. 興趣是關(guān)鍵 我的學習完全是興趣導向的,所以壓力并不大。
因為有興趣,所以我會很想充分理解一切細節(jié)。
又因為理解,所以許多原本片片斷斷的知識都可以漸漸互相融會貫通,累積技術(shù)能量,理論和實務之間的藩籬被打破了,學習效率倍增。
學習并快樂著!
3. 敢于挑戰(zhàn) 不安于現(xiàn)成的程序,要敢于用多種方法實現(xiàn)一個目的。
現(xiàn)在的網(wǎng)絡科技很發(fā)達,想要一個源程序的確很簡單,所以很多人都是在自己都不明白題目要求時,就可以在網(wǎng)上搜到一模一樣的代碼,根本沒有自己的東西。
挑戰(zhàn)自我是項艱巨的任務。
只有堅持自己多看代碼多寫程序才會有更多的收獲。
4. 不懈追求 要對源代碼進行不斷地完善,要盡可能地實現(xiàn)課題所要求的功能。
對初學者來說,大量地寫程序是必要的,但過了某個階段之后,寫程序所帶來的技術(shù)能力成長已經(jīng)到了極限,還不如多花一點時間看書,學新技術(shù)和新觀念。
可以從實現(xiàn)源程序功能著手,比如添加寫模塊啊什么的,實現(xiàn)更多功能。
看著自己編的程序能成功運行,這樣自己會更感興趣,也就會樂此不疲。
【java計算器實驗總結(jié)】相關(guān)文章:
java課程總結(jié)心得 java實驗總結(jié)與心得10-29
java實驗心得06-21
java實驗報告12-01
java課程總結(jié)范文11-10
java實訓總結(jié)11-18
java課程設計總結(jié)11-19
java專業(yè)實訓總結(jié)11-17
java實習工作總結(jié)06-07
java項目個人總結(jié)模板04-04