Java实验报告学号149074353 姓名程裕博班级物141指导教师柯栋梁安徽工业大学计算机学院2016年11月实验一: 利用JAVA 反射技术分析类结构自己定义的类:package chap05;public class analysis {private int a;private char c;protected int b;public double d;public void test1(){}private void test2(){}protected double test3(){return 1.0;}}用java反射技术分析输出的结果:Enter class name (e.g. java.util.Date):chap05.analysisclass chap05.analysis{public chap05.analysis();public void test1();private void test2();protected double test3();private int a;private char c;protected int b;public double d;}1.分析程序运行时的输出结果。
输出的结果中显示了被分析类的方法与变量,包括这些方法与变量的修饰符2.分析与JAVA反射技术相关的几个类的作用:ng.reflect.Constructor;Constructor 提供关于类的单个构造方法的信息以及对它的访问权限。
ng.reflect.Field;Field 提供有关类或接口的单个字段的信息,以及对它的动态访问权限。
反射的字段可能是一个类(静态)字段或实例字段。
ng.reflect.Method;Method 提供关于类或接口上单独某个方法(以及如何访问该方法)的信息。
所反映的方法可能是类方法或实例方法(包括抽象方法)。
ng.reflect.Modifier;Modifier 类提供了static 方法和常量,对类和成员访问修饰符进行解码。
修饰符集被表示为整数,用不同的位位置(bit position) 表示不同的修饰符。
实验二:利用JAVA 反射技术分析对象结构实验内容:运行示例程序,分析Integer 数组对象的结构;改写程序分析一下自定义的类对象,如Employee 类。
结果分析:该程序较为复杂,通过调试模式我发现ObjectAnalyzer的toString方法为一递归函数,从代码可以看出程序是用来分析对象中非静态的成员变量并将其显示出来,通过递归分析对象中的对象的成员变量,通过获取超类分析其继承来的成员变量。
自己编的类:package chap05;public class analysis extends Employee{private int a = 0;private char c = ' ';static protected int b = 0;public double d = 0;public void test1(){}private void test2(){}protected double test3(){return 1.0;}}分析结果:chap05.analysis[a=0,c= ,d=0.0][][salary=0.0,name= null,year=0,mouth=0,day=0][]实验三: 利用JAVA 反射技术调用方法指针定义的People类:package chap05;public abstract class People {double salary;String name;int year,mouth,day;public abstract double getSalary();public abstract void setSalary(double salary);}class Employee extends People{Employee(String name,double salary,int year,int mouth,int day){this.salary = salary; = name;this.year = year;this.mouth = mouth;this.day = day;}Employee(){}public double getSalary(){return salary;}public void setSalary(double salary){this.salary = salary;}}class Manager extends People{private double bonus;Manager(String name,double salary,int year,int mouth,int day){this.salary = salary; = name;this.year = year;this.mouth = mouth;this.day = day;bonus= 0;}public double getSalary(){return salary+bonus;}public void setSalary(double salary){this.salary = salary;}public void setBonus(double bonus){this.bonus = bonus;}}通过指针调用的结果:public static double chap05.MethodPointerTest.square(double)9.0public static double ng.Math.sqrt(double)3.0public double chap05.Employee.getSalary()50000.0public void chap05.Manager.setBonus(double)public double chap05.Manager.getSalary()80000.03.结果分析:1.分析程序运行时的输出结果。
代码中Methodl类就相当于一个指向类中方法的指针,Class类通过getMethod方法获取这个Method类,之后Method类的对象可以通过invoke方法来调用这个方法。
2.掌握反射技术调用类(对象)方法的基本原理与流程。
(同上)4.扩展:1.自定义一些简单类,使用反射技术调用该类(静态方法)或类对象的方法;package chap05;import ng.reflect.*;public class MethodPointertest2 {public static void main(String[] args) throws Exception{ Method sin = Math.class.getMethod("sin",double.class);Field PI = Math.class.getField("PI");System.out.println(sin.invoke(null,PI.getDouble(null)/2));}}输出结果:1.0这里没有再自己定义方法了,这里类比Method方法的使用过程通过Field方法获取了Math类里的PI成员并使用2.对于未知类(对象)的内部结构,如何利用反射技术调用其方法?可以先通过反射技术分析其结构再通过反射技术调用其方法。
实验四: 利用TreeSet 实现集合元素排序源代码:package shiyan04;import java.util.*;public class Employee implements Comparable<Employee>{ public String name;public int salary;public Date hiredate = new Date();Employee(String name,int salary,Date hiredate){ = name;this.salary = salary;this.hiredate.setTime(hiredate.getTime());}public int compareTo(Employee other){return pareTo();}}class salaryComparator implements Comparator<Employee>{public int compare(Employee a,Employee b){return a.salary - b.salary;}}package shiyan04;import java.text.SimpleDateFormat;import java.util.*;public class Process {public static void main(String[] args) {Employee[] e = new Employee[10];Date nowTime = new Date();SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");String[] name = { "张三", "李四", "王五", "赵六", "Tom", "Jerry", "Amy", "test", "Jack", "John" };long oneday = 86400000;// System.out.println(nowTime);for (int i = 0; i < 10; i++) {nowTime.setTime(nowTime.getTime() - oneday * 30);e[i] = new Employee(name[i], 3000 + i * 100, nowTime);}System.out.println("按姓名:");SortedSet<Employee> byname = new TreeSet<Employee>();for (Employee i : e) {byname.add(i);}Iterator<Employee> iter = byname.iterator();while (iter.hasNext()) {Employee e1 = iter.next();System.out.println( + " " + e1.salary + " "+ format.format(e1.hiredate));}System.out.println("按工资:");salaryComparator comp2 = new salaryComparator();SortedSet<Employee> bysalary = new TreeSet<Employee>(comp2);for (Employee i : e) {bysalary.add(i);}iter = bysalary.iterator();while (iter.hasNext()) {Employee e1 = iter.next();System.out.println( + " " + e1.salary + " "+ format.format(e1.hiredate));}System.out.println("按日期:");SortedSet<Employee> bydate = new TreeSet<Employee>(new Comparator<Employee>() {public int compare(Employee a, Employee b) {return pareTo(b.hiredate);}});for (Employee i : e) {bydate.add(i);}iter = bydate.iterator();while (iter.hasNext()) {Employee e1 = iter.next();System.out.println( + " " + e1.salary + " "+ format.format(e1.hiredate));}}}运行结果:按姓名:Amy 3600 2013-05-28Jack 3800 2013-03-29Jerry 3500 2013-06-27John 3900 2013-02-27Tom 3400 2013-07-27test 3700 2013-04-28张三 3000 2013-11-24李四 3100 2013-10-25王五 3200 2013-09-25赵六 3300 2013-08-26按工资:张三 3000 2013-11-24 李四 3100 2013-10-25 王五 3200 2013-09-25 赵六 3300 2013-08-26 Tom 3400 2013-07-27 Jerry 3500 2013-06-27 Amy 3600 2013-05-28 test 3700 2013-04-28 Jack 3800 2013-03-29 John 3900 2013-02-27 按日期:John 3900 2013-02-27 Jack 3800 2013-03-29 test 3700 2013-04-28 Amy 3600 2013-05-28 Jerry 3500 2013-06-27 Tom 3400 2013-07-27 赵六 3300 2013-08-26 王五 3200 2013-09-25 李四 3100 2013-10-25 张三 3000 2013-11-24实验五: 多线程同步之团结就是力量实验源代码:package chap08;import java.util.Random;public class ExCooperation2 {public static void main(String[] args) {WorkStudent2 ws1 = new WorkStudent2(1, 10);WorkStudent2 ws2 = new WorkStudent2(2, 20);WorkStudent2 ws3 = new WorkStudent2(3, 30);WorkStudent2 ws4 = new WorkStudent2(4, 40);new Thread(ws1).start();new Thread(ws2).start();new Thread(ws3).start();new Thread(ws4).start();}}class DeskAndChair{int[] desk={0,0,0,0,0},chair={0,0,0,0,0};private int chairCount = 500;private int deskCount = 500;public synchronized Boolean distribute(int id,long sleeptime){int chairs = 1;int desks = 1;Random rdm = new Random(System.currentTimeMillis());if(rdm.nextInt() % 2 ==0){if(chairCount > 0){chairs = chairs - 1;while(chairs<0){try{wait();}catch(InterruptedException e){}}System.out.println("Student "+id+": wiping desk "+chairCount--);chair[id]+=1;chairs = chairs + 1;notifyAll();}}else{if(deskCount > 0){desks = desks - 1;while(desks<0){try{wait();}catch(InterruptedException e){}}System.out.println("Student "+id+": wiping desk "+deskCount--);desk[id]+=1;desks = desks + 1;notifyAll();}}if(chairCount == 0&&deskCount == 0)return true;elsereturn false;}}class WorkStudent2 implements Runnable {private long sleeptime;private int id;static DeskAndChair dc = new DeskAndChair();public WorkStudent2(int id,long time){super();this.id=id;this.sleeptime=time;}public void run(){while(!dc.distribute(id, sleeptime)){try{Thread.sleep(sleeptime);}catch(Exception e){}};System.out.println("Result: Student "+id+" wiped "+dc.desk[id]+" desks and"+dc.chair[id]+" chairs.");}}运行结果:实验六: 利用JDBC 技术实现简易MYSQL 客户端程序1.工具:(2)数据库软件:Microsoft Access 2010(3)Java开发工具:Eclipse2.程序框图:主界面成绩表班级表添加修改删除添加修改删除成绩表班级表按姓名查询按成绩查询按班级名称查询按平均成绩查询2.部分运行截图及源代码:数据库表:1.主界面代码:class MyPanel extends JPanel{Image img=Toolkit.getDefaultToolkit().getImage("c:/a.jpg");public void paint(Graphics g){g.drawImage(img,0,0,this);}}public class MainForm extends JFrame implements ActionListener {JMenu mSystem=new JMenu("系统");JMenuItem mExit=new JMenuItem("退出");JMenu mOperate=new JMenu("数据操作");JMenuItem mAdd=new JMenuItem("添加成绩表中的数据");JMenuItem mDel=new JMenuItem("删除成绩表中的数据");JMenuItem mModify=new JMenuItem("修改成绩表中的数据");JMenu mQuery=new JMenu("查询");JMenuItem mName=new JMenuItem("在成绩表中按姓名查询");JMenuItem mScore=new JMenuItem("在成绩表中按成绩查询");JMenuItem mBname=new JMenuItem("在班级表中按班级名查询");JMenuItem mBscore=new JMenuItem("在班级表中按班级平均成绩查询");JMenu mHelp=new JMenu("帮助");JMenuItem mAbout=new JMenuItem("系统信息");JMenuBar mBar=new JMenuBar();MainForm(){super("学生成绩管理系统");setSize(680,640);mSystem.add(mExit);mOperate.add(mAdd);mOperate.add(mDel);mOperate.add(mModify);mQuery.add(mName);mQuery.add(mScore);mQuery.add(mBname);mQuery.add(mBscore);mHelp.add(mAbout);mBar.add(mSystem);mBar.add(mOperate);mBar.add(mQuery);mBar.add(mHelp);setJMenuBar(mBar);mExit.addActionListener(this);mAdd.addActionListener(this);mDel.addActionListener(this);mModify.addActionListener(this);mName.addActionListener(this);mScore.addActionListener(this);mBname.addActionListener(this);mBscore.addActionListener(this);mAbout.addActionListener(this);setContentPane(new MyPanel());setVisible(true);}public void actionPerformed(ActionEvent ae){if(ae.getSource()==mExit)System.exit(0);else if(ae.getSource()==mAbout)JOptionPane.showMessageDialog(this,"学生成绩管理系统\n\n计算机学院\n\n2013年12月","系统信息",RMATION_MESSAGE);else if(ae.getSource()==mAdd)new AddForm().setVisible(true);else if(ae.getSource()==mDel)new DeleteForm().setVisible(true);else if(ae.getSource()==mModify)new ModifyForm().setVisible(true);else if(ae.getSource()==mName)new NameQueryForm().setVisible(true);else if(ae.getSource()==mScore)new ScoreQueryForm().setVisible(true);else if(ae.getSource()==mBname)new BnameQueryForm().setVisible(true);else if(ae.getSource()==mBscore)new BsoreQueryForm().setVisible(true); }public static void main(String[] args) {new MainForm();}}2.添加数据:3.修改数据:如果没有该数据:4.删除数据:注:限于篇幅部分功能没有截图。