




下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、淮海 工學院 計算機工程學院課程設計報告設計名稱: 面向對象課程設計選題名稱:計算器的設計與實現姓 名: 陳瑤 學 號:2015123226專業(yè)班級:網絡151系(院): 計算機工程學院設計時間: 2016.11.282016.12.10設計地點:軟件實驗室、教室成績:指導教師評語:簽名:1 .課程設計目的面向對象程序設計是一門實踐性很強的計算機專業(yè)基礎課程,課程設計是學習完該課程后進行的一次較全面的綜合練習。其目的在于通過實踐加深學生對面向對象程序設計的理論、方法和基礎知識的理解,掌握使用 Java語言進行面向對象設計的基本方法,提高運用面向對象知識分 析實際問題、解決實際問題的能力。同時復
2、習本學期所學的知識以及運用于實踐中去,將基本知識點進行穩(wěn)定,使之更加熟悉知識點。通過本次學習了解以下的內容:1)、類、包、接口的定義與使用2)、Java常用的標準 GUI組件及事件處理2 .課程設計任務與要求:設計一個能實現計算器功能的Java程序,可以進行加、減、乘、除(另外可適當加 上階乘、正弦、余弦和指數)運算。中間可以加入一些自己的想法,比如界面如何設計出一些特色,算法的實現是否可 以加入自己的想法,以上種種各樣的想法以及構思。Java中提供了大量的類有一些問題的解決方法根本不用自己去從新定義,前人已經栽好了樹我們只需要直接拿過來用就好 了,所以對于一些你想解決但覺得自己又解決不了的問
3、題,可以上網上查查有沒有類似 的已經有現成的函數拿過來用用。3.課程設計說明書、需求分析計算器大家都不陌生,使用量也是非常的大,所以一款操作方便結構豐富的計算器是非 常有必要的,所以我們在這里設計出一款適合大眾的計算器。輸入一個數Display。方法顯讀入下一個符Math. cos()Math.Math.sin()pow()Funjiecheng()Display。方法顯示二、概要設計Calculator類用來實現所要應用的各種功能:Calculator extends JFrame'<<Interface>>一一 聲 11!ActionListener1* _
4、pnll: JPanel pnl2: JPanel textfild: JTextField grd: GridLayout b0: JButton b1 :JButton b2: JButton b3: JButton b4: JButton b5 :JButton b6: JButton b7: JButton b8: JButton b9: JButton bp: JButton bd: JButton badd:JButton bsub:JButton bmul: :JButton bdiv:JButton bcos: :JButton bsin:JButton bjiecheng:J
5、Button bpow:JButton beql:JButton bclear:JButton num1:double num2:double end:boolean add: boolean sub: boolean mul: boolean div: boolean cos: boolean sin: boolean jiecheng : boolean pow: boolean面向對象課程設計報告第5頁,共 24頁Calculator。Funjiecheng()actionPerformend(ActionEvent e)Display(int i)加上代碼:public class C
6、alculator extends JFrame implements ActionListenerJMenuBar mb;JMenu menu;JMenuItem t,t1,t2;JPanel pnl1,pnl2;JTextField textfild;GridLayout grd;JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bp,bd;JButton badd,bsub,bmul,bdiv,bcos,bsin,bjiecheng,bpow,beql,bclear;String str;boolean end,add,sub,mul,div,cos,sin,j
7、iecheng,pow;double num1,num2;Calculator。public void actionPerformed(ActionEvent e)else if(e.getSource()=bsub)else if(e.getSource()=bmul)else if (e.getSource()=bdiv)else if(e.getSource()=bcos)end=true;解俞入結束else if(e.getSource()=bsin)else if(e.getSource()=bjiecheng)else if(e.getSource()=bpow)else if(e
8、.getSource()=bclear)bd.setEnabled(true);textfild.setText("0");else if(e.getSource()=bd)str=textfild.getText();str+="."texfild.setText(str);bd.setEnabled(false);else if(e.getSource()=beql)bd.setEnabled(true);num2=Double.parseDouble(texfild.getText();/數字類型轉化 if(add)num1=num1+num2;e
9、lse if(sub)num1=num1-num2;else if(mul)num1=num1*num2;else if(div)if(num2=0) textfild.setText("輸入非法!"); return;num1=num1/num2;else if(cos)num1=Math.cos(num1);else if(sin)num1=Math.sin(num1);else if(jiecheng)num1=funjiecheng(num1);else if(pow)num1=Math.pow(num1,num2);/調用庫函數中的 pow 方法texfild.s
10、etText(String.valueOf(num1);把所得結果顯示在文本框 end=true;public double funjiecheng(double i)double num=1;if(i=0|i=1)num=1;elsenum=num*funjiecheng(i-1);return num;public void Display(int i)String s=null;s=String.valueOf(i);if(end)/如果數字輸入結束,則將文本框置零,重新輸入texfild.setText("0");end=false;if(tex帕ld.getTex
11、t().equals("0")/如果文本框的內容為零,則覆蓋文本框的內容textfild.setText(s); elsestr=textfild.getText()+s;textfild.setText(str);Text類是對Calculator類的詳細實現及其測試:Text main(String a)public class Textpublic static void main(String a)new Calculator。;三、詳細設計加上代碼:import java.awt*;import java.awt.event.*;import javax.swin
12、g.*;public class Calculator extends JFrame implements ActionListenerJMenuBar mb;JMenu menu;JMenuItem t,t1,t2;JPanel pnl1,pnl2;JTextField textfild;GridLayout grd;JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bp,bd;JButton badd,bsub,bmul,bdiv,bcos,bsin,bjiecheng,bpow,beql,bclear;String str;boolean end,add,sub
13、,mul,div,cos,sin,jiecheng,pow;double num1,num2;Calculator。super("簡單的計算器”);grd=new GridLayout(6,4,6,6);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);pnl1=new JPanel(grd);pnl2=new JPanel(grd);Container cp=this.getContentPane();姓意 getContentPanecp.add(pnl1,"North");cp.add(pnl2,&quo
14、t;Center");pnl1.setBackground(Color.orange);面向對象課程設計報告第11頁,共 24頁pnl2.setBackground(Color.green);this.setBounds(400,500,450,240);textfild=new JTextField("0",33);textfild.setHorizontalAlignment(JTextField.CENTER);mb=new JMenuBar();menu=new JMenu('說明書");t=new JMenuItem("此計算
15、器使用方便,可以實現連續(xù)不同優(yōu)先級的運算。當進 行一次運算后,");t1=new JMenuItem("再點擊數字按鈕則開始新一輪的運算,如果在是運算后直接 點運算符號(加減等)");t2=new JMenuItem("則會直接繼續(xù)上次的結果進行運算。");menu.add(t);menu.add(t1);menu.add(t2);mb.add(menu);b0=new JButton("0");b1=new JButton('T);b2=new JButton("2");b3=new JButt
16、on("3");b4=new JButton("4");b5=new JButton("5");b6=new JButton("6");b7=new JButton("7");b8=new JButton("8");b9=new JButton("9");bd=new JButton(".");bdiv=new JButton(" +");bdiv.setForeground(Color.red);bmul=new
17、JButton(" x");bmul.setForeground(Color.red);badd=new JButton("+");badd.setForeground(Color.red);bsub=new JButton("-");bsub.setForeground(Color.red);bcos=new JButton("Cos");bcos.setForeground(Color.blue);bsin=new JButton("Sin");bsin.setForeground(Colo
18、r.blue);bjiecheng=new JButton("n!");bjiecheng.setForeground(Color.blue);bclear=new JButton("C");bpow=new JButton("xAy");bpow.setForeground(Color.blue);beql=new JButton("=");pnll.add(mb);pnll.add(textfild);pnl2.add(b9);b9.addActionListener(this);pnl2.add(b8);b8
19、.addActionListener(this);pnl2.add(b7);b7.addActionListener(this);pnl2.add(bdiv);bdiv.addActionListener(this);pnl2.add(b6);b6.addActionListener(this);pnl2.add(b5);b5.addActionListener(this);pnl2.add(b4);b4.addActionListener(this);pnl2.add(bmul);bmul.addActionListener(this);pnl2.add(b3);b3.addActionLi
20、stener(this);pnl2.add(b2);b2.addActionListener(this);pnl2.add(b1);bl.addActionListener(this);pnl2.add(badd);badd.addActionListener(this);pnl2.add(bsub);bsub.addActionListener(this);pnl2.add(b0);bO.addActionListener(this);pnl2.add(bd);bd.addActionListener(this);pnl2.add(bjiecheng);bjiecheng.addAction
21、Listener(this);pnl2.add(badd);badd.addActionListener(this);pnl2.add(bsin);bsin.addActionListener(this);pnl2.add(bcos);bcos.addActionListener(this);pnl2.add(bpow);bpow.addActionListener(this);pnl2.add(bclear);bclear.addActionListener(this);pnl2.add(beql);beql.addActionListener(this);this.setVisible(t
22、rue);public void actionPerformed(ActionEvent e)if(e.getSource()=b0)Display(0);else if(e.getSource()=b1)Display(1);else if(e.getSource()=b2)Display(2);else if(e.getSource()=b3)Display(3);else if(e.getSource()=b4)Display(4);else if(e.getSource()=b5)Display(5);else if(e.getSource()=b6)Display(6);else i
23、f(e.getSource()=b7)Display(7);else if(e.getSource()=b8)Display(8);else if(e.getSource()=b9)Display(9);else if(e.getSource()=badd)add=true;sub=false;mul=false;div=false;cos=false;sin=false;jiecheng=false;pow=false;bd.setEnabled(true);num1=Double.parseDouble(texfild.getText();接收第一個字符并轉化成double 型end=tr
24、ue;解俞入結束else if(e.getSource()=bsub)add=false;sub=true;mul=false;div=false;cos=false;sin=false;jiecheng=false;pow=false;bd.setEnabled(true);num1=Double.parseDouble(texfild.getText();接收第一個字符并轉化成double 型end=true;解俞入結束else if(e.getSource()=bmul)add=false;sub=false;mul=true;div=false;cos=false;sin=false;
25、jiecheng=false;pow=false;bd.setEnabled(true);num1=Double.parseDouble(texfild.getText();接收第一個字符并轉化成double 型end=true;解俞入結束else if (e.getSource()=bdiv)add=false;sub=false;mul=false;div=true;cos=false;sin=false;jiecheng=false;pow=false;num1=Double.parseDouble(texfild.getText();接收第一個字符并轉化成double 型end=tru
26、e;解俞入結束else if(e.getSource()=bcos)add=false;sub=false;mul=false;div=false;cos=true;sin=false;jiecheng=false;pow=false;bd.setEnabled(true);num1=Double.parseDouble(texfild.getText();接收第一個字符并轉化成double 型end=true;解俞入結束else if(e.getSource()=bsin)add=false;sub=false;mul=false;div=false;cos=false;sin=true;j
27、iecheng=false;pow=false;bd.setEnabled(true);num1=Double.parseDouble(texfild.getText();接收第一個字符并轉化成double 型end=true;解俞入結束else if(e.getSource()=bjiecheng)add=false;sub=false;mul=false;div=false;cos=false;sin=false;jiecheng=true;pow=false;bd.setEnabled(true);num1=Double.parseDouble(texfild.getText();接收第
28、一個字符并轉化成 double 型end=true;解俞入結束else if(e.getSource()=bpow)add=false;sub=false;mul=false;div=false;cos=false;sin=false;jiecheng=false;pow=true;bd.setEnabled(true);num1=Double.parseDouble(texfild.getText();接收第一個字符并轉化成 doubleend=true;解俞入結束else if(e.getSource()=bclear)bd.setEnabled(true);textfild.setTex
29、t("0");else if(e.getSource()=bd)str=textfild.getText();str+="."textfild.setText(str);bd.setEnabled(false);else if(e.getSource()=beql)bd.setEnabled(true);num2=Double.parseDouble(texfild.getText();/數字類型轉化 if(add)num1=num1+num2;else if(sub)num1=num1-num2;else if(mul)num1=num1*num2;else if(div)if(num2=0) textfild.setText("輸入非法!"); return;num1=num1/num2;else if(cos)num1=Math.cos(num1);else if(sin)num1=Math.sin(num1);else if(jiecheng)num1=funjiecheng(num1);else if(pow)num
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 油氣田智能化開發(fā)與管理系統(tǒng)建設方案
- 機場貴賓廳吧臺設計與施工合同范本
- 美食廣場經營權轉讓合同
- 知識產權采購合同中專利授權及糾紛解決條款
- 車輛掛名權益保障及免責責任明確協議
- 彩鋼結構簡易搭建與環(huán)保評估合同
- 環(huán)保產業(yè)財務合同環(huán)保技術投資與運營管理合同
- 出租車企業(yè)智能化調度司機合作協議
- 經銷白酒招商方案
- 企業(yè)四新培訓課件
- 國家開放大學機考答案 5個人與團隊管理2025-06-21
- 大慶師范學院《跳高》2023-2024學年第一學期期末試卷
- 2025年廣元市中考語文試卷真題(含標準答案)
- 2025年山西省中考語文試卷真題(含答案解析)
- 農機下鄉(xiāng)活動方案
- 幸福與健康課件
- 幼兒弱視防治指南
- 2025人教英語初中七年級下冊期末測試卷(含答案)
- 窗簾實施方案(3篇)
- 主管輪值管理制度
- 2025-2030中國豆腐產業(yè)消費趨勢及未來發(fā)展預測分析報告
評論
0/150
提交評論