java实验指导书(2012黄国华).doc_第1页
java实验指导书(2012黄国华).doc_第2页
java实验指导书(2012黄国华).doc_第3页
java实验指导书(2012黄国华).doc_第4页
java实验指导书(2012黄国华).doc_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

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

文档简介

Java程序设计实验指导书课程名称:Java程序设计课程性质:专业选修课实验课时:12学时教学对象: 09信息与计算科学指导老师:黄国华实验一:Java开发环境建立实验一、教学时数:2学时。二、实验目的与要求1、熟悉安装和配置JDK开发环境。2、熟悉安装和配置IDE开发工具。3、掌握Java程序编辑、编译和运行的过程。4、掌握Java程序的构成特点。5、总结在调试过程中的错误。三、实验内容和步骤1、下载、安装并设置 Java SDK 软件包。2、安装 Java IDE软件,解压到jdk安装目录。3、编写一个简单的 Java 程序,运行结果为在屏幕上输出“HELLO WORLD!”。public class Hellopublic static void main(String args)System.out.println(HELLO WORLD!);/4、编写一个Java Applet程序实现功能为:在屏幕上显示“HELLO WORLD!”。四、注意事项Java源程序文件的命名规则。五、预习与思考题1、什么是Java虚拟机?它的作用是什么?2、如何在不同的环境下设置环境变量?实验二:Java语言基础一、教学时数:2学时二、实验目的与要求1、掌握 Java 基本数据类型、运算符与表达式、数组和字符串的使用方法。2、掌握各种变量的声明方式。3、理解 Java 程序语法结构,掌握顺序结构、选择结构和循环结构语法的程序设计方法。4、通过以上内容,掌握 Java 语言的编程规则。5、了解Java程序文件名和类名的说明方法。三、实验内容和步骤1、编写声明不同数据类型变量的程序文件 Java2_1.java,源代码如下:public class Java2_1public static void main(String args)byte b=0x55;short s=0x55ff;int i=1000000;long l=0xffffL;char c=a;float f=0.23F;double d=0.7E-3;boolean B=true;String S=这是字符串类数据类型;System.out.println(字节型变量 b = +b); System.out.println(短整型变量 s = +s); System.out.println(整型变量 i = +i); System.out.println(长整型变量 l = +l); System.out.println(字符型变量 c = +c);System.out.println(浮点型变量 f = +f);System.out.println(双精度变量 d = +d);System.out.println(布尔型变量 B = +B); System.out.println(字符串类对象 S = +S);编译并运行该程序。2、建立 Java2_2.java 文件,通过本程序了解变量的使用范围,源代码如下。public class Java2_2static int i=10;public static void main(String args)int k=10; System.out.println(i=+i);System.out.println(k=+k);System.out.println(i=+i);System.out.println(k=+k); /编译时将出错,已出k的使用范围编译 Java2_2.java,此时会出现错误提示。因为变量 k 在方法块中声明,在方法块之外它是不存在的,所以编译时会出错。修改上面的程序,并成功运行该程序。3、编程采用冒泡法实现对数组元素由小到大排序,冒泡法排序对相邻的两个元素进行比较,并把小的元素交换到前面。提示:关键代码如下:for(i=0;iintArray.length-1;i+)for(j=i+1;jintArrayj)t=intArrayi;intArrayi=intArrayj;intArrayj=t;4、数组选做实验:利用数组解决约瑟夫环问题。设有n个人围做一圈并按顺时针方向从1到n编号,从第s个人开始进行1到m报数,报数到第m的人出圈,再从其下一个人重新开始从1到m的报数,如此进行下去知道所有的人都出圈为止。给出这n个人出圈的顺序。5、运行下面的程序,理解String类常用方法的使用。public class 2_4public static void main (String args)String str=”I like java programming”;int i1=str.indexOf(J);String s1=str.substring(i1);String s2=str.substring(i1,i1+4);int i2=str.lastIndexOf(J);String s3=str.substring(i2+5);System.out.println(s1=+s1);System.out.println(s2=+s2);System.out.println(s3=+s3);四、注意事项Java语言中的数组与C语言中的数组差别。五、预习与思考题1、定义变量的作用是什么?2、&和&有什么区别?3、break和continue在流程控制中的作用是什么?实验三:Java面向对象编程基础实验一、教学时数:2学时二、实验目的与要求1、理解 Java 语言是如何体现面向对象编程基本思想。2、了解类的封装方法,以及如何创建类和对象。3、掌握成员变量和成员方法的特性及构造方法和析构方法的使用。4、熟练掌握 OOP 方式进行程序设计的方法,理解类的继承性和多态性的作用。三、实验内容和步骤1、多态在工资系统中的应用。下面给出一个根据雇员类型利用多态性完成工资单计算的程序。定义一个类Employee作为超类,Employee的子类有Boss(每星期发给他固定工资,而不计工作时间)、PieceWorker(按其生产的产品数发放工资)、HourlyWorker(根据工作时间长短发放工资)。对所有雇员类型都使用earnings()方法完成其工资单的计算,但每个人挣的工资按他所属的雇员类计算,所有雇员类都是从超类Employee派出生的。所以在超类中声明earnings()方法,该方法没有实质性工作,而是在每个子类都提供恰当的earnings()方法的重写。为了计算雇员的工资,程序仅使用雇员对象的一个超类引导并调用earnings()方法。class Employee private String firstName; private String lastName; public Employee(String first,String last) firstName=first; lastName=last; public String getEmployeeName()return firstName; public String getLastName()return lastName; public String toStrings()return firstName+ +lastName; public double earnings()System.out.println(Employs salary is 0.0);return 0.0;/定义Boss类,为Employee的子类class Boss extends Employee private double weeklySalary; public Boss(String first,String last,double s) super(first,last); setWeeklySalary(s);public void setWeeklySalary(double s)weeklySalary=(s0?s:0);public double earnings() return weeklySalary; public String toStrings() return Boss: +super.toStrings();/定义PieceWorker类,为Employee的子类class PieceWorker extends Employee private double wagePiece; private int quantity; public PieceWorker(String first,String last,double w,int q) super(first,last); setWage(w); setQuantity(q);public void setWage(double w) wagePiece=(w0?w:0); public void setQuantity(int q) quantity=(q0?q:0);public double earnings() return quantity*wagePiece; public String toStrings() return PieceWoeker: +super.toStrings();/定义HourlyWorker类,为Employee的子类class HourlyWorker extends Employee private double wage; private double hours; public HourlyWorker(String first,String last ,double w,double h) super(first,last); setWage(w); setHours(h);public void setWage (double w) wage=(w0?w:0); public void setHours(double h) hours=(h=0&h168?h:0);public double earnings() return wage*hours;public String toStrings() return HourlyWorker: +super.toStrings(); public class TestEmployeepublic static void main(String args )/使用超类声明ref Employee ref; String out=;/分别定义各子类 Boss b=new Boss(Hohn,Smith,800.00); PieceWorker p=new PieceWorker(Bob,Lewis,2.5,200); HourlyWorker h=new HourlyWorker(Karen,price,13.75,40);/使用子类分别实例化 ref=b; out+=ref.toStrings()+earned $+ref.earnings()+n+b.toStrings()+ earned $+b.earnings()+n;System.out.print(out);ref=p;out+=ref.toStrings()+earned $+ref.earnings()+n+p.toStrings()+ earned $+p.earnings()+n;System.out.print(out);ref=h;out+=ref.toStrings()+earned $+ref.earnings()+n+h.toStrings()+ earned $+h.earnings()+n;System.out.print(out);2、编程让一个类实现对个自定义的接口。要求定义两个接口收费和调节温度。分别定义公交汽车、出租车、电影院,实现以上定义的两个接口。interface Charge public void charging();interface Adapt public void controlTemperature();class Bus implements Charge public void charging() System.out.println(公共汽车:一元/张,不计算公里数); class Taxis implements Charge, Adapt public void charging() System.out.println(出租车:1.60元/公里,起价3公里); public void controlTemperature() System.out.println(安装了Hair空调); class Cinema implements Charge, Adapt public void charging () System.out.println(电影院:门票,十元/张); public void controlTemperature() System.out.println(安装了中央空调); class Example public static void main(String args) Bus busofline7=new Bus(); Taxi 天宇=new 出租车(); 电影院 红星=new 电影院(); 七路.收取费用(); 天宇.收取费用(); 红星.收取费用(); 天宇.controlTemperature(); 红星.controlTemperature(); 四、注意事项子构造方法的使用。五、预习与思考题1、如何继承一个类?2、为什么说构造函数是一种特殊的方法?特殊在哪里?构造函数什么时候执行?被谁调用?3、同名的不同方法共存的情况称为什么?如何区分这些同名方法?4、内部类inner class和外部类的区别?5、抽象类和接口的区别?实验四:图形用户界面的设计实验一、教学时数:2学时二、实验目的与要求掌握GUI编程中的主要概念:AWT,Swing,窗口、面板,容器布局,布局管理器。掌握GUI编程。掌握事件编程机制。三、实验内容和步骤1. 创建一个黄色面板,通过add()方法在面板中添加了一个按钮,然后将该面板添加到一个JFrame的实例中,JFrame实例的背景被设置为蓝绿色import java.awt.*;import javax.swing.*;public class FrameWithPanel public static void main(String args) JFrame frame = new JFrame(Frame with Panel);Container contentPane = frame.getContentPane();contentPane.setBackground(Color.CYAN);JPanel panel = new JPanel();panel.setBackground(Color.yellow);JButton button = new JButton(Press me);panel.add(button);contentPane.add(panel, BorderLayout.SOUTH);frame.setSize(300, 200);frame.setVisible(true);2. ActionEvent事件的监听程序定义在组件类中.在Frame中添加一个自定义的命令按钮类(mybutton)对象,让它为注册事件的监听程序。import java.awt.*;import javax.swing.*;import java.awt.event.*;public class ActionEventDemo2 public static void main(String args) JFrame frame = new JFrame(ActionEvent Demo2);/ 创建自定义组件MyButton的实例MyButton b = new MyButton(Close);frame.getContentPane().add(b, BorderLayout.CENTER);frame.pack();frame.setVisible(true);/ 下面是MyButton类的定义:class MyButton extends JButton implements ActionListener public MyButton(String text) super(text);/ 注册事件的监听程序addActionListener(this);/ 出现ActionEvent事件时,将结束程序的运行public void actionPerformed(ActionEvent e) System.exit(0);3使用CardLayout布局管理器管理组件,实现翻扑克牌的效果。import java.awt.*;import java.awt.event.*;import javax.swing.*;public class CardLayoutDemo extends MouseAdapter JPanel p1,p2,p3,p4,p5;JLabel l1,l2,l3,l4,l5;/声明一个CardLayout 对象CardLayout myCard;JFrame frame;Container contentPane;public static void main (String args) CardLayoutDemo that = new CardLayoutDemo();that.go();public void go() frame = new JFrame (Card Test);contentPane = frame.getContentPane();myCard = new CardLayout();/设置CardLayout布局管理器contentPane.setLayout(myCard);p1 = new JPanel();p2 = new JPanel();p3 = new JPanel();p4 = new JPanel();p5 = new JPanel();/为每个JPanel创建一个标签并设定不同的/背景颜色,以便于区分l1 = new JLabel(This is the first JPanel);p1.add(l1);p1.setBackground(Color.yellow);l2 = new JLabel(This is the second JPanel);p2.add(l2);p2.setBackground(Color.green);l3 = new JLabel(This is the third JPanel);p3.add(l3);p3.setBackground(Color.magenta);l4 = new JLabel(This is the fourth JPanel);p4.add(l4);p4.setBackground(Color.white);l5 = new JLabel(This is the fifth JPanel);p5.add(l5);p5.setBackground(Color.cyan);/ 设定鼠标事件的监听程序p1.addMouseListener(this);p2.addMouseListener(this);p3.addMouseListener(this);p4.addMouseListener(this);p5.addMouseListener(this);/将每个JPanel作为一张卡片加入frame的内容窗格contentPane.add(p1, First);contentPane.add(p2, Second);contentPane.add(p3, Third);contentPane.add(p4, Fourth);contentPane.add(p5, Fifth);/显示第一张卡片myCard.show(contentPane, First);frame.setSize(300, 200);frame.setVisible(true);/ 处理鼠标事件,每当敲击鼠标键时,即显示下一张卡片。/ 如果已经显示到最后一张,则重新显示第一张。public void mouseClicked(MouseEvent e) myCard.next(contentPane);4. 求给定整数的平方根.编写一个综合程序,带有简单的界面,实现整数的平方根计算(可选)import java.awt.event.*;import java.awt.*;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;class Dwindow extends JFrame implements ActionListener JTextField inputNumber;JTextArea show;Dwindow(String s) super(s);inputNumber = new JTextField(22);inputNumber.addActionListener(this);show = new JTextArea();add(inputNumber, BorderLayout.NORTH);JScrollPane sp = new JScrollPane();sp.setViewportView(show);add(sp, BorderLayout.CENTER);setBounds(60, 60, 300, 300);setVisible(true);validate();addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0););public void actionPerformed(ActionEvent e) boolean boo = false;if (e.getSource() = inputNumber) String s = inputNumber.getText();char a = s.toCharArray();for (int i = 0; i a.length; i+) if (!(Character.isDigit(ai)boo = true;if (boo = true) / 弹出“警告”消息对话框JOptionPane.showMessageDialog(this, 您输入了非法字符, 警告对话框,JOptionPane.WARNING_MESSAGE);inputNumber.setText(null); else if (boo = false) int number = Integer.parseInt(s);show.append(n + number + 平方根: + Math.sqrt(number);public class Ex01 public static void main(String args) new Dwindow(带对话框的窗口,求给定整数的平方根);四、注意事项通过接口来完成事件处理时,应实现接口中的所有方法。五、预习与思考题1、Java的事件处理机制是怎样2、若一个数据只有两种取值可能,采用哪种组件表示比较合适?如果有二种到三种取值可能,采用哪种合适?如果取值的可能大于5个,采用哪种组件合适?实验五:字符和字节流输入/输出一、教学时数:2学时二、实验目的与要求1、了解文件的概念和文件对象的创建方法。2、了解FileInputStream和FileOutoutStream的基本概念。3、学会创建文件输入输出流。4、掌握使用文件输入输出流读写文件的方法。三、实验内容和步骤1创建一个File类对象,判断它是否存在,是否是文件、目录,是否是隐藏文件,如果存在,读取文件的名字,大小,最后修改时间,它的绝对路径等代码如下:import java.io.File;import java.io.IOException;class FileDemo public static void main(String args) try File file=new File(file1.txt); System.out.println(文件是否存在: +file.exists(); System.out.println(是文件吗: +file.isFile(); System.out.println(是文件夹吗: +file.isDirectory(); System.out.println(是否隐藏: +file.isHidden(); System.out.println(名称: +file.getName(); System.out.println(标准文件名: +file.getCanonicalFile(); System.out.println(相对路径: +file.getPath(); System.out.println(绝对路径: +file.getAbsolutePath(); System.out.println(标准路径: +file.getCanonicalPath(); System.out.println(最后修改时间: +file.lastModified(); System.out.println(文件大小: +file.length()+ 字节); catch(IOException ex) ex.printStackTrace(); 2. 利用FileWriter类及BufferedWriter来输出数据文件,利用FileReader类及BufferedRead类读入数据。import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.FileReader;import java.io.IOException;public class BufferReaderandBufferWriter public static void main(String args) try FileWriter fw=new FileWriter(file1.txt); BufferedWriter bw=new BufferedWriter(fw); bw.write(大家好,我正在学习Java); bw.newLine(); bw.write(请多多指教); System.out.println(file1.txt写入成功!*开始读.n); bw.flush(); bw.close(); FileReader fr=new FileReader(file1.txt); BufferedReader br=new BufferedReader(fr); String temp=null; do temp=br.readLine(); System.out.println(temp=null?:temp); while(temp!=null); fr.close(); br.close(); System.out.println(file1.txt已经读完!*); catch(IOException ex) ex.printStackTrace(); 3. .利用FileInputStream类实现输出数据,利用FileOutputStream类实现输出数据,import java.io.*;public class FileStream public static void main(String args) throws Exception FileOutputStream out = new FileOutputStream(test.txt); out.write(.getBytes();/读取字符串,write()不能直接读取字符串,所以只能转换成字节数 out.close();/关闭输入文件对象 byte buf = new byte1024; File f = new File(test.txt); FileInputStream in = new FileInputStream(f); int len = in.read(buf);/取文件的长度 System.out.println(new String(buf,0,len); in.close(); 4. 编程将百度首页的文本内容分别通过字符流和字节流在屏幕上显示出来,看看这两者流对象的区别。import java.io.IOException;import java.io.InputStream;import .MalformedURLException;import .URL;import java.io.*;/* * 测试字节流和字符流读取站点数据时的区别 * 现象:用字符流读取时,中文出现乱码 * * 猜想:字符流读取,字符错位引起的 * author ayis * Mar 13, 2009 */public class Test public static void main(String args)try URL url = new URL();InputStream in = url.openStream();int n;StringBuffer buffer = new StringBuffer();/ 用字节流读取while(n= in.read() != -1)buffer.append(char)n);/ 用字符流读取BufferedReader reader = new BufferedReader(new InputStreamReader(in);while(n = reader.read() != -1)buffer.append(char)n);/ 转码并打印结果String response = new String(buffer.toString().getByt

温馨提示

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

评论

0/150

提交评论