太原理工大学JAVA实验报告.doc_第1页
太原理工大学JAVA实验报告.doc_第2页
太原理工大学JAVA实验报告.doc_第3页
太原理工大学JAVA实验报告.doc_第4页
太原理工大学JAVA实验报告.doc_第5页
已阅读5页,还剩26页未读 继续免费阅读

下载本文档

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

文档简介

本科实验报告课程名称: java语言程序设计 实验地点: 明向校区综合实验楼 专业班级: 软件1302 学号: 2013006158 学生姓名: 李国涛 指导教师: 李君婵 时间:2014年 12 月 31 日实验名称 Java语言基础实验目的和要求:(1) 通过实验,掌握Java语言程序设计的基本方法。(2) 学会Java语言中标示符的命名、运算符和表达式的应用。(3) 熟练地掌握Java程序流程控制语句的应用。实验内容(1) 编写应用程序,计算100(含100)以内所有偶数的和。(2) 编写一个程序,求1-100间的素数。(3) 使用for循环语句,编写程序输出以下图案。主要仪器设备笔记本电脑实验记录(写出实验内容中的程序代码和运行结果)(可分栏或加页)package db;public class db public static void main(String args) / TODO Auto-generated method stubint sum=0;for(int i=1;i=100;i+)if(i%2=0)sum=sum+1;System.out.println(sum);package SS;public class SS public static void main(String args) System.out.print(1-100间的素数为:2 );/ TODO Auto-generated method stubfor(int i=1;i=100;i+) for(int a=2;a=(i-1)System.out.print(i+ ); package shiyanyi;import java.io.*;public class One5 public static void main(String args)throws IOException for(int i=0;i1&i%2=0);elseSystem.out.print(n);for(int k=9;ki/2;k-)System.out.print( );for(int j=0;j0;i-)if(i1&i%2=0);elseSystem.out.print(n);for(int k=9;ki/2;k-)System.out.print( );for(int j=0;ji;j+)System.out.print(*);遇到的问题和解决方法1.程序没有输出结果。解决方法:使用java提供的“System.out.println()“方法输出数据。2.程序无法运行。解决方法:检查代码是否有语法错误,将鼠标移动到感叹号上查明原因。3.程序运行结果有异常心得体会1.java在使用中每一项的实现都有很多种方法,要尽可能的尝试,区别每种方法的特点。2.java编程时要注意语法、逻辑错误。实验名称 实验2 数组和字符串编程实验目的和要求1.通过实验,掌握Java语言中数组、字符串程序设计的基本方法。2.较熟练地掌握Java程序数组的应用、字符串处理操作的方法应用。实验内容1.编写一个程序,在控制台输入10个数,按大小顺序输出。2.求一个3*3矩阵对角元素之和。3.输入一段字符串,统计其中有多少个单词。(单词用空格隔开)主要仪器设备笔记本电脑实验记录(写出实验内容中2,3的程序代码和运行结果)(可分栏或加页)2、 求一个3*3矩阵对角元素之和。package shiyaner;import java.io.*;public class Two2 public static void main(String args)throws IOException int a,suma=0; a= new int33; for(int i=0;i3;i+) for(int j=0;j3;j+) aij=i+j; for(int i=0;i3;i+) for(int j=0;j3;j+) if(i=j) suma+=aij; System.out.print(suma); 3、 输入一段字符串,统计其中有多少个单词。(单词用空格隔开)package shiyaner;import java.io.*;public class Two3 public static void main(String args)throws IOException String str;char ch1 = new char111; int q=0;InputStreamReader re = new InputStreamReader(System.in);BufferedReader bre = new BufferedReader (re);str = bre.readLine();ch1=str.toCharArray();int nw,state;state=0;nw=0;for(int i=0;ich1.length;i+)if(ch1i= )state=0;else if(state=0)state=1;+nw;System.out.print(nw); 遇到的问题和解决方法1.使用二维数组时要用new创建数组。2.程序报出异常,程序不能运行时,要判断是否要抛出异常。实验体会 使用数组时要注意下标值不要超出范围,数组元素的下标从零开始,直到数组元素个数减1为止,如果下标超出范围,则运行时会产生“数组访问越界异常”。实验名称 实验3 Java面向对象程序设计实验目的和要求 掌握Java面向对象程序设计中类、继承、包和接口的概念与应用,能熟练应用方法、类、成员变量等元素。实验内容1.创建一个复数类complex,以附属进行数学运算、复数具有如下格式:RealPart+ImaginaryRart*I ,其中,I为-1的平方根。 要求如下:1 利用浮点变量表示此类的私有数据。提供两个构造方法,一个用于此类声明的对象的初始化;一个为默认的无参构造方法。2 提供两复数加、减、乘的运算方法。3 按格式(a,b)打印复数。其中a为实部,b为虚部。2.创建银行账号Account类,实现存款(balance)的存(deposit)、取(withdraw)和查询(getbalance)等功能。3.编程定义一个接口,实现三个数中求最小值和最大值的方法,并将程序存放在mymaths包中。4.编程实现以下简单超市管理,具体要求如下:1 实现描述超市的类Suppermacket类,具有货架属性,记录系统中现有商品,定义盘点商品的函数check(盘点各类商品的数量,并输出盘点结果),查询商品的函数query,并显示查询结果;2 定义商品类Goods,具有商品名称Name,商品价格Price,商品产地Producer等属性,操作Sale(销售商品,余额不足时给予提示)、Add(商品上架操作)和ShowMe(显示商品信息)。3 main函数,测试以上所要求的各种功能,即可以根据菜单命令增加、删除、查询商品、盘点商品,以及商品销售和商品上架的操作提示: 超市货架可以使用商品数组,数组大小100表明存放100个商品。 注意实现商品查询的多态性,要求可以根据商品的名称或产地或价格查询商品的数量。主要仪器设备笔记本电脑实验记录(写出实验内容中1,4的程序代码和运行结果)(可分栏或加页)1、创建一个复数类complex,以附属进行数学运算、复数具有如下格式:RealPart+ImaginaryRart*I ,其中,I为-1的平方根。 要求如下: 利用浮点变量表示此类的私有数据。提供两个构造方法,一个用于此类声明的对象的初始化;一个为默认的无参构造方法。 提供两复数加、减、乘的运算方法。 按格式(a,b)打印复数。其中a为实部,b为虚部。package shiyansan;public class Three1 public static void main(String args)complex a = new complex(1,2);complex b = new complex(3,4);a.print();a.add(b).print();a.sub(b).print();a.mul(b).print();class complexfloat RealPart;float ImaginaryRart;public complex()RealPart = 0;ImaginaryRart = 0;public complex(float RealPart,float ImaginaryRart)this.RealPart = RealPart;this.ImaginaryRart = ImaginaryRart;public complex add(complex b)this.RealPart = this.RealPart+b.RealPart;this.ImaginaryRart = this.ImaginaryRart+b.ImaginaryRart;return this;public complex sub(complex b)this.RealPart = this.RealPart-b.RealPart;this.ImaginaryRart = this.ImaginaryRart-b.ImaginaryRart;return this;public complex mul(complex b)float a,d; a = this.RealPart*b.RealPart - this.ImaginaryRart*b.ImaginaryRart; d = this.RealPart*b.ImaginaryRart + this.ImaginaryRart*b.RealPart;this.RealPart = a;this.ImaginaryRart = d;return this;public void print()System.out.println(+RealPart+,+ImaginaryRart+);4、package shiyansan;import java.io.*;public class Three4 public static void main(String args) throws IOExceptionSuppermacket a = new Suppermacket(); int month; BufferedReader in = new BufferedReader(new InputStreamReader(System.in); String read = new String(); for(;) System.out.println(1。增加商品种类 2。盘点商品 3.根据商品名查找 4.根据价格查找 5.根据产地查找 6.出售商品 7.商品上架 8。显示商品信息); System.out.println(Enter the month number:); month = (int)Double.parseDouble(in.readLine(); switch(month) case 1:a.Add_Goods();break; case 2:a.check();break; case 3:read = in.readLine();a.query(read);break; case 4:read = in.readLine();a.query(Float.parseFloat(read);break; case 5:read = in.readLine();a.query(read,1);break; case 6:read = in.readLine();a.goodInteger.parseInt(read).Sale();break; case 7:read = in.readLine();a.goodInteger.parseInt(read).Add();break; case 8:read = in.readLine();a.goodInteger.parseInt(read).ShowMe();break; case 9:read = in.readLine();a.delete(Integer.parseInt(read);break; class Suppermacket public Goods good = new Goods200 ;public int Goods_num = -1;public int Add_Goods() throws IOExceptionInputStreamReader re = new InputStreamReader(System.in); BufferedReader bre = new BufferedReader (re); System.out.println(输入商品名字:);String Name = new String();Name = bre.readLine();System.out.println(输入商品价格);float Price = Float.parseFloat(bre.readLine();System.out.println(输入商品产地);String Produced = new String();Produced = bre.readLine(); for(int i=0;i=Goods_num;i+) if(goodi.getname().equals(Name)=true) goodi.addcount(); return 1; this.Goods_num+;goodGoods_num = new Goods();goodGoods_num.setGoods(Name, Price, Produced); return 0;public void check() for(int i=0;i=Goods_num;i+) System.out.println(goodi.getname()+ +goodi.getcount(); public void query(String Name) for(int i=0;i=Goods_num;i+) if(goodi.getname().equals(Name)=true) System.out.println(goodi.getname()+ +goodi.getcount(); public void query(float Price ) for(int i=0;i=Goods_num;i+) if(goodi.getprice()=Price) System.out.println(goodi.getname()+ +goodi.getcount(); public void query(String Producer ,int j ) for(int i=0;i=Goods_num;i+) if(goodi.getproducer().equals(Producer)=true) System.out.println(goodi.getcount(); public void delete(int i)int k;for(k=i+1;k0)this.count-;System.out.println(商品已经卖出!);elseSystem.out.println(商品已卖空,此操作不成功!);public void Add()this.count+;System.out.println(商品已经上架!);public void ShowMe() System.out.println(商品名称+this.Name); System.out.println(商品价格+this.Price); System.out.println(商品产地+this.Produced);public void setGoods(String Name,float Price,String Produced)this.Name = Name;this.Price = Price;this.Produced = Produced;public String getname()return this.Name;public float getprice()return this.Price;public String getproducer()return this.Produced;public void addcount()this.count+;public int getcount()return count;遇到的问题和解决方法1.声明对象要用new实例化。2.复数类的乘法运算中发生计算错误。解决方法:可以使用局部变量来存储计算结果,然后再赋给所在对象的变量,最后将对象返回。实验体会1. java中提供了很多方法来使用,可以根据实际需要高产量高效率地编写代码。2.面向对象的编写程序的方法对代码重用特别有效,方法的重载缩短了程序的长度。实验名称 实验4 Java异常处理实验目的和要求1.掌握异常的概念,以及如何定义、抛出和捕获处理异常。实验内容3.在实验三中的超市、商品类中创建一个自定义异常类。要求:1.在定义商品类时,若销售商品数量大于余额则作为异常处理。2.自定义异常类名为InsufficientFundsException。提示:1 产生异常条件是某类商品销售数量大于库存额,因此是否抛出异常要先判断该条件。2 确定产生异常的方法,应该在sale方法中产生异常Insufficient Funds Exception。3 处理异常安排在调用sale的时候,因此sale方法要声明异常,由上级方法捕获并处理。4 要定义好自己的异常。主要仪器设备笔记本计算机实验记录(写出实验内容中3的程序代码和运行结果)(可分栏或加页)3、package shiyansi;import java.io.*;public class Four3 public static void main(String args) throws ExceptionSuppermacket a = new Suppermacket(); int month; BufferedReader in = new BufferedReader(new InputStreamReader(System.in); String read = new String(); for(;) System.out.println(1。增加商品种类 2。盘点商品 3.根据商品名查找 4.根据价格查找 5.根据产地查找 6.出售商品 7.商品上架 8。显示商品信息); System.out.println(Enter the month number:); month = (int)Double.parseDouble(in.readLine(); switch(month) case 1:a.Add_Goods();break; case 2:a.check();break; case 3:read = in.readLine();a.query(read);break; case 4:read = in.readLine();a.query(Float.parseFloat(read);break; case 5:read = in.readLine();a.query(read,1);break; case 6:read = in.readLine();a.goodInteger.parseInt(read).Sale();break; case 7:read = in.readLine();a.goodInteger.parseInt(read).Add();break; case 8:read = in.readLine();a.goodInteger.parseInt(read).ShowMe();break; case 9:read = in.readLine();a.delete(Integer.parseInt(read);break; class Suppermacket public Goods good = new Goods200 ;public int Goods_num = -1;public int Add_Goods() throws IOExceptionInputStreamReader re = new InputStreamReader(System.in); BufferedReader bre = new BufferedReader (re); System.out.println(输入商品名字:);String Name = new String();Name = bre.readLine();System.out.println(输入商品价格);float Price = Float.parseFloat(bre.readLine();System.out.println(输入商品产地);String Produced = new String();Produced = bre.readLine(); for(int i=0;i=Goods_num;i+) if(goodi.getname().equals(Name)=true) goodi.addcount(); return 1; this.Goods_num+;goodGoods_num = new Goods();goodGoods_num.setGoods(Name, Price, Produced); return 0;public void check() for(int i=0;i=Goods_num;i+) System.out.println(goodi.getname()+ +goodi.getcount(); public void query(String Name) for(int i=0;i=Goods_num;i+) if(goodi.getname().equals(Name)=true) System.out.println(goodi.getname()+ +goodi.getcount(); public void query(float Price ) for(int i=0;i=Goods_num;i+) if(goodi.getprice()=Price) System.out.println(goodi.getname()+ +goodi.getcount(); public void query(String Producer ,int j ) for(int i=0;i=Goods_num;i+) if(goodi.getproducer().equals(Producer)=true) System.out.println(goodi.getcount(); public void delete(int i)int k;for(k=i+1;k=Goods_num;k+)this.goodk-1 = goodk; this.Goods_num-;class Goodsprivate String Name;private float Price;private String Produced;private int count = 0;InsufficientFundsException problem = new InsufficientFundsException();public void Sale()throws InsufficientFundsExceptionthis.count-;if(this.count0)throw problem;public void Add()this.count+;System.out.println(商品已经上架!);public void ShowMe() System.out.println(商品名称+this.Name); System.out.println(商品价格+this.Price); System.out.println(商品产地+this.Produced);public void setGoods(String Name,float Price,String Produced)this.Name = Name;this.Price = Price;this.Produced = Produced;public String getname()return this.Name;public float getprice()return this.Price;public String getproducer()return this.Produced;public void addcount()this.count+;public int getcount()return count;class InsufficientFundsException extends ExceptionInsufficientFundsException()super(余额不足!交易不成功!);遇到的问题和解决方法1. 抛出异常使用错误。 需要根据实际需要使用,区别throw和throws。实验体会 java提供了异常类,区别throw和throws,自定义异常主要作用是区分异常发生位置,要分别监听类与异常类。实验名称 实验5 多线程编程实验目的和要求1.现成的概念、线程的生命周期。2.多线程的编程:扩展Thread类与使用Runnable接口。3. 使用多线程机制实现动画实验内容3.创建简单的程序ThreeThread.java,该程序将创建三个线程,每个线程应当显示它所运行的时间(可以考虑使用Date类或Calendar类)。主要仪器设备笔记本计算机实验记录(写出实验内容中3的程序代码和运行结果)(可分栏或加页)3、 创建简单的程序ThreeThread.java,该程序将创建三个线程,每个线程应当显示它所运行的时间(可以考虑使用Date类或Calendar类)。package shiyanwu;import java.util.Calendar;public class Five3 public static void main(String args)Thread1 t1= new Thread1(t1);Thread1 t2= new Thread1(t2);Thread1 t3= new Thread1(t3);t1.start();t2.start();t3.start();class Thread1 extends ThreadString name;public Thread1(String str)name = str;public void run()long time1,time2; time1 = Calendar.getInstance().getTimeInMillis();System.out .println(name+开始+time1);for(int i=0;i44455555;i+)time2 = Calendar.getInstance().getTimeInMillis();System.out .println(name+结束+time2);System.out .println(name+运行完毕!用时:+(time2-time1)+ms); 遇到的问题和解决方法 使用runnable接口实现多线程编程时,由runnable接口实现的对象中没有start( )方法来使线程启动。解决方法:使用thread类的对象的构造方法来与runnable类对象相接,然后用thread类的对象调用start( )方法。实验体会 通过实验我了解到多线程可以把任务分块执行,分块后可以同时进行而不用等待, 这样效率更高。实验名称 实验6 图形用户界面编程实验目的和要求 掌握文本组件、按钮和单、复选按钮组件的使用;掌握列表的使用,鼠标、键盘事件的处理;掌握布局控制的方法。实验内容2. 完成下图GUI的布局管理,不需要实现功能。3.试设计一窗口,内含一个按钮。开始运行时,按钮显示“Click Me”字样,当按钮按下时,按钮显示为“Click Me Again”字样,再按一次,则按钮显示“Click Me”字样,依次循环。主要仪器设备笔记本计算机实验记录(写出实验内容中2,3的程序代码和运行结果)(可分栏或加页)2、package shiyanliu;import javax.swing.*;import javax.swing.border.TitledBorder;public class Six2 public static void main(String vrgs)JFrame frm = new JFrame();frm.setTitle(FileChooserDemo);/frm.setBounds(50, 50,850, 480);/frm.setResizable(false);/FlowLayout flow = new FlowLayout();frm.setLayout(null);JPanel one = new JPanel(); JPanel two = new JPanel(); JPanel three = new JPanel(); JPanel four = new JPanel(); JPanel five = new JPanel(); one.setBounds(5, 15, 150, 270);one.setBorder(new TitledBorder(Dialog Type);two.setBounds(152,15 , 250, 270);two.setBorder(new TitledBorder(Filter Controls);three.setBounds(400,15 , 200, 270);three.setBorder(new TitledBorder(Display Options);four.setBounds(598, 15, 230, 270);four.setBorder(new TitledBorder(File and Directory Options);five.setBounds(5, 290, 821, 100);five.setBorder(new TitledBorder();frm.add(one);frm.add(two); frm.add(three); frm.add(four); frm.add(five); one.setLayout(null); two.setLayout(null); three.setLayout(null); four.setLayout(null); five.setLayout(null); JRadioButton jra = new JRadioButton();JRadioButton jra1 = new JRadioButton();JRadioButton jra2 = new JRadioButton();jra.setText(Open);jra.setBounds(20, 40, 90,20);jra1.setText(Save);jra1.setBounds(20, 80, 75,20);jra2.setText(Custom);jra2.setBounds(20, 120, 75,20);JTextArea jt = new JTextArea();jt.setLineWrap(true); jt.append(dialog1);jt.setBounds(20, 185, 100, 50);one.add(jra);one.add(jra1);one.add(jra2);one.add(jt);JButton jb = new JButton();jb.setText(Remove + +All Files+ +Filter);jb.setBounds(15, 40, 215, 30);JButton jb1 = new JButton();jb1.setText(Add + +All Files+ +Filter);jb1.setBounds(15, 100, 215, 30);JRadioButton jrab1 = new JRadioButton();jrab1.s

温馨提示

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

评论

0/150

提交评论