java入门程序题及源码5

上传人:大米 文档编号:548268091 上传时间:2023-05-05 格式:DOC 页数:17 大小:238.50KB
返回 下载 相关 举报
java入门程序题及源码5_第1页
第1页 / 共17页
java入门程序题及源码5_第2页
第2页 / 共17页
java入门程序题及源码5_第3页
第3页 / 共17页
java入门程序题及源码5_第4页
第4页 / 共17页
java入门程序题及源码5_第5页
第5页 / 共17页
点击查看更多>>
资源描述

《java入门程序题及源码5》由会员分享,可在线阅读,更多相关《java入门程序题及源码5(17页珍藏版)》请在金锄头文库上搜索。

1、一. 实验目的理解并掌握线程与同步学会使用java实现GUI设计学会使用awt和Swing组件的一般功能二. 实验内容实验题目一: 分别建立两个线程,产生并显示100,10000和2000,3000之间的随机数各 10000个,并分别求和显示出来1 程序源代码 import java.util.*; class MyThread1 implements Runnable public void run()Random r=new Random();int n,i=0; long p=0; don = r.nextInt(9901)+100;p = p+n;System.out.printf(%

2、d ,n);i+; while( i10000);System.out.println(); System.out.println(the sum of 10000 number between 100,10000 is:+p); class MyThread2 implements Runnable public void run()Random r=new Random();int n,i=0; long p=0; don = r.nextInt(1001)+2000;p = p+n;System.out.printf(%d ,n);i+; while( i10000); System.o

3、ut.println(); System.out.println(the sum of 10000 number between 2000,3000 is:+p); public class title5_1 public static void main(String args)System.out.println(the random numbers are shown as follows:); MyThread1 holy=new MyThread1(); Thread t1=new Thread(holy); t1.start(); MyThread2 shit=new MyThre

4、ad2(); Thread t2=new Thread(shit); t2.start(); 2 实验结果 实验题目二: 建立两个线程分别不断读取两个文本文件中的数据并求和,当为-1时结束并显示求和结果。1 程序源代码import java.io.*;import java.util.*;class sum private int sum=0; synchronized void sum(int i) sum=sum+i;public void sum1()System.out.println(=+sum);class MyThread1 implements Runnablesum sum2

5、;MyThread1(sum sum2)this.sum2=sum2;new Thread(this).start(); public void run() try Scanner sc1=new Scanner(new File(num1.txt); int n; while(sc1.hasNextInt() if(n=sc1.nextInt()=-1)sum2.sum1();System.exit(-1);elseSystem.out.printf(+%d,n);tryThread.sleep(300);catch(InterruptedException e)sum2.sum(n); c

6、atch(Exception e) class MyThread2 implements Runnablesum sum2;MyThread2(sum sum2)this.sum2=sum2;new Thread(this).start(); public void run() try Scanner sc2=new Scanner(new File(num2.txt); int n; while(sc2.hasNextInt() if(n=sc2.nextInt()=-1)sum2.sum1();System.exit(-1);elseSystem.out.printf(+%d,n);try

7、Thread.sleep(300);catch(InterruptedException e)sum2.sum(n); catch(Exception e) public class title5_2 public static void main(String args)System.out.printf(sum=0);sum sum1=new sum(); MyThread1 holy=new MyThread1(sum1); MyThread2 shit=new MyThread2(sum1); 2.实验结果实验题目三: 请编写一个swing图形界面程序,设计一个计算器,要求如下:(1)

8、 支持+、-、*、/ 和%等常用运算符;(2) 支持sin、cos、max、min、pow和sqrt等常用函数;(3) 支持十进制、八进制、十六进制和二进制之间的转换。1 程序源代码import javax.swing.*;import java.awt.*;import java.awt.event.*;public class title5_3 extends JFrame implements ActionListener double num1, num2, num3; boolean end, add, mul, sub, div, sin, cos, max, min, Hex,

9、Oct, Bin; JTextField t = new JTextField(0); JButton button = new JButton24; JPanel p1 = new JPanel(); JPanel p2 = new JPanel();public title5_3() setTitle(计算器); setResizable(false); setBounds(100, 100, 330, 280); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(p1, North); t.setEditable(false); t.

10、setColumns(28); t.setHorizontalAlignment(JTextField.RIGHT); p1.add(t); GridLayout l = new GridLayout(4, 0); l.setVgap(10); l.setHgap(10); p2.setLayout(l); add(p2, Center); button0 = new JButton(1); button1 = new JButton(2); button2 = new JButton(3); button3 = new JButton(sin);button3.setMargin(new I

11、nsets(0,0,0,0); button4 = new JButton(Oct);button4.setMargin(new Insets(0,0,0,0); button5 = new JButton(+); button6 = new JButton(4); button7 = new JButton(5); button8 = new JButton(6); button9 = new JButton(cos);button9.setMargin(new Insets(0,0,0,0); button10 = new JButton(Hex);button10.setMargin(n

12、ew Insets(0,0,0,0); button11 = new JButton(-); button12 = new JButton(7); button13 = new JButton(8);button14 = new JButton(9); button15 = new JButton(max);button15.setMargin(new Insets(0,0,0,0); button16 = new JButton(Bin);button16.setMargin(new Insets(0,0,0,0); button17 = new JButton(*); button18 = new JButton(0);button19 = new JButton(.); button20 = new JButton(=); button21 = new JButton(min);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号