Java实验报告
学院:计算机科学与技术学院
班级: 软件工程 班
学号:
姓名:
一、实验名称:算术测试
二、实验目的:学习处理ActionEvent事件
三、实验要求:编写一个算术测试小软件,用来训练小学生的算术能力。程序有3个类组成,其中Teacher对象充当监视器,负责给出算术题目,并判断回答者的答案者的答案是否正确。ComputerFrame对象负责为算术题目提供视图,比如用户可以通过ComputerFrame对象提供的GUI界面看到题目,并通过该GUI界面给出题目的答案;MainClass是软件的主类。
四、实验过程:
package cn.edu.xust.qingzhi;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class MainClass {
public static void main(String[] args)
{
ComputerFrame frame;
frame=new ComputerFrame();
frame.setTitle("算术测试");
frame.setBounds(100,100,650,180);
}
}
class ComputerFrame extends JFrame {
JMenuBar menubar;
JMenu choiceGrade;
JMenuItem grade1,grade2;
JTextField textOne,textTwo,textResult;
JButton getProblem,giveAnwser;
JLabel operatorLabel,message;
Teacher teacherZhang;
ComputerFrame()
{
teacherZhang=new Teacher();
teacherZhang.setMaxInteger(20);
setLayout(new FlowLayout());
menubar=new JMenuBar();
choiceGrade=new JMenu("选择级别");
grade1=new JMenuItem("幼儿级别");
grade2=new JMenuItem("儿童级别");
grade1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
teacherZhang.setMaxInteger(10);
}
});
grade2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
teacherZhang.setMaxInteger(50);
}
});
choiceGrade.add(grade1);
choiceGrade.add(grade2);
menubar.add(choiceGrade);
setJMenuBar(menubar);
textOne=new JTextField(5);//【代码1】创建textone,其可见字符长是5;
textTwo=new JTextField(5);
textResult=new JTextField(5);
operatorLabel=new JLabel("+");
operatorLabel.setFont(new Font("Arial",Font.BOLD,20));
message=new JLabel("你还没有回答呢");
getProblem=new JButton("获取题目");
giveAnwser=new JButton("确认答案");
add(getProblem);
add(textOne);
add(operatorLabel);
add(textTwo);
add(new JLabel("="));
add(textResult);
add(giveAnwser);
add(message);
textResult.requestFocus();
textOne.setEditable(false);
textTwo.setEditable(false);
getProblem.setActionCommand("getProblem");
textResult.setActionCommand("anwser");
giveAnwser.setActionCommand("anwser");
teacherZhang.setJTextField(textOne,textTwo,textResult);
teacherZhang.setJLabel(operatorLabel,message);
getProblem.addActionListener(teacherZhang); //【代码2】将teacherZhang注册为getProblem的ActionEvent事件监听器
giveAnwser.addActionListener(teacherZhang);//【代码3】将teacherZhang注册为giveAnwser的ActionEvent事件监听器
textResult.addActionListener(teacherZhang);//【代码4】将teacherZhang注册为textResult的ActionEvent事件监听器
setVisible(true);
validate();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
}
class Teacher implements ActionListener {
int numberOne,numberTwo;
String operator="";
boolean isRight;
Random random; //用于给出随机数
int maxInteger; //题目中最大的整数
JTextField textOne,textTwo,textResult;
JLabel operatorLabel,message;
Teacher(){
random=new Random();
}
public void setMaxInteger(int n){
maxInteger=n;
}
public void actionPerformed(ActionEvent e){
String str=e.getActionCommand();
if(str.equals("getProblem")){
numberOne=random.nextInt(maxInteger)+1;//1到maxInteger之间的随机数
numberTwo=random.nextInt(maxInteger)+1;
double d=Math.random(); //获取0-1之间的随机数
if(d>=0.5)
operator="+";
else
operator="-";
textOne.setText(""+numberOne);
textTwo.setText(""+numberTwo);
operatorLabel.setText(operator);
message.setText("请回答:");
textResult.setText(null);
}
else if(str.equals("anwser")){
String anwser=textResult.getText();
try{
int result=Integer.parseInt(anwser);
if(operator.equals("+")){
if(result==numberOne+numberTwo)
message.setText("你回答正确");
else
message.setText("你回答错误");
}
else if (operator.equals("-")){
if(result==numberOne-numberTwo)
message.setText("你回答正确");
else
message.setText("你回答错误");
}
}
catch(NumberFormatException ex){
message.setText("请输入数字字符");
}
}
}
public void setJTextField(JTextField...t){
textOne=t[0];
textTwo=t[1];
textResult=t[2];
}
public void setJLabel(JLabel...label){
operatorLabel=label[0];
message=label[1];
}
}
五、实验结果:
六、实验总结:通过这次的实验我学会了组件的属性和功能,还有处理组件上发生的界面事件,其中还要掌握事件源,监视器,处理事件的接口等等。学会了使用随机函数。
第二篇:java实验报告三
西安郵電學院
《java编程及应用》课内实验报告
题 目: 异常类和多线程
院系名称: 继职学院
班 学号(8位): 3094202017
指导教师: 张 荣
设计起止时间: 20xx年5月 12日
一、设计目的
1.掌握java面向对象程序设计的基本过程,加深对java基本语法,方法的了解;
2、掌握java语言中异常类中捕获异常和定义自己的异常类
3、关于多线程:必须先了解多线程的概念,然后了解Thread类和Runnable接口。
二.设计内容
1.设计银行类中存钱和取钱时的异常处理。
2.随便选择两个城市作为预选旅游目标。实现两个线程分别显示10次城市名。每次显示后休眠一段时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口,和Thread类实现。
3·创建两个不同优先级的线程,都从1数到1000,看看哪个数得快。
三、设计与实现过程
1·设计银行类中,存钱与取钱时异常的处理机制
2·随便选择两个城市作为预选旅游目标,实现两个独立线程分别显示10次城市名,每次显示后休眠一段时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用runnable接口,和Thread类实现。
3·创建两个不同优先级的线程,都从1数到1000,看看哪个数得快。
四.测试数据及运行结果
1.
2.
3.
五.调试情况,设计技巧及体会
调试情况和心得体会
1·该设计总体上收获不小,对java有了更深的了解,实现了银行家算法的异常处理机制。和简单的多线程的编程,我觉得在编写代码方面还有待加强,在编写时应更加细心,仔细修改错误
2·在设计过程中对java语言基本语法还不够熟悉,如,继承类和接口的运用。还有一些构造方法的编写不太到位
3.在传递参数时弄得不太清楚,应当多读这方面的代码,增强编程能力。
六.附录:源代码(电子版)
1. 设计银行类中,存钱与取钱时异常的处理机制
public class SaveAndFetchMoney
{
public static void main(String[]args) { int num_of_save=20; int num_of_fetch=100; Thread[]thread=new Thread[num_of_save]; Thread[]thread1=new Thread[num_of_fetch]; Account t=new Account("007",10000); for(int i=0;i<num_of_save;i++) { thread[i]=new Thread(new thread[i].start(); SaveMoney(t,(float)Math.random()*1000)); } for(int i=0;i<num_of_fetch;i++) {
thread1[i]=new Thread(new thread1[i].start(); FetchMoney(t,(float)Math.random()*1000)); } for(int i=0;i<num_of_save;i++) { } for(int i=0;i<num_of_fetch;i++) { } try { thread1[i].join(); }catch(InterruptedException e) {e.printStackTrace();} try{ thread[i].join(); }catch(InterruptedException e) {e.printStackTrace();}
System.out.println("账号:"+t.accountName+"最终金额为:
"+t.balance);
}
class Account
{
{
System.out.print("账号:"+this.accountName+"余额为:"+this.balance); balance+=amount; String accountName; float balance; Account(String accountName, int value) { } this.accountName=accountName; this.balance=value; } public synchronized void add(float amount)
System.out.println("您存入:"+amount+"元后,现在金额为:"+this.balance); }
public synchronized void sub(float amount)
{
if (balance>=amount) {
System.out.print("账号:"+this.accountName+"余额为:
"+this.balance);
balance-=amount;
System.out.println("您取出:"+amount+"元后,现在金额为:
"+this.balance);
} else { System.out.println("账号:"+this.accountName+"余额为:
"+this.balance+"您要取出的金额为:"+amount+"余额不足!无法完成取款操作!"); }
}
}
class SaveMoney implements Runnable
{
{
Account a; float amount; FetchMoney(Account a,float amount) { } public void run() { try{ Thread.sleep((int)Math.random()*2000); this.a=a; this.amount=amount; Account a; float amount; SaveMoney(Account a,float amount) { } public void run() { } try{ Thread.sleep((int)Math.random()*2000); }catch (InterruptedException e) { e.printStackTrace(); System.out.println(e.toString()); this.a=a; this.amount=amount; } a.add(amount); class FetchMoney implements Runnable
}
} } }catch (InterruptedException e) { e.printStackTrace(); System.out.println(e.toString()); } a.sub(amount);
2·随便选择两个城市作为预选旅游目标,实现两个独立线程分别显示10次城市名,每次显示后休眠一段时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用runnable接口,和Thread类实现。
public class City extends Thread
{
}
public class City2 implements Runnable
{
private String name; public City2(String name) { this.name = name; private String name; public City(String name) { } public void run() { } int i=1; while(i<11) { } System.out.println("我最后真的下定决心决定去"+name); System.exit(0); System.out.println("我选择去"+name+"这是我第"+i+"次选择"); try { i++; Thread.sleep((int)(Math.random()*1000)); } catch (InterruptedException e) {} this.name = name;
} } public void run() { int i=1; while(i<11) { } System.out.println("我选择去"+name+"这是我的第"+i+"次选择"); try { Thread.sleep((int)(Math.random()*1000)); i++; } catch (InterruptedException e) {} System.out.println("我最后真的下决心决定去"+name); System.exit(0); }
public class demoC
{
} public static void main(String[] arg) { } choseCity thread1 = new choseCity("上海"); City2 c2 = new City2("深圳"); Thread thread2 = new Thread(c2); thread1.start(); thread2.start();
3·创建两个不同优先级的线程,都从1数到1000,看看哪个数得快。 public class hightThread extends Thread
{
public void run() { for(int i=1;i<=1000;i++) { System.out.println("高优先级线程数的数:"+i); if(i==1000) { System.out.println("高优先级线程已经把数数完了!"); }
} } }
public class demoThread
{
}
public class lowThread extends Thread {
} public void run() { } for(int i=1;i<=1000;i++) { } System.out.println("低优先级线程数的数:"+i); if(i==1000) { System.out.println("低优先级线程已经把数数完了!"); } public static void main(String[] arg) { } hightThread ht = new hightThread(); ht.setPriority(Thread.MAX_PRIORITY); lowThread t = new ht.start(); t.start(); lowThread(); t.setPriority(Thread.MIN_PRIORITY);