1、Java语言进阶实验指导书_Java语言进阶实验指导书福建农林大学计算机与信息学院实验一 Generic and Collections Framwork泛型与集合框架 一、实验目的:1. 了解Java集合框架的接口和实现类2. 理解泛型类、泛型接口、泛型方法的特点3. 掌握常用泛型集合接口及其实现类二、实验内容:1. 编写程序练习List集合的基本使用:2.设计学生管理类StudentManager(用List集合管理学生对象)。StudentManager类的功能包括添加学生、查询学生、删除学生、统计学生成绩等。需要设计表示学生对象的Student类,并用LinkedList或ArrayL
2、ist集合来管理可被数量的学生对象。另外还需要设计测试类Test来验证StudentManager的功能。 3. (Optional ) Write a generic method that returns the maximum element in a two-dimensional array.public static E extends Comparable E max(E list)三、实验要求:1、认真执行每一个步骤的,并作好记录。2、实验报告中给出配置的详细步骤和重要的截图。四、实验学时:2学时五、实验步骤:1、编写程序练习List集合的基本使用:1) 创建一个只能容纳Str
3、ing对象名为names的ArrayList集合;2)按顺序往集合中添加5个字符串对象:“张三”、“李四”、“王五”、“马六”、“赵七”;3)对集合进行遍历,分别打印集合中的每个元素的位置与内容;4)首先打印集合的大小,然后删除集合中的第3个元素,并显示删除元素的内容,然后再打印目前集合中第3个元素的内容,并再次打印集合的大小。代码如下:package zhoupeili;import java.util.*;public class L1 public static void main(String args) List names = new ArrayList(); names.add(
4、张三); names.add(李四); names.add(王五); names.add(马六); names.add(赵七); for (int i = 0; i names.size(); i+) System.out.println(第 + (i + 1) + 条数据 为: + names.get(i); System.out.println(集合大小为: + names.size(); names.remove(2); for (int i = 0; i names.size(); i+) System.out.println(第 + (i + 1) + 条数据 为: + names.
5、get(i); System.out.println(第3条数据为:+ names.get(2); System.out.println(集合大小为: + names.size(); 运行上述代码,观察并记录运行结果。2.设计学生管理相应类,比较与上一段代码的重要区别。实验二 Multithreading多线程 一、实验目的:1. 理解多线程的原理和机制2. 掌握Java内置多线程编程接口和相关的类_ dgdo pass sum by ref- _二、实验内容:1. Write a program that launches 1000 threads. Each thread adds 1 t
6、o a variable sum that initially is 0. You need to pass sum by reference to each thread. In order to pass it by reference, define an Integer wrapper object to hold sum. Run the program with and without synchronization to see its effect.2. .分析代码并解释三、实验要求:1、认真执行每一个步骤的,并作好记录。2、实验报告中给出配置的详细步骤和重要的截图。四、实验学
7、时:2学时五、实验步骤:1. Write a program that launches 1000 threads. Each thread adds 1 to a variable sum that initially is 0. You need to pass sum by ref- erence to each thread. In order to pass it by reference, define an Integer wrapper object to hold sum. Run the program with and without synchroniza- tion
8、to see its effect.2. dgdo pass sum by ref- _运行下列代码,记录结果,并分析死锁的原因 / Exercise29_11.java: Demonstrate dead lockpublic class Exercise29_11 private Object object1 = new Object(); private Object object2 = new Object(); public static void main(String args) Exercise29_11 test = new Exercise29_11(); public E
9、xercise29_11() new Thread1().start(); new Thread2().start(); class Thread1 extends Thread public void run() synchronized (object1) / do something try Thread.sleep(100); catch (InterruptedException ex) System.out.println(Attempt to acquire a lock on object2); synchronized (object2) / do something cla
10、ss Thread2 extends Thread public void run() synchronized (object2) / do something try Thread.sleep(100); catch (InterruptedException ex) System.out.println(Attempt to acquire a lock on object1); synchronized (object1) / do something 实验三 Networking网络编程 一、实验目的:1. 理解基于流与基于数据包的网络编程方式2. 掌握应用socket建立CS应用程
11、序的相关类的应用_ dgdo pass sum by ref- _二、实验内容: Write a program that enables two users to chat. Implement one user asthe server (Figure 30.24(a) and the other as the client (Figure 30.24(b). Theserver has two text areas: one for entering text and the other (noneditable) fordisplaying text received from the
12、 client. When the user presses the Enter key,the current line is sent to the client. The client has two text areas: one for receivingtext from the server, and the other for entering text. When the userpresses the Enter key, the current line is sent to the server. Name the clientExercise30_12Client a
13、nd the server Exercise30_12Server.三、实验要求:1、认真执行每一个步骤的,并作好记录。2、实验报告中给出配置的详细步骤和重要的截图。四、实验学时:2学时五、实验步骤:给定如下客户端的代码,请完成服务器端代码的编写。/ Exercise30_12Client.javaimport java.io.*;import .*;import java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;public class
14、Exercise30_12Client extends JFrame / Text area for entering server text private JTextArea jtaServer = new JTextArea(); / Text area for displaying client text private JTextArea jtaClient = new JTextArea(); private PrintWriter output; public static void main(String args) new Exercise30_12Client(); pub
15、lic Exercise30_12Client() / Place text area on the frame setLayout(new GridLayout(2, 1); JScrollPane jScrollPane1 = new JScrollPane(jtaServer); JScrollPane jScrollPane2 = new JScrollPane(jtaClient); jScrollPane1.setBorder(new TitledBorder(Server); jScrollPane2.setBorder(new TitledBorder(Client); add
16、(jScrollPane2, BorderLayout.CENTER); add(jScrollPane1, BorderLayout.CENTER); jtaServer.setWrapStyleWord(true); jtaServer.setLineWrap(true); jtaClient.setWrapStyleWord(true); jtaClient.setLineWrap(true);/ jtaClient.setEditable(false); setTitle(Exercise30_12Client); setSize(500, 300); setDefaultCloseO
17、peration(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); / Center the frame setVisible(true); / It is necessary to show the frame here! try / Create a server socket Socket connectToServer = new Socket(localhost, 8000); output = new PrintWriter(connectToServer.getOutputStream(); new SendThread(co
18、nnectToServer).start(); new ReceiveThread(connectToServer).start(); catch(IOException ex) System.err.println(ex); jtaServer.addKeyListener(new KeyAdapter() public void keyPressed(KeyEvent e) if (e.getKeyCode() = e.VK_ENTER) output.print(jtaServer.getText(); ); class SendThread extends Thread SendThr
19、ead(Socket socket) / Keep sending to the client class ReceiveThread extends Thread ReceiveThread(Socket socket) / Keep receiving from the client 实验四 MVC in GUI (GUI中的MVC模式) 一、实验目的:1. 掌握MVC设计模式的概念。2. 掌握应用MVC模式开发图形用户界面应用程序的方法。_ dgdo pass sum by ref- _二、实验内容: (Creating a student table ) Create a table
20、for student records. Each record consistsof name, birthday, class status, in-state, and a photo, as shown in Figure 1. The name is of the String type; birthday is of the Date type; class status is one of the following five values: Freshman, Sophomore, Junior, Senior, or Graduate; in-state is a boole
21、an value indicating whether the student is a resident of the state; and photo is an image icon. Use the default editors for name, birthday, and in-state. Supply a combo box as custom editor for class status.代码d_报一个重要信报_Figure 1.三、实验要求:1、认真执行每一个步骤的,并作好记录。2、实验报告中给出配置的详细步骤和重要的截图。四、实验学时:2学时五、实验步骤:(请给出详细
22、代码,并加入代码注释,注释是成绩高低和判定是否抄袭的一个重要依据。)实验五 JDBC Programming (JDBC数据库编程) 一、实验目的:掌握应用JDBC技术访问数据库的方法。_ dgdo pass sum by ref- _二、实验内容: (Accessing and updating a Staff table) Write a Java application that views, inserts,and updates staff information stored in a database, as shown in Figure 1.The View button d
23、isplays a record with a specified ID. The Staff table is createdas follows:create table Staff (id char(9) not null,lastName varchar(15),firstName varchar(15),mi char(1),address varchar(20),city varchar(20),state char(2),telephone char(10),email varchar(40),primary key (id);Figure 1.三、实验要求:1、认真执行每一个步骤的,并作好记录。2、实验报告中给出配置的详细步骤和重要的截图。四、实验学时:2学时五、实验步骤:(请给出详细代码,并加入代码注释,注释是成绩高低和判定是否抄袭的一个重要依据。)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1