java計算器綜合實驗報告_第1頁
java計算器綜合實驗報告_第2頁
java計算器綜合實驗報告_第3頁
java計算器綜合實驗報告_第4頁
java計算器綜合實驗報告_第5頁
已閱讀5頁,還剩11頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

1、華北科技學院計算機學院綜合性實驗實 驗 報 告 課程名稱 JAVA程序設計 實驗學期 2012 至 2013 學年 第 1 學期學生所在系部 計算機學院 任課教師 白磊 實驗成績 計算機學院制JAVA程序設計課程綜合性實驗報告開課實驗室:基礎四 2012年12月09日實驗題目簡單計算器設計一、 實驗目的通過編寫Java的應用系統(tǒng)綜合實例簡單計算器軟件,總結(jié)、回顧和實踐面向?qū)ο蟮木幊趟枷胍约熬幊谭椒ǎ⑼ㄟ^編寫程序來掌握Java語言編程技巧,將學習到的知識融會貫通,同時提高調(diào)試程序的能力,養(yǎng)成良好的編程習慣,并增強對程序設計整體思路的把握。熟悉在集成開發(fā)環(huán)境下編寫Java程序。二、 設備與環(huán)境E

2、clipse、JDK開發(fā)包、集成開發(fā)環(huán)境三、 實驗內(nèi)容及代碼首先設計的是程序的結(jié)構(gòu)。程序中主要是一計算器公共類(implements ActionListener),包括構(gòu)造函數(shù)(用于計算器的外觀設置,布局)。在設計一個界面時先設計Frame容器,在north添加一個TextField,center添加一個Panel容器。再在Panel容器中添加兩個panel容器,第一個panel中添加“CE”(西端)、“backspace”(中間)、“C”(東端)三個按鈕。第二個panel容器中GridLaout把容器分成四行五列,添加1,2,3,4,5,6,7,8,9,0,+,-,*,/,%,+-,1/x

3、,sqrt,=, 這些按鈕。定義各個按鈕逐個添加給ActionListener監(jiān)聽器進行注冊。界面構(gòu)造號后接著定義算法。定義加減乘除,倒數(shù),開方等這些基本運算包含于actionPerformed方法(用于事件的響應),一個public static void main(String arg)程序運行入口。在main中創(chuàng)建一個計算器類對象,在創(chuàng)建對象的同時,初始化各組件,創(chuàng)建圖形用戶界面。代碼:import javax.swing.*;import java.awt.*;import java.awt.event.*; public class 計算器 implements ActionList

