




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上第六章 Java输入输出1、说明程序功能:import java.io.*;public class CharWrite public static void main(String args) try FileWriter fw=new FileWriter("charset.txt "); for ( int i=32;i<126;i+) fw.write(i); fw.close(); catch (IOException e) 答:向文件charset.txt中输入ASCII码从32到126共94个字符。2、先用随机函数产生出任意的2
2、0个整数,再按由小到大的顺序排序,然后将结果写入一个文件中,最后从该文件中读出后显示出来。请分别用顺序文件和随机文件的读写形式进行编程测试。代码:import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;im
3、port java.io.OutputStream;import java.util.Random;public class RandNum public static void main(String args) Random rand = new Random();/实例化一个产生随机数的类 int num = new int20;/定义一个整型数组,用来存储产生的随机数 int r = 0;/随机数 for(int i = 0;i<num.length;i+) r = rand.nextInt(1000);/产生一个1000以内的随机数 /放入数组 numi = r; int k;
4、 /进行排序 for(int i = 0;i<num.length;i+) for(int j = 0;j<i;j+) if(numi < numj) k = numi; numi = numj; numj = k; RandNum rn = new RandNum(); /写入文件 rn.WriteFile("src/123",num); /读取文件 int x = rn.ReadFile("src/123"); /读取的数组 for(int i = 0;i<num.length;i+) System.out.print(xi
5、+ "t"); if(i%5 = 4) System.out.println(); /* * 读取文件的方法 * param path */ public int ReadFile(String path) int a = null; try / 实例化一个File输入流对象InputStream is = new FileInputStream(path);/ 实例化一个Buffer输入流对象BufferedInputStream bs = new BufferedInputStream(is);int length = bs.available();/读取第一个为数组大
6、小int k = bs.read();/用来计数int count = 0;/实例化数组a = new intk;String str = ""/中间变量for(int i = 0;i<length;i+)/继续读取k = bs.read();/若读取的不是换行if(k != 13 && k != 10 && k != -1)/读取到空格while(k != 32)str += (char)k;k = bs.read();/把字符串里的数字转换成整型放入数组中acount = Integer.parseInt(str);count+;s
7、tr = ""/清空字符串is.close();bs.close();System.out.println("读取成功"); catch (FileNotFoundException e)System.out.println("找不到指定文件,请确认文件路径"); catch (IOException e) e.printStackTrace(); return a; /* * 写入文件 * param path 路径 * param a 数组 */ public void WriteFile(String path,int a)tr
8、y / 实例化一个File输出流OutputStream os = new FileOutputStream(path);/ 实例化一个Buffered输出流BufferedOutputStream bos = new BufferedOutputStream(os);/写入数组大小bos.write(a.length);/写入换行符bos.write(13);bos.write(10);/循环写入数组的值for(int i = 0;i<a.length;i+)bos.write(Integer.toString(ai).getBytes();/写入1个空格bos.write(32);b
9、os.flush();bos.close();os.close();System.out.println("写入成功"); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); 截图3、统计一个文本文件中单词的个数。文本文件的名字从命令行中获得。代码import java.io.FileReader;import java.io.IOException;import java.util.StringTokenizer;public cla
10、ss test public static void main(String args) throws IOException FileReader fileReader=new FileReader(args0); StringBuffer stringBuffer=new StringBuffer(); char b=new char506; while(fileReader.read(b,0,505)!=-1) stringBuffer.append(b); StringTokenizer stringTokenizer=new StringTokenizer(stringBuffer.
11、toString(); System.out.println("文本文件中单词的个数为:"+stringTokenizer.countTokens(); 结果截图第七章 Java图形用户界面1、阅读下面的程序,回答问题。 import java.awt.*; import javax.swing.*; public class T extends JFrame public T ( ) super("GridLayout"); Container con=this.getContentPane(); con.setLayout(new GridLayou
12、t(2,3); con.add(new JButton("a"); con.add(new JButton("b"); con.add(new JButton("c"); con.add(new JButton("d"); con.add(new JButton("e"); con.add(new JButton("f"); setSize(200, 80); setVisible(true); public static void main(String args) new
13、 T(); 画图表示程序运行后的图形界面。如果程序通过实现某个接口处理按钮的动作事件,则该接口名为何?接口中的方法头声明如何?答:实现的接口是ActionListener,该接口的方法声明是public void actionPerformed(ActionEvent e)2、编写一个简单的计算器,要求图形用户界面如下图所示。代码import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionL
14、istener;import java.util.HashSet;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;public class jisuanqi public static void main(String args) new MyCalculator(); class MyCalculator private double result=0.0; private String string=""
15、; private JTextField jtext = new JTextField("",15); String str=new String"Clear","+","0","1","2","3","-","*",".","4","5","6","/","%","=",&qu
16、ot;7","8","9" public MyCalculator() JFrame frame = new JFrame("calculator"); frame.setBounds(400,200,450,250); JPanel jp = new JPanel(new GridLayout(3,6,10,10); frame.setLayout(new BorderLayout(); frame.add(jtext,BorderLayout.NORTH); frame.add(jp,BorderLayout.CENTE
17、R); final HashSet hashSet=new HashSet(); class MyEvent implements ActionListener public void actionPerformed(ActionEvent e) for(Object k:hashSet) if(e.getSource()=k) JButton jb=(JButton)k; if(jb.getText()="Clear") jtext.setText(""); else jtext.setText(jtext.getText()+jb.getText()
18、; if(jb.getText()="=") char c=jtext.getText().toCharArray(); int i; for(i=0;i<c.length;i+) if(ci='+'|ci='-'|ci='*'|ci='/'|ci='%') break; String astr=String.copyValueOf(c, 0, i); String bstr=String.copyValueOf(c,i+1,c.length-i-2); MyCompute mycompu
19、te = new MyCompute(); result=pute(Double.parseDouble(astr),Double.parseDouble(bstr),ci); jtext.setText(""+result); for(int i=0;i<18;i+) JButton b=new JButton(stri); jp.add(b); b.addActionListener(new MyEvent(); hashSet.add(b); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.
20、setVisible(true); class MyCompute private double result=0.0; public MyCompute() public double compute(double a,double b,char c) if(c='+') result=a+b; if(c='-') result=a-b; if(c='*') result=a*b; if(c='/') result=a/b; if(c='%') result=a%b; return result; 3. 编程题:
21、请编写一个简单的用户登录程序。要求使用图形用户界面,* 用户名和密码假定均为java,且密码输入时全部显示为星号(*)。 */代码public class TheExam7_3 public static void main(String args) MyLand my=new MyLand(); class MyLand int a=0; public MyLand() JFrame frame = new JFrame(); frame.setBounds(450,300,370,220); Font font=new Font("宋体",Font.PLAIN,30);
22、 frame.setFont(font); frame.setLayout(new BorderLayout(); JPanel paneltop = new JPanel(); paneltop.add(new JLabel("用户登陆",JLabel.CENTER); frame.add(paneltop,BorderLayout.NORTH); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel_1 = new JPanel(new Gri
23、dLayout(3,1); frame.add(panel_1,BorderLayout.CENTER); JPanel panel_11 = new JPanel(new FlowLayout(); JPanel panel_12 = new JPanel(new FlowLayout(); JPanel panel_13 = new JPanel(new GridLayout(1,5); panel_1.add(panel_11); panel_1.add(panel_12); panel_1.add(panel_13); panel_11.add(new JLabel("用户名
24、:"); final JTextField text1 = new JTextField("",10); panel_11.add(text1); panel_12.add(new JLabel("用户密码:"); final JPasswordField text2 = new JPasswordField("",10); text2.setEchoChar('*'); panel_12.add(text2); JButton b=new JButton("确定"); class MyE
25、xam7_3 implements ActionListener public void actionPerformed(ActionEvent e) JDialog dialog=new JDialog(); dialog.setBounds(450, 200,560,400); dialog.setVisible(true); Font font1 = new Font("宋体",Font.BOLD,70); dialog.setFont(font1); if(text1.getText().equalsIgnoreCase("java")&
26、&String.valueOf(text2.getPassword().equals("java") dialog.add(new JLabel("登陆成功!",JLabel.CENTER); / new MyCalculator(); else dialog.add(new JLabel("密码或用户名错误!",JLabel.CENTER); b.addActionListener(new MyExam7_3(); panel_13.add(new Label(" "); panel_13.add(new
27、 Label(" "); panel_13.add(b); panel_13.add(new Label(" "); panel_13.add(new Label(" "); 结果截图第八章 Java多线程技术1、程序如下:public class Borley extends Threadpublic static void main(String argv) Borley b = new Borley(); b.start(); public void run() System.out.println("Running&
28、quot;); 下面描述正确的是:(B)单选A) 通过编译和运行但是没有任何输出。B) 通过编译,运行后输出"Running"C) 编译出错,没有线程可供运行D) 编译出错,没有权限使用Thread2、 编写程序,启动1000个线程,每个线程给初值为0的变量sum加1。(提示,需要通过引用传递,将sum传递给每个线程,为了能够进行引用传递,需要定义一个Integer包装对象来保存sum)。代码public class Borley extends Threadpublic static void main(String argv)/通过Integer包装对象sumInteg
29、er sum = new Integer(0);/循环1000次,开启1000个线程for(int i = 0;i<1000;i+)Borley b = new Borley(sum);b.start();private Integer sum;public Borley(Integer sum)this.sum = sum;public void run() sum+;System.out.println(sum);截图3、利用多线程技术模拟龟兔赛跑的场面。提示:先设计一个线程类模拟两个参与赛跑的选手的行为;然后创建该类的两个对象分别代表乌龟和兔子;让兔子跑快些,但在路上睡眠休息时间长
30、些,而乌龟跑慢些却不休息;当某个选手到达终点时其线程运行结束;二个选手均到达终点时,显示并祝贺获胜选手,然后主线程结束。代码Import java.util.Date;public class xiancheng extends Thread private int tortoise_walk = 0; / 乌龟已跑长度存放变量 private int rabbit_walk = 0; / 兔子已跑长度存放变量 private int finish = 1000; / 终点 private volatile boolean hasWinner = false;/ 胜利者诞生 /* class
31、Tortoise_Run implements Runnable public void run() try while (!hasWinner) if (tortoise_walk % 100 = 0 && (tortoise_walk != 0|tortoise_walk>=finish) /乌龟每100米休息500毫秒 Thread.sleep(500); tortoise_walk+; System.out.println("乌龟已跑"+tortoise_walk+"米"); catch (InterruptedException e) e.printStackTrace(); class Rabbit_Run implements Runnable public void run() try while (!hasWinner) if (rabbit_walk % 20 = 0 && (rabbit_walk != 0|rabbit_walk>=finish) /兔子每20米休息500毫秒 System.out.pr
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025浙江嘉兴市中医医院招聘编外合同制人员9人(第二批)笔试备考试题及答案解析
- 2025浙江大学药学院专职研究员招聘5人考试参考题库附答案解析
- 2025云南玉溪国润建筑有限责任公司招聘工作人员10人笔试模拟试题及答案解析
- 2025浙江杭州市建德市三江生态管理有限公司招聘13人考试参考题库附答案解析
- 2025广西南宁职业技术大学招聘教职人员控制数44人笔试参考题库附答案解析
- 2025西藏日喀则市殡仪馆人员招聘17人笔试模拟试题及答案解析
- 2025浙江杭州市拱墅区拱宸桥街道社区卫生服务中心(拱墅区大运河中医院)招聘编外聘用人员9人考试模拟试题及答案解析
- 2025年甘肃省兰州市城关区盐场路街道盐场堡村社区卫生服务站招聘考试参考题库附答案解析
- 2025云南省宣威市双河乡中心学校招聘编制外学龄前教育有关辅助人员(7人)笔试模拟试题及答案解析
- 在线医疗健康咨询平台开发与运营规划书
- 2025年四川高校大学《辅导员》招聘考试题库及答案
- 2025-2026学年统编版(2024)初中语文七年级上册教学计划及进度表
- 标准化产品需求文档编写方法
- 办公室文秘岗试题带答案
- 2025年河南疾控中心考试题库
- 2025年高考【数学】真题及答案(新高考Ⅱ卷)
- 养老护理员竞赛理论试卷答案(含答案)
- 2025年广东省广州市中考历史试卷(含解析)
- 2025年四川省能源投资集团有限责任公司人员招聘笔试备考题库及答案详解(新)
- 广东省公路服务区管理系统升级及运维项目
- 2025版《中国系统性红斑狼疮诊疗指南》解读 4
评论
0/150
提交评论