2022年《Java语言程序设计》软件实验指导书 .pdf
1 本科实验报告课程名称:java 程序设计实验地点:实验室 110 专业班级:物联网1401 学号: 2014002010 学生姓名:李涛宇指导教师:李君蝉实验成绩:2016年6 月20 日名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 17 页 - - - - - - - - - 2 目录实验 1 Java语言基础 . 3实验 2 数组和字符串编程 . 4实验 3 Java面向对象程序设计 . 5实验 4 Java异常处理 . 7实验 5 多线程编程 . 12实验 6 图形用户界面编程 . 13实验 7 输入与输出 . 15名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 17 页 - - - - - - - - - 3 实验 1 Java 语言基础1. 实验目的(1) 通过实验,掌握Java 语言程序设计的基本方法。(2) 学会 Java 语言中标示符的命名、运算符和表达式的应用。(3) 熟练地掌握Java 程序流程控制语句的应用。2. 实验内容“水仙花数”是指一个3 位数,其个位、十位、百位上的数字的立方和等于该数本身,例如 371=33+73+13,因此 371 是一个水仙花数。编写程序,求所有的水仙花数。3. 实验步骤及结果(1) 实验过程中实验步骤及其结果。package实验测试 ; publicclass Flower publicstaticvoid main(String args ) / TODO Auto-generated method stubinta, b, c ; for( a=1; a10; a+) for( b=0; b10; b+) for( c=0; c + name + 成功 ! ); else System. out .println( 该商品不存在 ); publicstaticvoid Add() System. out .println(* 增加商品 * ); System. out .print(商品名称: ); name=Getline(); System. out .print(商品价格: ); price=Float.parseFloat( Getline(); System. out .print(商品数量: ); num=Integer.parseInt( Getline(); System. out .print(商品产地: ); producer=Getline(); if ( market .Add( name, price, num, producer) System. out .println( 增加商品 - + name + 成功 ! ); else System. out .println( 存在相同商品, 增加失败 ! ); publicstatic String Getline() Scanner scan = new Scanner(System.in ); String str = scan.nextLine(); return str.trim(); 2)publicclass Goods private String name; privatefloatprice; privateintnum; private String producer; public Goods(String name,float price,int num,String producer) this. name=name; this. price=price; this. num=num; this. producer=producer; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 17 页 - - - - - - - - - 10 public String GetName() returnthis. name; publicfloat GetPrice() returnthis. price; publicint GetNum() returnnum; public String GetProducer() returnthis. producer; publicvoid Showgoods() System. out .print(商品名称: +name+ 商品价格: +price+ 商品数量: +num+ 商品产地: +producer+n); 3)import java.util.Vector; publicclass Supermarkt static Vector listgoods=new Vector(); publicvoid ShowMenu() System. out .println(-); System. out .println(1. 增加商品 ); System. out .println(2. 删除商品 ); System. out .println(3. 显示商品 ); System. out .println(4. 查询商品 ); System. out .println(5. 退出系统 ); System. out .println(-); return; / 增加商品publicboolean Add(String name,float price, int num,String producer) int count=listgoods.size(); for ( int i=0;icount;i+) if ( listgoods.get(i).GetName().equals(name) returnfalse; Goods newgoods =new Goods(name,price,num,producer); listgoods.add(newgoods); returntrue; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 17 页 - - - - - - - - - 11 / 删除商品publicboolean Del(String name) int count=listgoods.size(); for ( int i=0;icount;i+) if ( listgoods.get(i).GetName().equals(name) listgoods.remove(i); returntrue ; returnfalse; / 显示商品publicboolean Showgoods() int count=listgoods.size(); if (count=0) returnfalse; else for ( int i=0;icount;i+) listgoods.get(i).Showgoods(); returntrue ; / 查询商品publicboolean Query(String name) int count=listgoods.size(); for ( int i=0;icount;i+) if ( listgoods.get(i).GetName().equals(name) listgoods.get(i).Showgoods(); returntrue ; returnfalse; (2) 实验体会。Java 语言由于异常处理的存在,使得 java 可以对程序运行中出现的不合法数据或事件有处理能力,针对不同的情况,可以使用不同的try catch进行分别处理。Java程序更加具有强壮性。名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 17 页 - - - - - - - - - 12 实验 5 多线程编程1. 实验目的(1) 现成的概念、线程的生命周期。(2) 多线程的编程:扩展Thread 类与使用Runnable 接口。(3) 使用多线程机制实现动画2. 实验内容(1) 创建简单的程序ThreeThread.java,该程序将创建三个线程,每个线程应当显示它所运行的时间(可以考虑使用Date 类或 Calendar 类) 。3. 实验步骤及结果(1) 实验过程中实验步骤及其结果import java.text.SimpleDateFormat; import java.util.*; publicclass Hi publicstaticvoid main(String args ) SimpleDateFormat s2 = new SimpleDateFormat(yyyy年MM 月dd 日EEEEE时mm 分ss 秒 ); new HiH(s2 , jerry).start(); new HiH(s2 , liming).start(); new HiH(s2 , merry).start(); class HiH extends Thread private SimpleDateFormat a; private String s1 ; public HiH(SimpleDateFormat a,String str) this. a = a; s1 = str; publicvoid run() synchronized( this. a) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 17 页 - - - - - - - - - 13 System.out .println( +s1 ); System.out .println(this. a.format(new Date(); try sleep( int)(Math.random ()*1000); catch(InterruptedException e) 运行结果(2) 实验体会本实验通过sleep 与 start控制线程在三个线程之间灵活切换,使得java 程序利用率更加提高。在就绪,执行,挂起之间切换。实验 6 图形用户界面编程1. 实验目的掌握文本组件、按钮和单、复选按钮组件的使用;掌握列表的使用,鼠标、键盘事件的处理;掌握布局控制的方法。2. 实验内容(1) 试设计一窗口,内含一个按钮。开始运行时,按钮显示“Click Me ”字样,当按钮按下时,按钮显示为“Click Me Again ”字样,再按一次,则按钮显示“Click Me ”字样,依次循环。3. 实验步骤及结果(1) 实验过程中实验步骤及其结果package图形界面 ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 13 页,共 17 页 - - - - - - - - - 14 import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JButton; import javax.swing.JFrame; publicclass GUI publicstaticvoid main(String args) / TODO Auto-generated method stubJFrame frm=new JFrame(); frm.setLayout(null); frm.setVisible(true); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setBounds(500,250,250,150); JButton btn=new JButton(Click Me); frm.add(btn); btn.setBounds(120,60,120,30); ButtonListener bln=new ButtonListener(btn); btn.addMouseListener(bln); class ButtonListener implements MouseListener booleant =true ; JButton btn ; public ButtonListener(JButton btn) this. btn =btn; Overridepublicvoid mouseClicked(MouseEvent arg0) / TODO Auto-generated method stubt =! t ; Overridepublicvoid mouseEntered(MouseEvent arg0) / TODO Auto-generated method stub 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 14 页,共 17 页 - - - - - - - - - 15 Overridepublicvoid mouseExited(MouseEvent arg0) / TODO Auto-generated method stub Overridepublicvoid mousePressed(MouseEvent arg0) / TODO Auto-generated method stubif ( t ) btn .setText(Click Me Again); elsebtn .setText(Click Me); Overridepublicvoid mouseReleased(MouseEvent arg0) / TODO Auto-generated method stub (2) 实验体会图形用户界面使得java 程序更具有交互性。在本实验中, 通过事件监听器的注册,可以对不同的鼠标指令多态地进行不同反应。编程中由于对button组建的函数不太熟悉,导致实现接口时出现问题。实验 7 输入与输出1. 实验目的(1) 掌握 Java 两种基本输入 / 输出流类的使用。(2) 掌握 Java 文件类型的使用。(3) 掌握文件的输入、输出方法。2. 实验内容。(1) 编写程序,利用Java 字符流文件输入/ 输出类,实现将源程序复制到另一个文件中。(2)编写一个程序,比较两个文件的内容是否相同。名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 15 页,共 17 页 - - - - - - - - - 16 3. 实验步骤及结果(1) 实验过程中实验步骤及其结果package输入输出 ; import java.awt.*; import java.awt.event.*; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; publicclass IOStream publicstaticvoid main(String args) throws IOException / TODO Auto-generated method stubBufferedInputStream bis = null; BufferedOutputStream bos = null; bis = new BufferedInputStream(new FileInputStream(newFile(输入输出输入输出IOStream.java); bos = new BufferedOutputStream(new FileOutputStream ( newFile(C:UsersAdministratorDesktop.outfile.txt); int i = 0; while (i = bis.read() != -1) bos.write(i); bis.close(); bos.close(); (二)文本内容比较package比较 ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 16 页,共 17 页 - - - - - - - - - 17 import java.io.FileReader; import java.io.IOException; publicclass Compare publicstaticvoid main(String args)throws IOException / TODO Auto-generated method stubint str = 0; int str1 = 0; FileReader fr = null; FileReader fw = null; fr = newFileReader(C:UsersAdministratorDesktopinfile.txt); fw = newFileReader(C:UsersAdministratorDesktopoutfile.txt); if (str=fr.read()!= (str1=fw.read() System. out .println(Different); else System. out .println( 文件内容一样 ); (2) 实验体会。试验中进行三个流类的套结,从而实现对文件中字符的读写输入。让我在实验中困惑的是 ecllips的相对路径好像无法识别,在文件路径打开时花了很大功夫。名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 17 页,共 17 页 - - - - - - - - -