1/3 課堂練習(xí)
第一次作業(yè)第二次作業(yè)第三次作業(yè)第四次作業(yè)第五次作業(yè)第六次作業(yè) 設(shè)計(jì)圖第六次作業(yè)_代碼圖第七次作業(yè)_設(shè)計(jì)圖第七次作業(yè)_代碼圖
https://wwb.lanzouh.com/iVkRQ05dr59a
https://wwb.lanzouh.com/iGv9p05drbna
鏈接:https://pan.baidu.com/s/1yEBHgGGm1D5OnEPguH5wBw 提取碼:njup
2/3 實(shí)驗(yàn)報(bào)告
實(shí)驗(yàn)一:綜合圖形界面程序設(shè)計(jì)
- 實(shí)驗(yàn)?zāi)康暮鸵?/li>
學(xué)習(xí)和理解 JAVA SWING 中的容器,部件嘀趟,布局管理器和部件事件處理方法勇皇。通過(guò)編寫(xiě)和調(diào)試程序枪眉,掌握 JAVA 圖形界面程序設(shè)計(jì)的基本方法嗜愈。
- 實(shí)驗(yàn)環(huán)境
PC 微型計(jì)算機(jī)系統(tǒng),Microsoft Windows 操作系統(tǒng),SUN Java Development Kit開(kāi)發(fā)工具包访得,NetBeans 開(kāi)發(fā)工具咸作。
- 實(shí)驗(yàn)原理及內(nèi)容
設(shè)計(jì)和編寫(xiě)一個(gè)用于將人民幣轉(zhuǎn)換為等值的美元的程序昵骤,界面要求可以輸入人民幣的金額并可以得到轉(zhuǎn)換后的結(jié)果。
1交惯、軟件工具的選擇
MyEclipse 10
首先次泽,導(dǎo)入需要用到的包(界面功能+按鈕功能)
import java.awt.*; //引入 AWT 包
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.*; //引入 swing 包
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
2、分析席爽,設(shè)計(jì)意荤,編寫(xiě)
人民幣轉(zhuǎn)換程序主要分為四個(gè)部分,第一個(gè)部分是界面的初始化只锻,第二個(gè)部分是輸入金額區(qū)域玖像,第三個(gè)部分是輸出金額區(qū)域,第四個(gè)部分是按鈕事件區(qū)域齐饮。
第一部分捐寥,利用 setBounds()函數(shù)設(shè)置界面布局的位置及寬高,利用 setBorder(new EmptyBorder) 函數(shù)設(shè)置邊框沈矿,利用 setLayout(new BorderLayout) 設(shè)置內(nèi)間距上真,利用 setLayout(new GridLayout)設(shè)置界面網(wǎng)格的布局。其他設(shè)置還包括:窗體標(biāo)題羹膳、退出操作睡互。
private JPanel contentPane; //總面板
private JTextField textField1; //單行文本域 1
private JTextField textField2; //單行文本域 2
private String money; //人民幣字符串
setTitle("轉(zhuǎn)換器"); //設(shè)置窗體標(biāo)題
setBounds(500, 200, 450, 350); //布局設(shè)置(x,y陵像,寬就珠,高)
setDefaultCloseOperation(EXIT_ON_CLOSE); //設(shè)置用戶在發(fā)起 “close” 時(shí)執(zhí)行退出操作
contentPane = new JPanel(); //面板對(duì)象
contentPane.setBorder(new EmptyBorder(5,5,5,5)); //線邊框(上厚,左厚醒颖,下厚妻怎,右厚)
contentPane.setLayout(new BorderLayout(0,0)); //邊界布局(橫距,縱距)
setContentPane(contentPane); //設(shè)置 Content Pane
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3,1,5,5)); //總網(wǎng)格布局(行數(shù)泞歉,列數(shù)逼侦,橫距匿辩,縱距)
contentPane.add(panel);
第二、三部分榛丢,利用 setBorder (new EtchedBorder) 設(shè)置邊框類型铲球,利用 setFont(new Font) 設(shè)置字體類型,利用 setLayout(new GridLayout)設(shè)置子網(wǎng)格的布局晰赞,利用 new JLabel()設(shè)置提示文本稼病,利用 setColumns()設(shè)置輸入輸出文本長(zhǎng)度。利用 addFocusListener(new FocusListener() ) 設(shè)置事件監(jiān)聽(tīng)器掖鱼,從而傳入輸入文本然走。
JPanel panel1 = new JPanel();
panel.add(panel1);
panel1.setBorder(new EtchedBorder(EtchedBorder.LOWERED,null,null)); //蝕刻邊框
panel1.setLayout(new GridLayout(1,2,5,5)); //網(wǎng)格布局
JLabel label = new JLabel("轉(zhuǎn)換前 RMB(¥):"); //提示文本設(shè)置
label.setFont(new Font("微軟雅黑", Font.PLAIN, 16)); //提示字體設(shè)置
panel1.add(label);
textField1 = new JTextField();
textField1.setFont(new Font("微軟雅黑",Font.PLAIN,16)); //輸入字體設(shè)置
panel1.add(textField1);
textField1.setColumns(10); //輸入長(zhǎng)度設(shè)置
textField1.addFocusListener(new FocusListener() {
public void focusLost(FocusEvent e) {
money = textField1.getText(); //輸入文本設(shè)置
}
public void focusGained(FocusEvent e) { }
});
JPanel panel2 = new JPanel();
panel.add(panel2);
panel2.setBorder(new EtchedBorder(EtchedBorder.LOWERED,null,null)); //蝕刻邊框
panel2.setLayout(new GridLayout(1,2,5,5)); //網(wǎng)格布局
JLabel label1 = new JLabel("轉(zhuǎn)換后 dollor($):"); //提示文本設(shè)置
label1.setFont(new Font("微軟雅黑", Font.PLAIN, 16)); //提示字體設(shè)置
panel2.add(label1);
textField2 = new JTextField();
textField2.setFont(new Font("微軟雅黑",Font.PLAIN,16)); //輸出字體設(shè)置
panel2.add(textField2);
textField1.setColumns(10); //輸出長(zhǎng)度設(shè)置
第四部分,利用 setBorder (new EtchedBorder) 設(shè)置邊框類型戏挡,利用 setFont(new Font) 設(shè)置字體類型芍瑞,利用 new JButton () 設(shè)置按鈕文本。再利用 addActionListener(new ActionListener()) 設(shè)置按鈕事件增拥,從而輸出轉(zhuǎn)換后的文本啄巧。
JPanel panel3 = new JPanel();
panel.add(panel3);
panel3.setBorder(new EtchedBorder(EtchedBorder.LOWERED,null,null)); //蝕刻邊框
JButton jb1 = new JButton("轉(zhuǎn)換"); //按鈕文本設(shè)置
jb1.setFont(new Font("微軟雅黑",Font.PLAIN,16)); //按鈕字體設(shè)置
panel3.add(jb1);
jb1.addActionListener(new ActionListener() { //輸出文本設(shè)置
public void actionPerformed(ActionEvent e) {
textField2.setText(Double.toString(Double.parseDouble(money)*0.1518));
}
});
setVisible(true);
3寻歧、調(diào)試
實(shí)驗(yàn)二:指針式時(shí)鐘程序
- 實(shí)驗(yàn)?zāi)康暮鸵?/li>
本實(shí)驗(yàn)旨在通過(guò)實(shí)驗(yàn)掌栅,培養(yǎng)學(xué)生將 JAVA 線程的相關(guān)知識(shí)點(diǎn)(包括線程調(diào)度,線程同步等)有機(jī)結(jié)合并加以綜合應(yīng)用码泛,在實(shí)驗(yàn)中設(shè)計(jì)多線程程序的能力猾封。
- 實(shí)驗(yàn)環(huán)境
PC 微型計(jì)算機(jī)系統(tǒng),Microsoft Windows 操作系統(tǒng),SUN Java Development Kit開(kāi)發(fā)工具包,NetBeans 開(kāi)發(fā)工具噪珊。
- 實(shí)驗(yàn)原理及內(nèi)容
設(shè)計(jì)和編寫(xiě)一個(gè)編寫(xiě)一個(gè)指針式時(shí)鐘程序晌缘,應(yīng)用線程實(shí)現(xiàn)時(shí)鐘的走動(dòng)。
import java.awt.*;
import javax.swing.*;
import java.util.*;
一痢站、Clock() 主函數(shù)
繼承 JFrame 類磷箕,創(chuàng)建一個(gè) Layout 對(duì)象
public class Clock extends JFrame{
public static void main(String[]s) {
new Layout();
}
}
二、Layout() 添加窗口和組件
繼承 JFrame 類阵难,創(chuàng)建一個(gè) Clockpaint 對(duì)象(坐標(biāo):20岳枷,20,半徑 170)
添加對(duì)象到容器呜叫,并初始化組件和時(shí)鐘窗口空繁。
class Layout extends JFrame { //添加窗口和時(shí)鐘組件
public Layout() {
ClockPaint cp = new ClockPaint(20, 20, 170);
add(cp); //添加組件到容器JFrame里
setBounds(260, 120, 400, 400); //設(shè)置組件的大小和位置
setResizable(false); //設(shè)置窗口不可調(diào)整大小
this.setTitle("指針式時(shí)鐘"); //設(shè)置窗口標(biāo)題
this.setVisible(true); //設(shè)置窗口可見(jiàn)
}
}
三、ClockPaint 定義時(shí)鐘組件
繼承 JFrame 類朱庆,預(yù)定義 Runnable 接口盛泡。
首先定義秒、分娱颊、時(shí)傲诵,以及弧度四個(gè)參數(shù)凯砍。
class ClockPaint extends JPanel implements Runnable { //定義時(shí)鐘組件
int x, y, r; //時(shí)鐘的位置坐和半徑
int h, m, s; //小時(shí),分鐘,秒
double rad = Math.PI / 180; //定義弧度
1、ClockPaint 構(gòu)造函數(shù)
構(gòu)造 Clockpaint 對(duì)象拴竹,對(duì)參數(shù) s果覆、m、h 賦值殖熟。然后創(chuàng)建并啟動(dòng)線程局待。
public ClockPaint(int x, int y, int r) { //構(gòu)造函數(shù)
this.x = x;
this.y = y;
this.r = r;
Calendar now = Calendar.getInstance(); //初始化H歷對(duì)象
s = now.get(Calendar.SECOND) * 6; //獲得初始秒轉(zhuǎn)換成度數(shù)
m = now.get(Calendar.MINUTE) * 6; //獲得初始分轉(zhuǎn)換成度數(shù)
h = (now.get(Calendar.HOUR_OF_DAY) - 12) * 30
+ now.get(Calendar.MINUTE) * 6 / 12; //獲得初始小時(shí)轉(zhuǎn)換成度數(shù)加分鐘實(shí)現(xiàn)連貫
Thread t = new Thread(this); //新建線程
t.start(); //啟動(dòng)線程
}
2、paint 畫(huà)鐘函數(shù)
設(shè)置背景色菱属,邊框色钳榨,畫(huà)邊框,畫(huà)一共12個(gè)刻度每個(gè)相隔30度纽门。
然后就是畫(huà)三條針薛耻, 長(zhǎng)度各不相同,對(duì)應(yīng)代碼如下:
x1 = (int) ((0.8 * r) * Math.sin(rad * s));
y1 = (int) ((0.8 * r) * Math.cos(rad * s));
x2 = (int) ((0.6 * r) * Math.sin(rad * s));
y2 = (int) ((0.6 * r) * Math.cos(rad * s));
x3 = (int) ((0.4 * r) * Math.sin(rad * s));
y3 = (int) ((0.4 * r) * Math.cos(rad * s));
最后在窗口數(shù)字顯示時(shí)間赏陵,用 drawString()函數(shù)饼齿。
public void paint(Graphics g) { //畫(huà)鐘
g.setColor(Color.BLACK); //背景色
g.fillRect(0, 0, r * 3, r * 3);
g.setColor(Color.WHITE); //邊框色
g.drawOval(x, y, r * 2, r * 2);
int d = 0;
int x1, y1, x2, y2;
for (int i = 0; i < 60; i++) { //每6度畫(huà)一個(gè)點(diǎn)
x1 = (int) ((r - 2) * Math.sin(rad * d));
y1 = (int) ((r - 2) * Math.cos(rad * d));
g.drawString("", x + r + x1 - 1, x + r - y1 + 1);
d += 6;
}
d = 30; //從30度開(kāi)始每30度畫(huà)一個(gè)數(shù)字和一條線
for (int i = 1; i <= 12; i++) {
x1 = (int) ((r - 14) * Math.sin(rad * d));
y1 = (int) ((r - 14) * Math.cos(rad * d));
g.drawString(i + "", x + r + x1 - 4, x + r - y1 + 5);
x1 = (int) ((r - 6) * Math.sin(rad * d));
y1 = (int) ((r - 6) * Math.cos(rad * d));
x2 = (int) ((r - 2) * Math.sin(rad * d));
y2 = (int) ((r - 2) * Math.cos(rad * d));
g.drawLine(x + r + x2, y+r - y2, x + r + x1, y + r - y1);
d += 30;
}
g.setColor(Color.RED); //按時(shí)間畫(huà)秒針
x1 = (int) ((0.8 * r) * Math.sin(rad * s));
y1 = (int) ((0.8 * r) * Math.cos(rad * s));
g.drawLine(x + r, y + r, x + r + x1, y + r - y1);
g.setColor(Color.BLUE); //按時(shí)間畫(huà)分針
x1 = (int) ((0.6 * r) * Math.sin(rad * m));
y1 = (int) ((0.6 * r) * Math.cos(rad * m));
g.drawLine(x + r, y + r, x + r + x1, y + r - y1);
g.setColor(Color.YELLOW); //按時(shí)間畫(huà)時(shí)針
x1 = (int) ((0.4 * r) * Math.sin(rad * h));
y1 = (int) ((0.4 * r) * Math.cos(rad * h));
g.drawLine(x + r, y + r, x + r + x1, y + r - y1);
g.setColor(Color.GREEN); //顯示時(shí)間
Calendar nowl = Calendar.getInstance();
g.drawString(nowl.get(Calendar.HOUR_OF_DAY) + ":" +
nowl.get(Calendar.MINUTE) + ":" +
nowl.get(Calendar.SECOND), 0, 20);
}
3、run 運(yùn)行函數(shù)
啟動(dòng)時(shí)鐘蝙搔,線程設(shè)置每 1000 毫秒刷新一次缕溉。
秒針每次走6度,秒針吃型、分針证鸥、時(shí)針每走完 360 度重置一次。
public void run() { //運(yùn)行時(shí)鐘
while (true) {
try {
Thread.sleep(1000);
} catch (Exception ex) {
System.out.println(ex);
}
s += 6; //秒針走6度
if (s >= 360) { //秒針走完1分鐘后重置
s = 0;
m += 6;
if (m >= 360) { //分針走完1小時(shí)后重置
m = 0;
h += 6;
}
if (h >= 360) { //時(shí)針走完12小時(shí)后重置
h = 0;
}
}
this.repaint(); //重新繪制時(shí)鐘
}
}
JFrame是官方提供的一個(gè)類勤晚,該類可以快速的開(kāi)發(fā)出Java界面應(yīng)用程序(c/s架構(gòu))枉层,屬于java.swing知識(shí)體系;它是屏幕上window的對(duì)象赐写,能夠最大化鸟蜡、最小化、關(guān)閉挺邀。
1.JFrame()創(chuàng)建一個(gè)無(wú)標(biāo)題的窗口
2.JFrame(String s)創(chuàng)建標(biāo)題為s的窗口
3.public void setSize(int width揉忘,int height)設(shè)置窗口大小4.public void setLocation(int x,int y)設(shè)置窗口位置悠夯,默認(rèn)位置為(0癌淮,0)
5.public void setBounds(int a,int b沦补,int width乳蓄,int height)設(shè)置窗口的初始位置是(a,b)夕膀,即距離屏幕左邊a個(gè)像素虚倒,巨離屏幕上方b個(gè)像素美侦,窗口的寬是width,高是height
6.public void setBackgorund(color.red)設(shè)置窗體背景顏色
7.public void setVisible(boolean b)設(shè)置窗口是否可見(jiàn)魂奥,默認(rèn)窗口是不可見(jiàn)的
8.public void setResizable(boolean b)設(shè)置窗口是否可調(diào)整大小菠剩,默認(rèn)窗口可調(diào)整大小
9.public void dispose()撤銷當(dāng)前窗口并釋放所有使用的資源
10.add(Component comp)向容器中增加組件
11.getContentpane()返回此窗口的容器對(duì)象
12.public void setExtendedState(int state)設(shè)置窗口的擴(kuò)展?fàn)顟B(tài),其中參數(shù)state取JFrame類中的下面類常量:MAXIMIZED_HORIZ(水平方向最大化)
MAXIMIZED_VERI(垂直方向最大化)
MAXIMIZED_BOYH(水平耻煤、垂直方向最大化)
13.public void setDefaultClose(int operation)設(shè)置單擊窗體右上角的關(guān)閉圖標(biāo)后具壮,程序會(huì)做出怎樣的處理參數(shù)operation取JFrame類中的下列int型static常量,程序根據(jù)參數(shù)operation取值做出不同的處理:DO_NOTHING_ON_CLOSE(什么也不做)
HIDE_ON_CLOSE(隱藏當(dāng)前窗口)
DISPOSE_ON_CLOSE(隱藏當(dāng)前窗口并釋放窗體占有的其他資源)
實(shí)驗(yàn)三:流處理程序設(shè)計(jì)
- 實(shí)驗(yàn)?zāi)康暮鸵?/li>
要求學(xué)生能在學(xué)習(xí)和理解課堂學(xué)習(xí)內(nèi)容中 JAVA 流編程理論的基礎(chǔ)上哈蝇,學(xué)習(xí)并逐步掌握 JAVA 流程序的編寫(xiě)和調(diào)試棺妓,學(xué)習(xí)根據(jù)處理需求對(duì)不同流的正確選擇使用和組合使用方法。
- 實(shí)驗(yàn)環(huán)境
PC 微型計(jì)算機(jī)系統(tǒng),Microsoft Windows 操作系統(tǒng),SUN Java Development Kit開(kāi)發(fā)工具包炮赦,NetBeans 開(kāi)發(fā)工具怜跑。
- 實(shí)驗(yàn)原理及內(nèi)容
設(shè)計(jì)和編寫(xiě)一個(gè)程序從鍵盤讀入一行字符串,將其寫(xiě)入一個(gè)文本文件中吠勘,再編寫(xiě)另一個(gè)程序從文本文件中讀入字符串并在命令行窗口顯示出來(lái)性芬。
import java.io.*;
import java.util.Calendar;
public class CalendarWR {
public static void main(String args[]){
// 將Calendar對(duì)象寫(xiě)入文件cal.ser
Calendar cal = Calendar.getInstance();
int h = cal.get(Calendar.HOUR_OF_DAY);
int m = cal.get(Calendar.MINUTE);
int s = cal.get(Calendar.SECOND);
String timeStr = h + ":" + m + ":" + s;
System.out.println("Time stored -> " + timeStr);
try{
FileOutputStream f=new FileOutputStream("cal.txt");
ObjectOutputStream o=new ObjectOutputStream(f);
o.writeObject(cal);
o.close();
f.close();
}
catch(IOException e){}
//從cal.ser文件讀出數(shù)據(jù)恢復(fù)Calendar對(duì)象
Calendar cal2 = null;
try{
FileInputStream f=new FileInputStream("cal.txt");
ObjectInputStream o=new ObjectInputStream(f);
cal2 = (Calendar)o.readObject();
o.close();
f.close();
}catch(IOException e){}
catch(ClassNotFoundException e){}
int hh = cal2.get(Calendar.HOUR_OF_DAY);
int mm = cal2.get(Calendar.MINUTE);
int ss = cal2.get(Calendar.SECOND);
String timeStr2 = hh + ":" + mm + ":" + ss;
System.out.println("Time recovered -> " + timeStr2);
}
}
實(shí)驗(yàn)四: 小應(yīng)用程序 Applet 設(shè)計(jì)
- 實(shí)驗(yàn)?zāi)康暮鸵?/li>
要求學(xué)生能在學(xué)習(xí)和理解課堂學(xué)習(xí)內(nèi)容中 JAVA 小應(yīng)用程序的基礎(chǔ)上,通
過(guò)實(shí)驗(yàn)剧防,培養(yǎng)學(xué)生將 JAVA 小應(yīng)用程序相關(guān)知識(shí)點(diǎn)(包括 JAVA Applet 和低級(jí)事件處理模型)有機(jī)結(jié)合植锉,設(shè)計(jì)基于 WEB 瀏覽器的小應(yīng)用程序的能力。
- 實(shí)驗(yàn)環(huán)境
PC 微型計(jì)算機(jī)系統(tǒng),Microsoft Windows 操作系統(tǒng),SUN Java Development Kit開(kāi)發(fā)工具包诵姜,IDEA 開(kāi)發(fā)工具汽煮。
- 實(shí)驗(yàn)原理及內(nèi)容
Java Applets Support 插件:https://plugins.jetbrains.com/plugin/13148-java-applets-support/versions
Edit Run Configuration
1搏熄、調(diào)用 paint()方法顯示小應(yīng)用程序
// 1棚唆、HelloWorld.java
import java.awt.*;
import java.applet.*;
public class HelloWorld extends Applet {
public void paint(Graphics g){
g.drawString("Hello World!", 25, 25);
}
}
2、簡(jiǎn)單的圖像測(cè)試
// 2心例、HwImage.java
import java.awt.*;
import java.applet.Applet;
public class HwImage extends Applet {
Image duke;
public void init() {
duke = getImage(getDocumentBase(), "images/duke.jpg");
}
public void paint(Graphics g) {
g.drawImage(duke, 25, 25, this);
}
}
<html>
<applet code=HwImage.class width=400 height=300>
<param name=duke value="images/duke.jpg">
</applet>
</html>
3宵凌、簡(jiǎn)單的聲音測(cè)試
// 3、HwLoop.java
import java.awt.Graphics;
import java.applet.*;
public class HwLoop extends Applet {
AudioClip sound;
public void init() {
sound = getAudioClip(getCodeBase(),"sounds/duke.mp3");
}
public void paint(Graphics g) {
g.drawString("Audio Test", 25, 25);
}
public void start() {
sound.loop();
}
public void stop() {
sound.stop();
}
}
<html>
<applet code=HwLoop.class width=400 height=300>
<param name=sound value="sounds/duke.mp3">
</applet>
</html>
4止后、設(shè)計(jì)和編寫(xiě)一個(gè)可以用鼠標(biāo)操作的 Applet 小應(yīng)用程序和相應(yīng)的 HTML 頁(yè)面瞎惫,觀察 Applet 的執(zhí)行過(guò)程,測(cè)試程序鼠標(biāo)用戶交互操作的效果译株。
// Mouseevent.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Mouseevent extends Applet implements ActionListener {
private static final long serialVersionUID = 1L;
Button b,c;
public void init() {
Label label=new Label("請(qǐng)按按鈕");
b=new Button("按鈕1");//構(gòu)造按鈕
b.addActionListener(this);//監(jiān)視器
c=new Button("按鈕2");
c.addActionListener(this);
add(label);
add(b);//將按鈕加入網(wǎng)頁(yè)
add(c);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b) {
Frame frame=new Frame("提醒");
frame.setSize(230,100);//設(shè)置大小
frame.setLocation(100,100);//設(shè)置位置
frame.add(new Label("你按了按鈕1"));//添加標(biāo)簽
frame.setVisible(true);//設(shè)置是否可見(jiàn)
frame.validate();
}
else if(e.getSource()==c) {
Frame frame=new Frame("提醒");
frame.setSize(230,100);
frame.setLocation(100,100);
frame.add(new Label("你按了按鈕2"));
frame.setVisible(true);
frame.validate();
}
}
}
<html>
<applet code=HwLoop.class width=400 height=400></applet>
</html>
3/3 期末大作業(yè)
- 大作業(yè)課題說(shuō)明
課題代號(hào): 2
課題名稱:TI程序員計(jì)算器程序
課題要求:① 基本要求:設(shè)計(jì)德州儀器程序員專用計(jì)算器(1977 年-1982 年發(fā)布)瓜喇。按照TI程序員計(jì)算器(1982 年LCD版本)的原始面板鍵盤和顯示布局設(shè)計(jì)出計(jì)算器的交互窗口,參照計(jì)算器的功能介紹歉糜,基本要求只需要實(shí)現(xiàn) 10 進(jìn)制和 16 進(jìn)制的基礎(chǔ)算術(shù)運(yùn)算乘寒。即不含括號(hào)的單步加減乘除運(yùn)算。
② 提高要求:參照計(jì)算器的功能介紹匪补,實(shí)現(xiàn)輸入的 10 進(jìn)制數(shù)和 16 進(jìn)制數(shù)之間的相互轉(zhuǎn)換伞辛;實(shí)現(xiàn)與烂翰、或、異或蚤氏、反碼甘耿、補(bǔ)碼和移位運(yùn)算;實(shí)現(xiàn)單一進(jìn)制模式下的帶括號(hào)的組合多步運(yùn)算竿滨;實(shí)現(xiàn)混合進(jìn)制模式下帶括號(hào)的組合多步運(yùn)算佳恬;設(shè)計(jì)運(yùn)算溢出等出錯(cuò)提示。
- 補(bǔ)充材料
- 實(shí)驗(yàn)原理及內(nèi)容
// Convert.java
package calculator;
public class Convert {
static int toInt(char str) {
switch (str) {
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case 'a':
return 10;
case 'b':
return 11;
case 'c':
return 12;
case 'd':
return 13;
case 'e':
return 14;
case 'f':
return 15;
}
return 0;
}
static int hex_convert(String s) {
int i;
int temp = 0;
for(i=s.length()-1;i>=0;i--)
temp += toInt(s.charAt(i))*Math.pow(16,s.length()-i-1);
return temp;
}
static int oct_convert(String s) {
int i;
int temp = 0;
for(i=s.length()-1;i>=0;i--)
temp += toInt(s.charAt(i))*Math.pow(8,s.length()-i-1);
return temp;
}
public static void main(String[] args){
// System.out.println(hex_convert("a7b"));
// System.out.println(oct_convert("57222"));
System.out.println(Integer.toHexString(0xff<<8));
}
}
// demo.java
package calculator;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import calculator.Convert;
class Calculator {
private Frame f = new Frame("計(jì)算器");
private Frame info = new Frame("對(duì)照表");
private Button[] b = new Button[16];
private Button[] cal = new Button[20];
private MenuBar mb = new MenuBar();
private Menu option = new Menu("Edit");
private Menu other = new Menu("Other");
private MenuItem about = new MenuItem("About");
private MenuItem reset = new MenuItem("Reset");
private MenuItem exit = new MenuItem("Exit");
private JLabel information = new JLabel();
private TextField text = new TextField(30);
private int flag = 0;
private int flag2 = -1;
private int operate = 0; //0:進(jìn)制轉(zhuǎn)換于游,1:加減乘除殿怜,2:邏輯運(yùn)算,3:位運(yùn)算
private int base = 0; //0:10進(jìn)制曙砂,1:16進(jìn)制头谜,2:8進(jìn)制
private int click = 0; //0:第一次設(shè)置進(jìn)制數(shù),1:第二次設(shè)置進(jìn)制數(shù)
private int Key = -1;
String load1 = new String();
String load2 = new String();
public void init(){
Panel p = new Panel();
NumListener numListen = new NumListener();
CalListener calListen = new CalListener();
load1 = load2 = null;
for(int i = 0; i <= 9; i++)
b[i] = new Button(""+i);
info.setSize(300,400);
information.setText("<html><p>Time:2022 / 5 / 18鸠澈,Author:Du1in9</p>"+
"<p>0x01:0001</p><p>0x02:0012</p><p>0x03:0011</p><p>0x04:0100</p>"+
"<p>0x05:0001</p><p>0x06:0012</p><p>0x07:0011</p><p>0x08:1000</p></p>"+
"<p>0x09:1001</p><p>0x0a:1010</p><p>0x0b:1011</p><p>0x0c:1100</p></p>"+
"<p>0x0d:1101</p><p>0x0e:1110</p><p>0x0f:1111</p><p>0x00:0000</p><p></html>");
for(int i = 0; i <= 9; i++) {
b[i].addActionListener(numListen);
b[i].setBackground(Color.pink);
}
b[10] = new Button("a");
b[11] = new Button("b");
b[12] = new Button("c");
b[13] = new Button("d");
b[14] = new Button("e");
b[15] = new Button("f");
b[10].setBackground(Color.pink);
b[11].setBackground(Color.pink);
b[12].setBackground(Color.pink);
b[13].setBackground(Color.pink);
b[14].setBackground(Color.pink);
b[15].setBackground(Color.pink);
b[10].addActionListener(numListen);
b[11].addActionListener(numListen);
b[12].addActionListener(numListen);
b[13].addActionListener(numListen);
b[14].addActionListener(numListen);
b[15].addActionListener(numListen);
b[10].addActionListener(numListen);
b[11].addActionListener(numListen);
b[12].addActionListener(numListen);
b[13].addActionListener(numListen);
b[14].addActionListener(numListen);
b[15].addActionListener(numListen);
cal[0] = new Button("+");
cal[1] = new Button("-");
cal[2] = new Button("*");
cal[3] = new Button("/");
cal[4] = new Button("=");
cal[5] = new Button("DE");
cal[6] = new Button("SHF");
cal[7] = new Button("AC");
cal[14] = new Button("DEC");
cal[15] = new Button("HEX");
cal[16] = new Button("OCT");
cal[17] = new Button("AND");
cal[18] = new Button("OR");
cal[19] = new Button("XOR");
cal[0].addActionListener(calListen); // +
cal[1].addActionListener(calListen); // -
cal[2].addActionListener(calListen); // *
cal[3].addActionListener(calListen); // /
cal[4].addActionListener(calListen); // =
cal[5].addActionListener(e->{ // DE
text.setText(text.getText().substring(0,text.getText().length()-1));
});
cal[6].addActionListener(calListen); // SHF
cal[7].addActionListener(e->{ // AC
load1 = load2 = null;
Key = -1;
flag = 0;
flag2 = -1;
operate = 0;
base = 0;
text.setText("");
});
cal[14].addActionListener(calListen); // DEC
cal[15].addActionListener(calListen); // HEX
cal[16].addActionListener(calListen); // OCT
cal[17].addActionListener(calListen); // AND
cal[18].addActionListener(calListen); // OR
cal[19].addActionListener(calListen); // XOR
p.add(b[1]);p.add(b[2]);p.add(b[3]);p.add(b[4]);p.add(cal[0]);p.add(cal[1]);
p.add(b[5]);p.add(b[6]);p.add(b[7]);p.add(b[8]);p.add(cal[2]);p.add(cal[3]);
p.add(b[9]);p.add(b[10]);p.add(b[11]);p.add(b[12]);p.add(cal[4]);p.add(cal[5]);
p.add(b[13]);p.add(b[14]);p.add(b[15]);p.add(b[0]);p.add(cal[6]);p.add(cal[7]);
p.add(cal[14]);p.add(cal[15]);p.add(cal[16]);p.add(cal[17]);p.add(cal[18]);p.add(cal[19]);
text.setEditable(false);
exit.addActionListener(e->System.exit(0));
about.addActionListener(e->{
info.setVisible(true);
});
option.add(reset);
option.add(exit);
other.add(about);
mb.add(option);
mb.add(other);
f.setMenuBar(mb);
f.add(text,BorderLayout.NORTH);
p.setLayout(new GridLayout(5,4,4,4));
info.add(information);
info.addWindowListener(new WindowListener());
f.addWindowListener(new WindowListener());
f.add(p);
f.setSize(560,500);
f.setVisible(true);
}
class WindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e){
if(e.getSource() == f)
System.exit(0);
else if(e.getSource() == info)
info.setVisible(false);
}
}
class NumListener implements ActionListener { //一柱告、輸入監(jiān)聽(tīng)
public void actionPerformed(ActionEvent e){
if(e.getSource() == b[0]){
if(flag2 == -1)
text.setText(text.getText()+"0");
else{
text.setText("0");
flag2 = -1;
}
}
else if(e.getSource() == b[1]){
if(flag2 == -1)
text.setText(text.getText()+"1");
else{
text.setText("1");
flag2 = -1;
}
}
else if(e.getSource() == b[2]){
if(flag2 == -1)
text.setText(text.getText()+"2");
else{
text.setText("2");
flag2 = -1;
}
}
else if(e.getSource() == b[3]){
if(flag2 == -1)
text.setText(text.getText()+"3");
else{
text.setText("3");
flag2 = -1;
}
}
else if(e.getSource() == b[4]){
if(flag2 == -1)
text.setText(text.getText()+"4");
else{
text.setText("4");
flag2 = -1;
}
}
else if(e.getSource() == b[5]){
if(flag2 == -1)
text.setText(text.getText()+"5");
else{
text.setText("5");
flag2 = -1;
}
}
else if(e.getSource() == b[6]){
if(flag2 == -1)
text.setText(text.getText()+"6");
else{
text.setText("6");
flag2 = -1;
}
}
else if(e.getSource() == b[7]){
if(flag2 == -1)
text.setText(text.getText()+"7");
else{
text.setText("7");
flag2 = -1;
}
}
else if(e.getSource() == b[8]){
if(flag2 == -1)
text.setText(text.getText()+"8");
else{
text.setText("8");
flag2 = -1;
}
}
else if(e.getSource() == b[9]){
if(flag2 == -1)
text.setText(text.getText()+"9");
else{
text.setText("9");
flag2 = -1;
}
}
else if(e.getSource() == b[10]){
if(flag2 == -1)
text.setText(text.getText()+"a");
else{
text.setText("a");
flag2 = -1;
}
}
else if(e.getSource() == b[11]){
if(flag2 == -1)
text.setText(text.getText()+"b");
else{
text.setText("b");
flag2 = -1;
}
}
else if(e.getSource() == b[12]){
if(flag2 == -1)
text.setText(text.getText()+"c");
else{
text.setText("c");
flag2 = -1;
}
}
else if(e.getSource() == b[13]){
if(flag2 == -1)
text.setText(text.getText()+"d");
else{
text.setText("d");
flag2 = -1;
}
}
else if(e.getSource() == b[14]){
if(flag2 == -1)
text.setText(text.getText()+"e");
else{
text.setText("e");
flag2 = -1;
}
}
else if(e.getSource() == b[15]){
if(flag2 == -1)
text.setText(text.getText()+"f");
else{
text.setText("f");
flag2 = -1;
}
}
}
}
class CalListener implements ActionListener { //二、操作監(jiān)聽(tīng)
public void actionPerformed(ActionEvent e){
if(e.getSource() == cal[0]){ //加法運(yùn)算
if(flag == 0){
load1 = text.getText();
flag = 1;
flag2 = Key = 0;
}
else if(flag == 1){
load2 = text.getText();
if(base == 0)
text.setText(String.valueOf(Integer.parseInt(load1) + Integer.parseInt(load2)));
else if(base == 1)
text.setText(Integer.toHexString(Convert.hex_convert(load1) + Convert.hex_convert(load2)));
else
text.setText(Integer.toOctalString(Convert.oct_convert(load1) + Convert.oct_convert(load2)));
load1 = text.getText();
load2 = null;
operate = 1;
}
}
else if(e.getSource() == cal[1]){ //減法運(yùn)算
if(flag == 0){
load1 = text.getText();
flag = 1;
flag2 = Key = 1;
}
else if(flag == 1){
load2 = text.getText();
if(base == 0)
text.setText(String.valueOf(Integer.parseInt(load1) - Integer.parseInt(load2)));
else if(base == 1)
text.setText(Integer.toHexString(Convert.hex_convert(load1) - Convert.hex_convert(load2)));
else
text.setText(Integer.toOctalString(Convert.oct_convert(load1) - Convert.oct_convert(load2)));
load1 = text.getText();
load2 = null;
operate = 1;
}
}
else if(e.getSource() == cal[2]){ //乘法運(yùn)算
if(flag == 0){
load1 = text.getText();
flag = 1;
flag2 = Key = 2;
}
else if(flag == 1){
load2 = text.getText();
if(base == 0)
text.setText(String.valueOf(Integer.parseInt(load1) * Integer.parseInt(load2)));
else if(base == 1)
text.setText(Integer.toHexString(Convert.hex_convert(load1) * Convert.hex_convert(load2)));
else
text.setText(Integer.toOctalString(Convert.oct_convert(load1) * Convert.oct_convert(load2)));
load1 = text.getText();
load2 = null;
operate = 1;
}
}
else if(e.getSource() == cal[3]){ //除法運(yùn)算
if(flag == 0){
load1 = text.getText();
flag = 1;
flag2 = Key = 3;
}
else if(flag == 1){
load2 = text.getText();
if(base == 0)
text.setText(String.valueOf(Integer.parseInt(load1) / Integer.parseInt(load2)));
else if(base == 1)
text.setText(Integer.toHexString(Convert.hex_convert(load1) / Convert.hex_convert(load2)));
else
text.setText(Integer.toOctalString(Convert.oct_convert(load1) / Convert.oct_convert(load2)));
load1 = text.getText();
load2 = null;
operate = 1;
}
}
else if(e.getSource() == cal[4]){ //等號(hào)
if(load1 != null && load2 == null){
load2 = text.getText();
if(Key == 0){ //加
if(base == 0)
text.setText(String.valueOf(Integer.parseInt(load1) + Integer.parseInt(load2)));
else if(base == 1)
text.setText(Integer.toHexString(Convert.hex_convert(load1) + Convert.hex_convert(load2)));
else
text.setText(Integer.toOctalString(Convert.oct_convert(load1) + Convert.oct_convert(load2)));
}
else if(Key == 1){ //減
if(base == 0)
text.setText(String.valueOf(Integer.parseInt(load1) - Integer.parseInt(load2)));
else if(base == 1)
text.setText(Integer.toHexString(Convert.hex_convert(load1) - Convert.hex_convert(load2)));
else
text.setText(Integer.toOctalString(Convert.oct_convert(load1) - Convert.oct_convert(load2)));
}
else if(Key == 2) { //乘
if(base == 0)
text.setText(String.valueOf(Integer.parseInt(load1) * Integer.parseInt(load2)));
else if(base == 1)
text.setText(Integer.toHexString(Convert.hex_convert(load1) * Convert.hex_convert(load2)));
else
text.setText(Integer.toOctalString(Convert.oct_convert(load1) * Convert.oct_convert(load2)));
}
else if(Key == 3) { //除
if(base == 0)
text.setText(String.valueOf(Integer.parseInt(load1) / Integer.parseInt(load2)));
else if(base == 1)
text.setText(Integer.toHexString(Convert.hex_convert(load1) / Convert.hex_convert(load2)));
else
text.setText(Integer.toOctalString(Convert.oct_convert(load1) / Convert.oct_convert(load2)));
}
else if(Key == 4) { //與
if(base == 0)
text.setText(String.valueOf(Integer.parseInt(load1) & Integer.parseInt(load2)));
else if(base == 1)
text.setText(Integer.toHexString(Convert.hex_convert(load1) & Convert.hex_convert(load2)));
else
text.setText(Integer.toOctalString(Convert.oct_convert(load1) & Convert.oct_convert(load2)));
}
else if(Key == 5){ //或
if(base == 0)
text.setText(String.valueOf(Integer.parseInt(load1) | Integer.parseInt(load2)));
else if(base == 1)
text.setText(Integer.toHexString(Convert.hex_convert(load1) | Convert.hex_convert(load2)));
else
text.setText(Integer.toOctalString(Convert.oct_convert(load1) | Convert.oct_convert(load2)));
}
else if(Key == 6){ //異或
if(base == 0)
text.setText(String.valueOf(Integer.parseInt(load1) ^ Integer.parseInt(load2)));
else if(base == 1)
text.setText(Integer.toHexString(Convert.hex_convert(load1) ^ Convert.hex_convert(load2)));
else
text.setText(Integer.toOctalString(Convert.oct_convert(load1) ^ Convert.oct_convert(load2)));
}
else if(Key == 7){ //左移
if(base == 0)
text.setText(Integer.toHexString(Integer.parseInt(load1)<<Integer.parseInt(load2)));
else if (base == 1)
text.setText(Integer.toHexString(Integer.parseInt(load1,16)<<Integer.parseInt(load2)));
else
text.setText(Integer.toHexString(Integer.parseInt(load1,8)<<Integer.parseInt(load2)));
}
load1 = text.getText();
load2 = null;
Key = -1;
flag = 0;
flag2 = -1;
base = 0;
operate = 0;
click = 0;
}
}
else if(e.getSource() == cal[6]){ //位運(yùn)算
if(flag == 0){
load1 = text.getText();
flag = 1;
flag2 = Key = 7;
}
else if(flag == 1){
load2 = text.getText();
if(base == 0)
text.setText(Integer.toHexString(Integer.parseInt(load1)<<Integer.parseInt(load2)));
else if (base == 1)
text.setText(Integer.toHexString(Integer.parseInt(load1,16)<<Integer.parseInt(load2)));
else
text.setText(Integer.toHexString(Integer.parseInt(load1,8)<<Integer.parseInt(load2)));
load1 = text.getText();
load2 = null;
operate = 2;
}
}
else if(e.getSource() == cal[14]) { //10進(jìn)制
load2 = text.getText();
if(click == 1 && operate ==0){
if(base == 0)
text.setText(load2);
else if (base == 1)
text.setText(String.valueOf(Integer.parseInt(load2,16)));
else
text.setText(String.valueOf(Integer.parseInt(load2,8)));
click--;
}
else
click++;
load1 = text.getText();
load2 = null;
base = 0;
}
else if(e.getSource() == cal[15]) { //16進(jìn)制
load2 = text.getText();
if(click == 1 && operate ==0){
if(base == 0)
text.setText(Integer.toHexString(Integer.parseInt(load2)));
else if (base == 1)
text.setText(load2);
else
text.setText(Integer.toHexString(Integer.valueOf(load2, 8)));
click--;
}
else
click++;
load1 = text.getText();
load2 = null;
base = 1;
}
else if(e.getSource() == cal[16]) { //8進(jìn)制
load2 = text.getText();
if(click == 1 && operate ==0){
if(base == 0)
text.setText(Integer.toOctalString(Integer.parseInt(load2)));
else if (base == 1)
text.setText(Integer.toOctalString(Integer.valueOf(load2, 16)));
else
text.setText(load2);
click--;
}
else
click++;
load1 = text.getText();
load2 = null;
base = 2;
}
else if(e.getSource() == cal[17]) { //與運(yùn)算
if(flag == 0){
load1 = text.getText();
flag = 1;
flag2 = Key = 4;
}
else if(flag == 1){
load2 = text.getText();
if(base == 0)
text.setText(String.valueOf(Integer.parseInt(load1) & Integer.parseInt(load2)));
else if(base == 1)
text.setText(Integer.toHexString(Convert.hex_convert(load1) & Convert.hex_convert(load2)));
else
text.setText(Integer.toOctalString(Convert.oct_convert(load1) & Convert.oct_convert(load2)));
load1 = text.getText();
load2 = null;
operate = 2;
}
}
else if(e.getSource() == cal[18]) { //或運(yùn)算
if(flag == 0){
load1 = text.getText();
flag = 1;
flag2 = Key = 5;
}
else if(flag == 1){
load2 = text.getText();
if(base == 0)
text.setText(String.valueOf(Integer.parseInt(load1) | Integer.parseInt(load2)));
else if(base == 1)
text.setText(Integer.toHexString(Convert.hex_convert(load1) | Convert.hex_convert(load2)));
else
text.setText(Integer.toOctalString(Convert.oct_convert(load1) | Convert.oct_convert(load2)));
load1 = text.getText();
load2 = null;
operate = 2;
}
}
else if(e.getSource() == cal[19]) { //異或運(yùn)算
if(flag == 0){
load1 = text.getText();
flag = 1;
flag2 = Key = 6;
}
else if(flag == 1){
load2 = text.getText();
if(base == 0)
text.setText(String.valueOf(Integer.parseInt(load1) ^ Integer.parseInt(load2)));
else if(base == 1)
text.setText(Integer.toHexString(Convert.hex_convert(load1) ^ Convert.hex_convert(load2)));
else
text.setText(Integer.toOctalString(Convert.oct_convert(load1) ^ Convert.oct_convert(load2)));
load1 = text.getText();
load2 = null;
operate = 2;
}
}
}
}
public static void main(String[] args){
new Calculator().init();
}
}