4、ener private boolean judge = true;private boolean flag = false;private String fh = ;private String num1 = ; private String num2 = ; JFrame f=null;JTextField tf;JButton b;public void jiemian()f=new JFrame(計算器);Container c=f.getContentPane();tf=new JTextField();tf.setHorizontalAlignment(JTextField.RIG

5、HT);c.add(tf,North);JPanel pn3=new JPanel(new BorderLayout(); c.add(pn3,Center); JPanel pn2=new JPanel(); pn2.setLayout(new BorderLayout(); JPanel pn1=new JPanel(); pn1.setLayout(new GridLayout(4,5); pn3.add(pn2,North); pn3.add(pn1);b=new JButton(CE);b.setForeground(Color.BLUE); b.addActionListener(

6、this);pn2.add(b,East);b=new JButton(C); b.setForeground(Color.BLUE); pn2.add(b,West);b=new JButton(backspace);b.setForeground(Color.BLUE); b.addActionListener(this); pn2.add(b,Center); b=new JButton(1); b.addActionListener(this); pn1.add(b); b=new JButton(2); b.addActionListener(this); pn1.add(b); b

7、=new JButton(3); b.addActionListener(this); pn1.add(b); b=new JButton(+);b.setForeground(Color.BLUE);b.addActionListener(this); pn1.add(b);b=new JButton(sqrt);b.setForeground(Color.BLUE);b.addActionListener(this); pn1.add(b);b=new JButton(4); b.addActionListener(this); pn1.add(b); b=new JButton(5);

8、b.addActionListener(this); pn1.add(b); b=new JButton(6); b.addActionListener(this); pn1.add(b); b=new JButton(-); b.setForeground(Color.BLUE); b.addActionListener(this); pn1.add(b); b=new JButton(%); b.setForeground(Color.BLUE); b.addActionListener(this); pn1.add(b);b=new JButton(7); b.addActionList

9、ener(this); pn1.add(b); b=new JButton(8); b.addActionListener(this); pn1.add(b); b=new JButton(9); b.addActionListener(this); pn1.add(b); b=new JButton(*); b.setForeground(Color.BLUE); b.addActionListener(this); pn1.add(b); b=new JButton(1/x); b.setForeground(Color.BLUE); b.addActionListener(this);

10、pn1.add(b);b=new JButton(0); b.addActionListener(this); pn1.add(b); b=new JButton(.); b.addActionListener(this); pn1.add(b); b=new JButton(+/-); b.setForeground(Color.RED); b.addActionListener(this); pn1.add(b); b=new JButton(/); b.setForeground(Color.BLUE); b.addActionListener(this); pn1.add(b); b=

11、new JButton(=); b.setForeground(Color.BLUE); b.addActionListener(this); pn1.add(b); f.setSize(325,325); f.setVisible(true); public void actionPerformed(ActionEvent e)String temp = e.getActionCommand();if(!(.indexOf(temp)=-1) if(flag) String s=tf.getText(); tf.setText(s+temp); else tf.setText(temp);

12、flag = true; else if(!(+-*/.indexOf(temp)=-1) if(judge) num1 = tf.getText(); flag = false; judge = false; tf.setText(); else flag = false; num2 = tf.getText();if(!num1.equals() & !num2.equals() double i1 = Double.parseDouble(num1); double i2 = Double.parseDouble(num2); if(+.equals(fh) tf.setText(Dou

13、ble.toString(i1+i2); flag = false; else if(-.equals(fh) tf.setText(Double.toString(i1-i2); flag = false; else if(*.equals(fh) tf.setText(Double.toString(i1*i2); flag = false; else if(/.equals(fh) tf.setText(Double.toString(i1/i2); flag = false; num1 = tf.getText();num2 = (); fh = temp; else if(=.equ

14、als(temp) num2 = tf.getText(); if(!num1.equals() & !num2.equals() double i1 = Double.parseDouble(num1); double i2 = Double.parseDouble(num2); if(+.equals(fh) tf.setText(Double.toString(i1+i2); flag = false; else if(*.equals(fh) tf.setText(Double.toString(i1*i2); flag = false; else if(-.equals(fh) tf

15、.setText(Double.toString(i1-i2); flag = false; else if(/.equals(fh) tf.setText(Double.toString(i1/i2); flag = false; num2 = tf.getText();num1=(); else if(+/-.equals(temp) String s = tf.getText(); if(!s.startsWith(-) tf.setText(-+s); else tf.setText(s.substring(1); flag=false; else if(sqrt.equals(tem

16、p) String s =tf.getText(); if (!s.startsWith(-) tf.setText(Double.toString(Math.sqrt(Double. parseDouble(tf.getText(); else tf.setText(Error); flag=false; else if(1/x.equals(temp) String s=tf.getText(); double d=Double.parseDouble(s); double d1=1/d; tf.setText(Double.toString(d1); flag=false; else i

17、f(%.equals(temp) String s=tf.getText(); double d=Double.parseDouble(s); double d1=d*100; tf.setText(Double.toString(d1)+%); flag=false; else if(CE.equals(temp) tf.setText(); num2 = ; flag = false; else if(C.equals(temp) tf.setText(); num1 = ; num2 = ; flag = false; judge = true; else if(backspace.eq

18、uals(temp) String s = tf.getText(); if(s.length()0)s = s.substring(0,s.length()-1); tf.setText(s); flag = true; else tf.setText(); flag = false; public static void main(String args) new 計算器().jiemian(); 界面如圖:四、實驗結(jié)果及分析 能實現(xiàn)的功能: 1、支持“+”、“”、“”、“”運算,以及開平方根(按“sqart”),將結(jié)果轉(zhuǎn)換為百分數(shù)(按“%”),取倒運算(按“1/x”)。2、支持浮點數(shù)表示(運算)例如:0.85+0.25=1.103、連續(xù)操作 例如:1+2+3=64、得出結(jié)果后不用清0進行運算 例如:4+52=14 +5=195、backspace:退格鍵,刪除一個數(shù)字6、C鍵:清除當前計算,開始新的運算。7、CE鍵:屏幕清零鍵,清除當前數(shù)字,可以重新輸入按CE前輸入的數(shù)字??偨Y(jié)與體會: 實驗達到了預期的目標。通過此次的編程,我學會了用Java語言編寫簡單的軟件,增強了我對學習Java的興趣。編程過程中,總體設計主要是窗口與組件的應用,具體執(zhí)行則是利用Java語言設計算法,而難點也在算法的設計上。雖然程

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論