java语言实验指导书.doc_第1页
java语言实验指导书.doc_第2页
java语言实验指导书.doc_第3页
java语言实验指导书.doc_第4页
java语言实验指导书.doc_第5页
已阅读5页,还剩20页未读 继续免费阅读

下载本文档

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

文档简介

此文档收集于网络,如有侵权,请联系网站删除 实验1 Java语言基础训练 1.1实验目的 通过简单的输入输出程序,熟悉Java编程环境,掌握Java程序结构,为进一步学习Java语言的面向对象特征奠定基础。1.2实验要求 1熟悉JCreator或Eclipse软件安装使用方法。 2掌握编写与运行 Java 程序的方法。 3熟悉 Java 语言的概貌,其中包括数据类型、变量、数组和程序控制结构。4掌握JDK 5.0的输入方法。 1.3实验内容1.3.1一个简单的问候程序 /* This is the first sample program */public class HelloWorld public static void main(String args) System.out.println( Hello, World!); 1.3.2 输入输出程序 /* 适合于JDK 5.0或更高的版本 */import java.util.*; / 适合于JDK 5.0或更高的版本,以下同 public class InputTest public static void main(String args) Scanner in = new Scanner(System.in); / 适合于JDK 5.0或更高的版本,以下同 / get first input System.out.print(What is your name? ); String name = in.nextLine(); / 适合于JDK 5.0或更高的版本,以下同 / get second input System.out.print(How old are you? ); int age = in.nextInt(); / 适合于JDK 5.0或更高的版本,以下同 / display output on console System.out.println(Hello, + name + . Next year, youll be + (age + 1); 1.3.3 求100个整数的和import java.util.*;public class ArrayOfStringsDemo public static void main(String args) int a=new int100; Scanner in=new Scanner(System.in); int k; for(int i=0;ia.length;i+) k=i+1; System.out.print(n输入第 +k+ 个整数: ); ai=in.nextInt(); long sum=0; for(int i=0;ia.length;i+) sum +=ai; System.out.println(整数的累计和是:+ sum); 1.3.4编程题1. 课后作业P24,22. P41,4实验 2 类编程练习 2.1实验目的 通过编程和上机实验理解 Java 语言是如何体现面向对象编程基本思想,了解类的封装方法,以及 如何创建类和对象,了解成员变量和成员方法的特性,掌握 OOP 方式进行程序设计的方法,了解类的继承性和多态性的作用。 2.2实验要求 1 编写一个体现面向对象思想的程序。 。 2 编写体现类的继承性的程序。 3 编写体现类的多态性的程序。2.3实验内容2.3.1用户自定义类 import java.util.*;public class EmployeeTest public static void main(String args) / fill the staff array with three Employee objects Employee staff = new Employee3; staff0 = new Employee(Carl Cracker, 75000, 1987, 12, 15); staff1 = new Employee(Harry Hacker, 50000, 1989, 10, 1); staff2 = new Employee(Tony Tester, 40000, 1990, 3, 15); / raise everyones salary by 5% for (int i=0;istaff.length;i+) staffi.raiseSalary(5); / print out information about all Employee objects for (int i=0;istaff.length;i+) System.out.println(name= + staffi.getName() + ,salary= + staffi.getSalary() + ,hireDay= + staffi.getHireDay(); class Employee public Employee(String n, double s, int year, int month, int day) name = n; salary = s; GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day); / GregorianCalendar uses 0 for January hireDay = calendar.getTime(); public String getName() return name; public double getSalary() return salary; public Date getHireDay() return hireDay; public void raiseSalary(double byPercent) double raise = salary * byPercent / 100; salary += raise; private String name; private double salary; private Date hireDay;2.3.2类的继承性 class Employee String name ;int salary;public Employee(String name,int salary) = name;this.salary = salary;public String getDetails( )return Name: +name+nSalary: +salary;class Manager extends Employee private String department ;public Manager(String name,int salary,String department)super(name,salary);this.department = department;public String getDetails( )return Name: +name+nSalary: +salary+nDepartment: + department;class Secretary extends Employeepublic Secretary(String name,int salary)super(name,salary);public class TestOverridingpublic static void main(String srgs)Manager m = new Manager(Tom,2000,Finance);Secretary s = new Secretary(Mary,1500);System.out.println(m.getDetails();System.out.println(s.getDetails(); 2.3.3编程题1.P78,4。2.P111,2,33写一个程序,定义学生类Student,用数组创建10个学生对象,完成以下功能:(1)统计学生的平均年龄。(2)按学号查询学生的相关信息。(3)统计成绩为优的学生人数 也可以改成录取程序。4写一个程序,定义接口或抽象类Shape,由它派生出5个派生类:Circle(圆)、 Square(正方形)、 Rectangle(矩形) Triangle(三角形)、Cylinder(圆柱)。利用Java多态特征,计算几种图形的面积,并求他们的和。(参照C+教材 p213 例6.4) 实验 3异常处理(exception) 3.1实验目的 了解 Java 中包(package)、接口(interface)和异常处理(exception)的作用,掌握包、接口、 异常处理的设计方法。 3.2实验要求 1. 了解 Java 系统包的结构。 2. 掌握创建自定义包的方法。 3. 掌握使用系统接口的技术和创建自定义接口的方法。4. 理解系统异常处理的机制和创建自定义异常的方法。 3.3实验内容 编程题:P111,5.实验 4 建立图形用户界面 4.1实验目的 了解图形用户界面基本组件窗口、按钮、文本框、选择框、滚动条等的使用方法,了解如何使用布局管理器对组件进行管理,以及如何使用 Java 的事件处理机制。 4.2实验要求 1. 理解 Java 的事件处理机制,掌握为不同组件编写事件处理程序的方法。 2. 掌握编写独立运行的窗口界面的方法。 3. 了解 Java Swing 组件的使用方法。 4. 了解对话框的使用方法。4.3实验内容4.3.1图形用户界面及事件处理import java.awt.*;import java.awt.event.*;import javax.swing.*;public class ButtonTest public static void main(String args) ButtonFrame frame = new ButtonFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); /* A frame with a button panel*/class ButtonFrame extends JFrame public ButtonFrame() setTitle(ButtonTest); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); / add panel to frame ButtonPanel panel = new ButtonPanel(); add(panel); public static final int DEFAULT_WIDTH = 300; public static final int DEFAULT_HEIGHT = 200; /* A panel with three buttons.*/class ButtonPanel extends JPanel public ButtonPanel() / create buttons JButton yellowButton = new JButton(Yellow); JButton blueButton = new JButton(Blue); JButton redButton = new JButton(Red); / add buttons to panel add(yellowButton); add(blueButton); add(redButton); / create button actions ColorAction yellowAction = new ColorAction(Color.YELLOW); ColorAction blueAction = new ColorAction(Color.BLUE); ColorAction redAction = new ColorAction(Color.RED); / associate actions with buttons yellowButton.addActionListener(yellowAction); blueButton.addActionListener(blueAction); redButton.addActionListener(redAction); /* An action listener that sets the panels background color. */ private class ColorAction implements ActionListener public ColorAction(Color c) backgroundColor = c; public void actionPerformed(ActionEvent event) setBackground(backgroundColor); private Color backgroundColor; 4.3.2 使用匿名类 import java.awt.*;import java.awt.event.*;import javax.swing.*;public class TextAreaTest public static void main(String args) TextAreaFrame frame = new TextAreaFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); /* A frame with a text area and buttons for text editing*/class TextAreaFrame extends JFrame public TextAreaFrame() setTitle(TextAreaTest); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); buttonPanel = new JPanel(); / add button to append text into the text area JButton insertButton = new JButton(Insert); buttonPanel.add(insertButton); insertButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent event) textArea.append(The quick brown fox jumps over the lazy dog. ); ); / add button to turn line wrapping on and off wrapButton = new JButton(Wrap); buttonPanel.add(wrapButton); wrapButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent event) boolean wrap = !textArea.getLineWrap(); textArea.setLineWrap(wrap); scrollPane.revalidate(); wrapButton.setText(wrap ? No Wrap : Wrap); ); add(buttonPanel, BorderLayout.SOUTH); / add a text area with scroll bars textArea = new JTextArea(8, 40); scrollPane = new JScrollPane(textArea); add(scrollPane, BorderLayout.CENTER); public static final int DEFAULT_WIDTH = 300; public static final int DEFAULT_HEIGHT = 300; private JTextArea textArea; private JScrollPane scrollPane; private JPanel buttonPanel; private JButton wrapButton;4.3.4编程题1. P258,1,2,3,实验5 流与文件5.1实验目的 1 理解数据流的概念2 理解Java流的层次结构3 理解文件的概念5.2实验要求 掌握字节流的基本使用方法掌握字符流的基本使用方法能够创建、读写、更新文件 5.3实验内容 5.3.1使用标准数据流的应用程序import java.io.*;public class CopyBytes public static void main(String args) throws IOException /创建两个File类对象。 File inputFile = new File(farrago.txt); File outputFile = new File(outagainb.txt); /创建文件输入/输出字节流。 FileInputStream in = new FileInputStream(inputFile); FileOutputStream out = new FileOutputStream(outputFile); int c; /读写文件流中的数据。 while (c = in.read() != -1) out.write(c); /关闭流。 in.close(); out.close(); 5.3.2使用文件I/O流的应用程序 import java.io.*;public class StandardIOpublic static void main(String args)String s;BufferedReader in = new BufferedReader(new InputStreamReader(System.in);System.out.println(Please input : );trys = in.readLine();while(!s.equals(exit)System.out.println( read: +s);s = in.readLine();System.out.println(End of Inputing.);in.close();catch(IOException e)e.printStackTrace();5.3.3使用随机文件类的应用程序 import java.io.*;public class RandomAccessTest public static void main(String args) throws Exception long filePoint = 0 ; String s; RandomAccessFile file = new RandomAccessFile(RandomAccessTest.java,r); long fileLength = file.length(); while (filePointfileLength) s = file.readLine(); System.out.println(s); filePoint = file.getFilePointer(); file.close(); 5.3.4 编程题1.P192,2,4下面2道为曾用题目,没有书上的简单。2.写一段程序,判断当前硬盘上d:test文件夹是否存在,如果存在则列出其内的所有文件及子文件夹,不存在就创建此目录。3.写程序判断D:a.txt文件是否存在,如果存在则向其内依次写入字符串 “Will Smith”,整数25,写入后再从文件中将其读出。实验6 常用实用类编程题1.写一个程序,定义学生类Student,用ArrayList存储创建的10个学生对象,完成以下功能:(1) 从窗口界面输入学生信息(创建学生对象)。(2) 输入后,点击界面“录取”按钮,进行录取(3) 点击“统计”按钮,显示已录取的所有学生信息(包括学号,姓名,分数等)。2.设计一个Java Application程序,从命令行输入一字符串,完成如下操作:(1)获得该字符串的长度,并输出,输出格式如下:字符串长度为:XXXX(2)统计该字符串中大写字母、小写字母和非英文字母各多少个,并输出,输出格式如下: 大写字母为:XXXX个 小写字母为:XXXX个 非英文字母为:XXXX个实验7 多线程7.1实验目的 1 线程的概念、线程的生命周期。2 多线程的编程:继承Thread类与使用Runnable接口。3 使用多线程机制实现动画。7.2实验要求 1 掌握利用JAVA语言编写多线程程序的方法2 掌握线程的调度方法3 掌握多线程环境中GUI程序的编写方法7.3实验内容7.3.1 利用Thread子类的方法实现多线程 public class ThreadTestpublic static void main(String args )Thread t1 = new Thread( new Hello( ) );Thread t2 = new Thread( new Hello( ) );t1.start( );t2.start( );class Hello implements Runnable int i ; public void run( )while( true)System.out.println(Hello+i+);if (i=5) break ; 7.3.2利用Runnable接口的方法实现多线程 public class ThreadTerminate public static void main(String args ) throws Exceptionint i=0;Hello h = new Hello();Thread t = new Thread( h);t.setPriority(Thread.MAX_PRIORITY);t.start(

温馨提示

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

评论

0/150

提交评论