Java程序設(shè)計(jì)實(shí)驗(yàn)報(bào)告-計(jì)算器.doc
《Java程序設(shè)計(jì)實(shí)驗(yàn)報(bào)告-計(jì)算器.doc》由會(huì)員分享,可在線(xiàn)閱讀,更多相關(guān)《Java程序設(shè)計(jì)實(shí)驗(yàn)報(bào)告-計(jì)算器.doc(19頁(yè)珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。
1、 Java程序設(shè)計(jì)實(shí)驗(yàn)報(bào)告題 目 計(jì)算器 姓 名 學(xué) 號(hào) 學(xué) 院 軟件學(xué)院 專(zhuān) 業(yè) 軟件工程 年 級(jí) 2009 級(jí) 指導(dǎo)教師 2010年 4 月 9 日一、實(shí)驗(yàn)?zāi)康?、 熟練掌握java面向?qū)ο缶幊獭?、 選擇合適的數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)需求。3、 熟練使用各種控制結(jié)構(gòu)。4、 掌握GUI組件、事件處理技術(shù)。二、實(shí)驗(yàn)任務(wù)1、 設(shè)計(jì)一個(gè)簡(jiǎn)易的計(jì)算器,可以進(jìn)行四則運(yùn)算:加、減、乘、除等(限于十進(jìn)制下)程序要求:(1)應(yīng)具有相應(yīng)的界面,可參考下圖進(jìn)行設(shè)計(jì): 圖示1 圖示2(2)操作符號(hào)定為:“+”,“-”,“*”,“/”,“(”,“)”等。(按國(guó)際慣例設(shè)計(jì))(3)用戶(hù)通過(guò)點(diǎn)擊面板中的各種組件輸入的表達(dá)式,應(yīng)在上
2、方文本框中顯示。(4)當(dāng)用戶(hù)點(diǎn)擊“=”時(shí),文本框中之前的內(nèi)容應(yīng)清空,直接顯示表達(dá)式運(yùn)算結(jié)果。例如:輸入表達(dá)式“3+2*(8-5)”之后按“=”,顯示結(jié)果“9”。(5)具有菜單欄,內(nèi)有相應(yīng)的編輯“復(fù)制”、“粘貼”等菜單,提供相應(yīng)的功能。(6)以上部分為必須完成的內(nèi)容。2、選作功能,可參考windows系統(tǒng)附帶計(jì)算器: (1)具有相應(yīng)的科學(xué)計(jì)算功能。例如“圖示2”中的各種科學(xué)計(jì)算功能。三、開(kāi)發(fā)工具與平臺(tái)1.開(kāi)發(fā)工具:Eclipse默認(rèn)是一個(gè)和Jbuilder類(lèi)似的Java開(kāi)發(fā)工具,但它不僅僅只 是Java開(kāi)發(fā)工具,只要裝上相應(yīng)的插件,eclipse也可作為其它語(yǔ)言的開(kāi)發(fā)工具。如C/C+插件(CDT
3、)。2.開(kāi)發(fā)平臺(tái):JDK1.5四、設(shè)計(jì)思路1.界面設(shè)計(jì) (1)制作一個(gè)簡(jiǎn)單的面板JFrame,在其上面添加構(gòu)造界面所需的各種組件其構(gòu)造方法為:calculate()super();this.setBounds(10, 10, 300, 250);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setResizable(false);this.setTitle(計(jì)算器);init();/創(chuàng)建菜單及各種組件(如按鈕,文本框等)getContentPane().setLayout(new BorderLayout(3,3);getCo
4、ntentPane().add(panelCenter,BorderLayout.CENTER);getContentPane().add(panelNorth,BorderLayout.NORTH);(2)實(shí)現(xiàn)構(gòu)造菜單及文本框等組件的函數(shù)init(),如下實(shí)現(xiàn):Public void Init()/菜單項(xiàng)的構(gòu)造形成; editor.add(copy);editor.addSeparator();editor.add(paste); editor.setMnemonic(E);look.setMnemonic(V);help.setMnemonic(H); copy.setAccelerato
5、r(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK); paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK); help.add(about); bar.add(editor);bar.add(look);bar.add(help); setJMenuBar(bar);/將菜單添加到JFrame; /* 接下來(lái)是文本或結(jié)果顯示區(qū)及Backspace、功能鍵(CE、C)和的添加,采用 GridBagLayout布局方式,將它
6、們放在北部panelNorth面板。 其次,是數(shù)字鍵及其他功能鍵的的使用,我這里采用的是網(wǎng)格布局,setLayout(new GridLayout(4,6,5,5);將它們放在主面板中心區(qū)域。 這里給出網(wǎng)格包布布局的兩個(gè)重要方法,由于代碼段較長(zhǎng),故將其他的代碼放在后面附錄;private void makeShow(GridBagLayout gridBag,GridBagConstraints constraints, Component component) gridBag.setConstraints(component, constraints);/ 網(wǎng)格包布布局; panelNort
7、h2.add(component);private void addButton(GridBagLayout gridBag,/網(wǎng)格包布布局重要方法;GridBagConstraints constraints, Component component) gridBag.setConstraints(component, constraints); panelNorth1.add(component);總體界面如下:2.邏輯設(shè)計(jì) 為了能夠得到我們想要的操作,比如讓那些數(shù)字參與運(yùn)算,做什么運(yùn)算等等,我們就需要為每個(gè)按鈕添加響應(yīng)事件,通過(guò)響應(yīng)事件來(lái)完成我們預(yù)期的結(jié)果。以下我們來(lái)分析下重要的事件是如
8、何響應(yīng)的:.String str = e.getActionCommand();/ TODO Auto-generated method stubif(str.equals(Backspace)/點(diǎn)擊 BackspacehanderBackspace();else if(str.equals(CE)/點(diǎn)擊了CE的功能是清除當(dāng)前顯示的數(shù)值bool = false;show.setText(0);else if(str.equals(C)/點(diǎn)擊了C的功能是清零copyText=;Div = 0.0;CalculateResult =;bool = false;show.setText(0);els
9、e if(str.equals(MC)/MC清楚存儲(chǔ)器內(nèi)容handerMC();else if(str.equals(MR)/MR將存于存儲(chǔ)器的值顯示在文本框中handerMR();else if(str.equals(MS)/MS將顯示框得文字存于存儲(chǔ)器handerMS();else if(str.equals(M+)/M+將顯示框的數(shù)與存儲(chǔ)器中的數(shù)相加并進(jìn)行存儲(chǔ)handerM();else if(0123456789.indexOf(str)=0)/按下的是數(shù)字鍵或者小數(shù)點(diǎn)正負(fù)號(hào)handerCenter(str);else if(str.equals(%)/將當(dāng)前顯示框里的數(shù)字用百分比表示
10、( x/100)String current = show.getText();double a = 0;if(!current.isEmpty() a = Double.parseDouble(current); Double result =(Double) a/100; String s4 = result.toString(); if(!isInt(s4) show.setText(s4);else show.setText(s4.substring(0,s4.indexOf(.); bool = true; else handerOperation(str);/響應(yīng)的是運(yùn)算符;處理運(yùn)
11、算符操作3. 程序測(cè)試 程序測(cè)試過(guò)程中無(wú)較大BUG,基本上輕松搞定。五、實(shí)驗(yàn)總結(jié) 這是我第一次正式的java程序設(shè)計(jì)實(shí)驗(yàn),感覺(jué)能夠把學(xué)到的東西用到實(shí)際中是件非常有趣的事,更重要的是讓我們真正明白了學(xué)貴致用的道理。 由于時(shí)間有限,本程序未能做到科學(xué)計(jì)算器,同時(shí)在這里也感謝老師的指導(dǎo)和同學(xué)的幫助。6、 附錄public class calculate extends JFrame implements ActionListener /* * param args */private JMenuBar bar = new JMenuBar();private JMenu editor =new JM
12、enu(編輯(E);private JMenuItem copy =new JMenuItem(復(fù)制);private JMenuItem paste =new JMenuItem(粘貼);private JMenu look = new JMenu(查看(V);private JMenu help = new JMenu(幫助(H);private JMenuItem about = new JMenuItem(關(guān)于);private String copyText ;private JPanel panelWest= new JPanel();private JPanel panelCen
13、ter = new JPanel();/放Number鍵private JPanel panelNorth = new JPanel();/放功能鍵1與文本框private JPanel panelNorth1 = new JPanel();private JPanel panelNorth2 = new JPanel();/放顯示框private JPanel panelEast = new JPanel();/放運(yùn)算鍵;private String strNorth = new StringBackspace,CE,C;/功能鍵1private String strCenter = new
14、 String7,8,9,4,5,6,1,2,3,0,+/-,.;private String strEast = new String/,sqrt,*,%,-,1/x,+,=;private StringstrWest = new StringMC,MR,MS,M+;private JButton buttonCenter = new JButtonstrCenter.length;private JButton buttonEast= new JButtonstrEast.length;private JButton buttonNorth = new JButtonstrNorth.le
15、ngth;private JButton buttonWest = new JButtonstrWest.length;private String ch=new String();/保存當(dāng)前運(yùn)算符private Double Div;/保存中間運(yùn)算數(shù)private String save = new String();/保存存儲(chǔ)器中的數(shù)值private String CalculateResult= new String() ;/保存運(yùn)算結(jié)果private boolean bool = false;/當(dāng)前結(jié)果是否為運(yùn)算結(jié)果private JTextField show = new JText
16、Field(0,24);/輸出顯示private JTextField label;private GridBagLayout gridBag = new GridBagLayout();/網(wǎng)格包布布局;private GridBagConstraints constraints = new GridBagConstraints();calculate()super();this.setBounds(10, 10, 300, 250);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setResizable(false);thi
17、s.setTitle(計(jì)算器);init();getContentPane().setLayout(new BorderLayout(3,3);getContentPane().add(panelCenter,BorderLayout.CENTER);getContentPane().add(panelNorth,BorderLayout.NORTH);public void init() editor.add(copy);editor.addSeparator();editor.add(paste); editor.setMnemonic(E);look.setMnemonic(V);hel
18、p.setMnemonic(H); copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK); paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK); help.add(about); bar.add(editor);bar.add(look);bar.add(help); setJMenuBar(bar); copy.addActionListener(this); paste.addAc
19、tionListener(this); about.addActionListener(this); panelNorth1.setLayout(gridBag);/ panelNorth2.setLayout(gridBag); show.setHorizontalAlignment(JTextField.RIGHT);/右對(duì)齊; show.setEditable(false); panelNorth.setLayout(new BorderLayout(); panelNorth.add(show,BorderLayout.NORTH); show.setBorder(new BevelB
20、order(BevelBorder.LOWERED);/讓顯示框凹陷下去 show.setBackground(Color.WHITE); /網(wǎng)格包布布局; constraints.gridx = 0; constraints.gridy = 0; constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.CENTER; makeShow(gridBag,constraints,show); /* /設(shè)置箱式布局間隔 (另一種布局方式) * / box.add(label); for(
21、int i = 0;istrNorth.length;+i) buttonNorthi = new JButton(strNorthi); box.add(Box.createHorizontalStrut(10); box.add(buttonNorthi); buttonNorthi.addActionListener(this); buttonNorthi.setForeground(Color.RED); */ label = new JTextField(2); label.setEditable(false); label.setBorder(new BevelBorder(Bev
22、elBorder.LOWERED);/讓文本框凹陷下去; constraints.gridx = 0; constraints.gridy = 0; constraints.fill = GridBagConstraints.NONE; constraints.insets = new Insets(10,5,10,10); addButton(gridBag,constraints,label); for(int j = 0;jstrNorth.length;+j) buttonNorthj = new JButton(strNorthj); buttonNorthj.addActionLi
23、stener(this); buttonNorthj.setForeground(Color.RED); constraints.gridx =j+1 ; constraints.gridy = 0; addButton(gridBag,constraints,buttonNorthj); panelNorth.add(panelNorth1,BorderLayout.CENTER); panelNorth.add(panelNorth2,BorderLayout.NORTH); panelCenter.setLayout(new GridLayout(4,6,5,5); for(int i
24、=0;istrWest.length;+i) buttonWesti = new JButton(strWesti); buttonWesti.setBorder(new BevelBorder(BevelBorder.RAISED); buttonWesti.setForeground(Color.RED); buttonWesti.addActionListener(this); for(int i =0;istrCenter.length;+i) buttonCenteri = new JButton(strCenteri); buttonCenteri.setBorder(new Be
25、velBorder(BevelBorder.RAISED); buttonCenteri.setForeground(Color.BLUE); buttonCenteri.addActionListener(this); for(int i =0;istrEast.length;+i) buttonEasti = new JButton(strEasti); buttonEasti.setBorder(new BevelBorder(BevelBorder.RAISED); buttonEasti.addActionListener(this); if(i%2=0|(i=7) buttonEa
26、sti.setForeground(Color.RED); else buttonEasti.setForeground(Color.BLUE); int w =0,e = 0, c = 0; for(int i = 0;i24;) if(i%6=0) buttonWestw.setBorder(new LineBorder(Color.GRAY); panelCenter.add(buttonWestw); +i; +w; else for(int j = 0;j3;+j) buttonCenterc.setBorder(new LineBorder(Color.GRAY); panelCe
27、nter.add(buttonCenterc); +i; +c; for(int j = 0;j=0)/按下的是數(shù)字鍵或者小數(shù)點(diǎn)正負(fù)號(hào)handerCenter(str);else if(str.equals(%)/將當(dāng)前顯示框里的數(shù)字用百分比表示( x/100)String current = show.getText();double a = 0;if(!current.isEmpty() a = Double.parseDouble(current); Double result =(Double) a/100; String s4 = result.toString(); if(!isI
28、nt(s4) show.setText(s4);else show.setText(s4.substring(0,s4.indexOf(.); bool = true; else handerOperation(str);private void handerMC() save = ;/ TODO Auto-generated method stublabel.setText();private void handerMR() if(!save.isEmpty() if(!isInt(save) show.setText(save); else show.setText(save.substr
29、ing(0,save.indexOf(.); private void handerMS() save = show.getText();/ TODO Auto-generated method stubprivate void handerM() String str = show.getText();/ TODO Auto-generated method stubdouble s=0,v=0;if(!str.isEmpty()&!save.isEmpty() s= Double.parseDouble(str); v = Double.parseDouble(save);save = S
30、tring.valueOf(s+v);label.setText(M);private void handerCenter(String str) String result = show.getText();if(str.equals(.)if(!(result.indexOf(.)=0)/判斷是否已存在小數(shù)點(diǎn)if(bool)show.setText();result=;result+=str;show.setText(result);bool = false;String num = result;if(*-+/.indexOf(ch)=0) handerResult(ch,num); e
31、lse if(result.equals(0)result=str;show.setText(result);bool = false;String num = result;if(*-+/.indexOf(ch)=0)handerResult(ch,num); else if(bool)show.setText();/文本框不為0.既有數(shù)字或者0和小數(shù)點(diǎn)result = ;result+=str;show.setText(result);bool = false;String num = result;/保存if(*-+/.indexOf(ch)=0) handerResult(ch,num
32、); private void handerOperation(String str) String result = show.getText();if(str.equals(+/-)/處理正負(fù)號(hào) if(result.indexOf(-)=0) StringBuffer e = new StringBuffer(result); e.deleteCharAt(0); show.setText(e.toString(); String num = e.toString(); if(*-+/.indexOf(ch)=0) handerResult(ch,num); else StringBuff
33、er e = new StringBuffer(result); e.insert(0, -); show.setText(e.toString(); String num = e.toString(); if(*-+/.indexOf(ch)=0) handerResult(ch,num); else if(str.equals(sqrt)/平方根功能if(!(result.indexOf(-)=0)double operate=0;if(!result.isEmpty() operate =Double.parseDouble(result); double reult = Math.sq
34、rt(operate); Double a = reult; String s =a.toString(); if(!isInt(s) show.setText(s); else show.setText(s.substring(0,s.indexOf(.); bool = true; if(*-+/.indexOf(ch)=0) handerResult(ch,s); else show.setText(函數(shù)輸入無(wú)效);bool= true;else if(str.equals(/)/除法運(yùn)算;String show2 = show.getText();show.setText();ch=/
35、;double operate = 0;if(!show2.isEmpty()operate = Double.parseDouble(show2); Div = (Double)operate;/保存被除數(shù);else if(str.equals(*)String show2 = show.getText();show.setText();ch=*;/保存當(dāng)前運(yùn)算符double operate = 0;if(!show2.isEmpty() operate = Double.parseDouble(show2);Div = (Double)operate;else if(str.equals(
36、-)String show2 = show.getText();show.setText();ch=-;double operate =0;if(!show2.isEmpty() operate= Double.parseDouble(show2);Div = (Double)operate;else if(str.equals(+)String show2 = show.getText();show.setText();ch=+;double operate = 0;if(!show2.isEmpty() operate = Double.parseDouble(show2); Div =
37、(Double)operate;/保存加數(shù)else if(str.equals(1/x)/求倒String show2 = show.getText();if(!show2.equals(0)double operate = 0;if(!show2.isEmpty() operate = Double.parseDouble(show2);double resultReverse = (1/operate);Double resultReverseDouble = resultReverse;show.setText(resultReverseDouble.toString();bool =
38、true;else show.setText(除數(shù)不能為零。);bool = true;else if(str.equals(=)show.setText(CalculateResult);bool = true;private boolean isInt(String s) if(s.indexOf(.)0)for(int i =(s.length()-1);is.indexOf(.);-i) if(!(s.charAt(i)=0) return false;return true;private void handerResult(String ch,String num) /處理運(yùn)算結(jié)果
39、并保存; char swt = 0 ;if(!ch.equals() swt = ch.charAt(0);/將String轉(zhuǎn)換成charswitch(swt)case -: double resultSub = (Div-Double.parseDouble(num);Double resultSubDouble = resultSub;String s1 = resultSubDouble.toString(); if(!isInt(s1) CalculateResult=s1; else CalculateResult = s1.substring(0,s1.indexOf(.); /保
40、存運(yùn)算結(jié)果;break;case +:double resultAdd = (Div+Double.parseDouble(num);Double resultAddDouble = resultAdd;String s2 = resultAddDouble.toString(); if(!isInt(s2) CalculateResult=s2; else CalculateResult = s2.substring(0,s2.indexOf(.); break;case *: double resultMul = (Div*(Double.parseDouble(num); Double
41、resultMulDouble = resultMul; String s3 = resultMulDouble.toString(); if(!isInt(s3) CalculateResult=s3; else CalculateResult = s3.substring(0,s3.indexOf(.); break;case /: if(Double.parseDouble(num)!=0) double resultDiv = (Div/(Double.parseDouble(num); Double resultDivDouble = resultDiv; String s4 = r
42、esultDivDouble.toString(); if(!isInt(s4) CalculateResult=s4; else CalculateResult = s4.substring(0,s4.indexOf(.); else CalculateResult = 除數(shù)不能為零。; break;private void handerBackspace() /退格鍵String show1 = show.getText();if(!bool)if(!(show1.equals(0)try String result = show1.substring(0, show1.length()-1); show.setText(result); if(result.isEmpty() show.setText(0); return; catch(ArrayIndexOutOfBoundsException e)e.printStackTrace();
- 溫馨提示:
1: 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 道路運(yùn)輸組織 客運(yùn)設(shè)施現(xiàn)代化PPT課件
- 應(yīng)用程序的結(jié)構(gòu)工程師課件
- 正弦穩(wěn)態(tài)分析
- 汽車(chē)照明與信號(hào)系統(tǒng)1
- 攝像頭的工作原理PPT課件
- 高中英語(yǔ)課程標(biāo)準(zhǔn)簡(jiǎn)介
- 目標(biāo)管理實(shí)務(wù)布衣公子作品版teliss課件
- 高中英語(yǔ)新課程通識(shí)培訓(xùn)校本研修
- (河南專(zhuān)版)九年級(jí)化學(xué)上冊(cè) 第五單元 化學(xué)方程式 課題1 第2課時(shí) 化學(xué)方程式(增分課練)習(xí)題課件 (新版)新人教版
- XXXX年淥口三級(jí)聯(lián)儲(chǔ)推介會(huì)講稿
- 131平方根(教育精品)
- 一年級(jí)《漢語(yǔ)拼音復(fù)習(xí)三》課件(教育精品)
- 為品牌戰(zhàn)略奠定基礎(chǔ)
- 化妝整體服務(wù)方案課件
- 同分異構(gòu)體的書(shū)寫(xiě)課件