java入门程序题及源码5_第1页
java入门程序题及源码5_第2页
java入门程序题及源码5_第3页
java入门程序题及源码5_第4页
java入门程序题及源码5_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

一.实验目的 理解并掌握线程与同步 学会使用 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; do n = r.nextInt(9901)+100; p = p+n; System.out.printf(“%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; do n = r.nextInt(1001)+2000; p = p+n; System.out.printf(“%d “,n); i+; while( i10000); System.out.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 MyThread2(); 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 Runnable sum sum2; 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); else System.out.printf(“+%d“,n); try Thread.sleep(300); catch(InterruptedException e) sum2.sum(n); catch(Exception e) class MyThread2 implements Runnable sum 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); else System.out.printf(“+%d“,n); try 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) 支持+、-、*、/ 和%等常用运算符; (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, 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.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 Insets(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(new 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“); button21.setMargin(new Insets(0,0,0,0); button22 = new JButton(“,“); button23 = new JButton(“/“); for (JButton x : button) p2.add(x); x.addActionListener(this); JLabel ll = new JLabel(); ll.setPreferredSize(new Dimension(10, 0); add(ll, “West“); JLabel rl = new JLabel(); rl.setPreferredSize(new Dimension(10, 0); add(rl, “East“); setVisible(true); public static void main(String args) title5_3 calculator = new title5_3(); public void num(int i) String s = String.valueOf(i); if (end) t.setText(“0“); end = false; if ( (t.getText().equals(“0“) t.setText(s); else String str = t.getText() + s; t.setText(str); public void sign(int i) if (i = 1) add = true; mul = false; sub = false; div = false; sin = false; cos = false; max = false; min = false; Hex = false; Oct = false; Bin = false; if (i = 2) add = false; mul = false; sub = true; div = false; sin = false; cos = false; max = false; min = false; Hex = false; Oct = false; Bin = false; if (i = 3) add = false; mul = true; sub = false; div = false; sin = false; cos = false; max = false; min = false; Hex = false; Oct = false; Bin = false; if (i = 4) add = false; mul = false; sub = false; div = true; sin = false; cos = false; max = false; min = false; Hex = false; Oct = false; Bin = false; if (i = 5) add = false; mul = false; sub = false; div = false; sin = true; cos = false; max = false; min = false; Hex = false; Oct = false; Bin = false; if (i = 6) add = false; mul = false; sub = false; div = false; sin = false; cos = true; max = false; min = false; Hex = false; Oct = false; Bin = false; if (i = 7) add = false; mul = false; sub = false; div = false; sin = false; cos = false; max = true; min = false; Hex = false; Oct = false; Bin = false; if (i = 8) add = false; mul = false; sub = false; div = false; sin = false; cos = false; max = false; min = true; Hex = false; Oct = false; Bin = false; if (i = 9) add = false; mul = false; sub = false; div = false; sin = false; cos = false; max = false; min = false; Hex = true; Oct = false; Bin = false; if (i = 10) add = false; mul = false; sub = false; div = false; sin = false; cos = false; max = false; min = false; Hex = false; Oct = true; Bin = false; if (i = 11) add = false; mul = false; sub = false; div = false; sin = false; cos = false; max = false; min = false; Hex = false; Oct = false; Bin = true; if(i!=5) end = true; public void point() String s; if (t.getText().indexOf(.) 0) s = t.getText() + “.“; t.setText(s); public void sepa() String s; s = t.getText() + “,“; t.setText(s); public void sin() t.setText(“sin“); public void cos() t.setText(“cos“); public void max() t.setText(“max“); public void min() t.setText(“min“); public void eql() String s=“; int k; /计算 if(add|div|mul|sub) num2 = Double.parseDouble(t.getText(); if(max|min) s = t.getText(); k = s.indexOf(“,“); num1 = Double.parseDouble(s.substring(0,k); num2 = Double.parseDouble(s.substring(k+1,s.length(); if (add) num3 = num1 + num2; if (mul) num3 = num1 * num2; if (sub) num3 = num1 - num2; if (div) num3 = num1 / num2; if (sin) num3 = Math.sin(num2/180*Math.PI); if (cos) num3 = Math.cos(num2/180*Math.PI); if (Oct) s = Integer.toOctalString(int)num1); if (Hex) s = Integer.toHexString(int)num1); if (Bin) s = Integer.toBinaryString(int)num1); if (max) num3 = Math.max(num1,num2); if (min) num3 = Math.min(num1,num2); if(!(Oct|Hex|Bin) s = String.valueOf(num3); t.setText(s); end = true; public void actionPerformed(ActionEvent e) if (e.getSource() = button0) num(1); if (e.getSource() = button1) num(2); if (e.getSource() = button2) num(3); if (e.getSource() = button6) num(4); if (e.getSource() = button7) num(5); if (e.getSource() = button8) num(6); if (e.getSource() = button12) num(7); if (e.getSource() = button13) num(8); if (e.getSource() = button14) num(9); if (e.getSource() = button18) num(0); if (e.getSource() = button5) sign(1); if (e.getSource() = button11) sign(2); if (e.getSource() = button17) sign(3); if (e.getSource() = button23) sign(4); if (e.getSource() = button19) point(); if (e.getSource() = button20) eql(); if (e.getSource() = button3) sin(); sign(5); if (e.getSource() = button9) cos(); sign(6); if (e.getSource() = button4) sign(10); if (e.getSource() = button10) sign(9); if (e.getSource() = button16) sign(11); if (e.getSource() = button15) max(); sign(7); if (e.getSource() = button21) min(); sign(8); if (e.getSource() = button22) sepa(); 2.试验结果 实验题目四: 编写一个 swing 图形界面程序能将多行文本框中的内容保存到一个文本文件, 也能将一个文本文件的内容显示到这个多行文本框中。 1程序源代码 import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; public class title5_4 public static void main(String args) JFrame frame = new JFrame(); FlowLayout flow = new FlowLayout(); final JTextArea textArea1 = new JTextArea(20, 30); JButton button2 = new JButton(“写入 holyshit.txt“); JButton

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论