![《Java基础程序设计》-编程题_第1页](http://file1.renrendoc.com/fileroot_temp2/2020-12/26/436df918-e852-4969-ab40-a6cef984411a/436df918-e852-4969-ab40-a6cef984411a1.gif)
![《Java基础程序设计》-编程题_第2页](http://file1.renrendoc.com/fileroot_temp2/2020-12/26/436df918-e852-4969-ab40-a6cef984411a/436df918-e852-4969-ab40-a6cef984411a2.gif)
![《Java基础程序设计》-编程题_第3页](http://file1.renrendoc.com/fileroot_temp2/2020-12/26/436df918-e852-4969-ab40-a6cef984411a/436df918-e852-4969-ab40-a6cef984411a3.gif)
![《Java基础程序设计》-编程题_第4页](http://file1.renrendoc.com/fileroot_temp2/2020-12/26/436df918-e852-4969-ab40-a6cef984411a/436df918-e852-4969-ab40-a6cef984411a4.gif)
![《Java基础程序设计》-编程题_第5页](http://file1.renrendoc.com/fileroot_temp2/2020-12/26/436df918-e852-4969-ab40-a6cef984411a/436df918-e852-4969-ab40-a6cef984411a5.gif)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第一章 1、请使用Eclipse编写一个程序,程序运行后,在控制台输出“这是我的第一个Java程序”。public class FirstJava public static void main(String args) System.out.println(这是我的第一个Java程序);第二章 1、请编写一个程序,计算100以内所有奇数的和。提示:1) 使用循环语句实现自然数199的遍历。2) 在遍历过程中,通过条件判断当前遍历的数是否为偶数,如果是,就continue,如果是奇数进行叠加运算。public class Demo01 public static void main(Strin
2、g args) int sum = 0;for (int x = 1; x 100; x+) if (x % 2 = 0) continue;sum += x;System.out.println(sum = + sum);2、定义一个函数,找出数组中的最大数或最小数。public class Demo02 public static void main(String args) int array = 5,10,-8,-2,-500,50,200;/最大数int max = array0;for (int i = 1; i max)max = arrayi;System.out.printl
3、n(数组中最大的数是:+max);/最小数int min = array0;for (int i = 1; i array.length; i+) if(arrayi min)min = arrayi;System.out.println(数组中最小的数是:+min);第三章1、编写一个程序,要求创建一个Student类,添加name和age属性,为该属性自动添加相应的getter和setter方法,并给出有参和无参的构造方法。public class Student private String name;private int age;public Student() public Stu
4、dent(String name, int age) super(); = name;this.age = age;public String getName() return name;public void setName(String name) = name;public int getAge() return age;public void setAge(int age) this.age = age;2、编写一个类,类中定义一个静态方法,用于求两个整数的和。请按照以下要求设计一个测试类Demo01,并进行测试。要求如下:1)Demo01类中有一
5、个静态方法get(int a,int b)该方法用户返回参数a、b两个整数的和;2)在main()方法中调用get方法并输出计算结果。public class Demo01 public static int getSum(int a, int b) return a + b;public static void main(String args) int result = Demo01.getSum(2, 3);System.out.println(result);第四章1、定义一个抽象类Car,在该类中包含一个抽象方法run()。分别定义一个Bike类和Bus类继承自Car,在重写的run
6、()方法中分别输出一句话。定义测试类,调用Bike类和Bus类中的方法。abstract class Carabstract void run();class Bike extends Carvoid run() System.out.println(自行车在行驶);class Bus extends Carvoid run() System.out.println(公交车在行驶);public class Demo01 public static void main(String args) Bike bike = new Bike();bike.run();Bus bus = new Bu
7、s();bus.run();2、编写一个程序,模拟计算机的PCI插槽以及各种插卡。主板上的插槽就是计算机中的接口,它可以把显卡、网卡、声卡等都插在PCI插槽上。在计算机启动主板时,这些插槽中的卡也随之启动;关机时,这些卡也随之停止工作。/ PCI接口interface PCI void start();void stop();/ 显卡class Graphics implements PCI public void start() System.out.println(显卡已开启);public void stop() System.out.println(显卡已停止);/ 网卡class N
8、etworkCard implements PCI public void start() System.out.println(网卡已开启);public void stop() System.out.println(网卡已停止);/ 声卡class SoundCard implements PCI public void start() System.out.println(声卡已开启);public void stop() System.out.println(声卡已停止);/ 主板class MainBoard public void PCICardStart(PCI p) p.sta
9、rt();public void PCICardStop(PCI p) p.stop();/ 电脑class Computer private PCI pciArr = new PCI4; / 电脑上的PCI插槽public void add(PCI usb) / 向电脑上安装一个PCI设备for (int i = 0; i pciArr.length; i+) / 循环遍历所有插槽if (pciArri = null) / 如果发现一个空的pciArri = usb; / 将usb设备装在这个插槽上break; / 装上之后结束循环public void turnOn() / 电脑的开机功能
10、for (int i = 0; i pciArr.length; i+) / 循环遍历所有插槽if (pciArri != null) / 如果发现有设备pciArri.start(); / 将PCI设备启动System.out.println(电脑开机成功);public void turnOff() / 电脑的开机功能for (int i = 0; i pciArr.length; i+) / 循环遍历所有插槽if (pciArri != null) / 如果发现有设备pciArri.stop(); / 将PCI设备启动System.out.println(电脑关机成功);public s
11、tatic void main(String args) Computer c = new Computer();c.add(new Graphics();c.add(new NetworkCard();c.add(new SoundCard();c.turnOn();c.turnOff();第五章1、编写一个程序,获取一个已知文件的扩展名。public class Demo01 public static void main(String args) System.out.println(getExtname(Person.java);public static String getExtn
12、ame(String filename)int index = filename.lastIndexOf(.);String extname = filename.substring(index+1);return extname;2、编写一个程序,接收一个字符串,将字符串中每个单词的首字母改为大写。public class Demo02 public static void main(String args) StringBuffer sbn = new StringBuffer(hellow world and happy new year);StringBuffer ss = new S
13、tringBuffer();String s = sbn.toString();String sb = s.split( );for (int i = 0; i sb.length; i+) sbi = sbi.substring(0, 1).toUpperCase() + sbi.substring(1);for (int i = 0; i sb.length; i+) ss.append(sbi);ss.append( );System.out.println(ss);第六章1、编写一个程序,向ArrayList集合中添加5个对象,然后使用迭代器输出集合中的对象。public class
14、Demo01 public static void main(String args) List list = new ArrayList();list.add(zhangsan);list.add(lisi);list.add(wangwu);list.add(zhaoliu);Iterator it = list.iterator();while (it.hasNext() Object object = (Object) it.next();System.out.println(object);2、编写一个程序,向Properties集合存入5个配置项,并迭代出所有的配置项。public
15、 class Demo02 public static void main(String args) Properties props = new Properties();props.setProperty(username, zhangsan);props.setProperty(password, );props.setProperty(email, );Enumeration e = pertyNames();while(e.hasMoreElements() String name = (String) e.nextElement(
16、);String value = props.getProperty(name);System.out.println(name + = + value);第七章1、 编写一个程序,使用定义数组的方式将D盘中的程序拷贝到E盘中。public class Demo01 public static void main(String args) throws IOException / 创建输入流 与源文件相关联InputStream in = new FileInputStream(D:jdk-7u60-windows-i586.exe);/ 创建输出流 与目标文件相关联OutputStream
17、out = new FileOutputStream(E:jdk-7u60-windows-i586.exe);long start = System.currentTimeMillis();copyByBuffer(in, out);long end = System.currentTimeMillis();System.out.println(耗时: + (end-start) + 毫秒); in.close(); out.close();/ 定义 byte数组作为缓冲区进行拷贝private static void copyByBuffer(InputStream in, OutputS
18、tream out) throws IOException byte buffer = new byte1024;int len;while(len=in.read(buffer)!=-1) out.write(buffer, 0, len);2、编写一个程序,遍历出指定目录下所有的.java文件,并将其绝对路径存入一个list集合中输出。public class Demo02 public static void main(String args) / 创建一个 File 对象 封装路径 d:File dir = new File(D:eclipseWorkspaceJavaBasicWor
19、kspacetestsrc);/ 创建一个 List 集合用于存放路径List list = new ArrayList();/ 调用方法listAllJavaFiles(dir, list);/ 输出for(String filename : list)System.out.println(filename);static void listAllJavaFiles(File dir, List list) / 获得 dir 目录中所有的子文件File files = dir.listFiles();/ 如果数组为 null 说明 dir是不可打开的目录或者 不是目录if(files=nul
20、l) return;/ 遍历数组 获得子文件for(File file : files) / 判断if(file.isDirectory() / 说明文件是目录 需要递归调用listAllJavaFiles(file, list); else / 说明是标准文件/ 判断是不是java文件 如果是存入listif(file.getName().endsWith(.java) list.add(file.getAbsolutePath();第八章1、设计一个窗体,窗体中有一个按钮,当单击按钮时,可以添加其它按钮,并按数字依次出现,当单击数字按钮时,被单击按钮消失,此窗体带关闭功能。public c
21、lass Demo01 static int num = 1;public static void main(String args) / 初始化一个frameFrame frame = new Frame(my frame);/ 设置大小frame.setSize(300, 300);/ 设置位置frame.setLocation(100, 100);/ 设置布局管理frame.setLayout(new FlowLayout();/ 添加一个按钮Button btn = new Button(按钮);frame.add(btn);frame.setVisible(true);/ 添加事件监
22、听器 监听窗口事件 / 通过继承 WindowAdapter(适配器)来实现WindowListenerframe.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) e.getWindow().dispose(););/ 为按钮添加事件 鼠标事件 事件源是按钮btn.addMouseListener(new MouseAdapter() public void mouseClicked(MouseEvent e) / 鼠标单击了/ 获得事件源 btnButton btn = (Butto
23、n) e.getComponent();/ 获得 btn 所在的容器 frameFrame frame = (Frame) btn.getParent();/ 添加一个新的btnButton newBtn = new Button(按钮 + num+); frame.add(newBtn);/ 刷新frame的显示frame.setVisible(true);/ 为newBtn 添加事件newBtn.addMouseListener(new MouseAdapter() public void mouseClicked(MouseEvent e) Button btn = (Button) e
24、.getComponent();btn.getParent().remove(btn);););2、编写一个小游戏:设计一窗体,窗体中上下有两个名称为“你来点我啊!”的按钮,当鼠标移动到上面按钮时,上面按钮消失,下面的显示;移动到下面时,下面消失,上面的显示。public class Demo02 static int num = 1;public static void main(String args) / 初始化一个framefinal Frame frame = new Frame(my frame);/ 设置大小frame.setSize(300, 300);/ 设置位置frame.
25、setLocation(100, 100);/ 添加一个按钮final Button btn1 = new Button(你来点我啊!);frame.add(btn1, BorderLayout.NORTH);final Button btn2 = new Button(你来点我啊!);frame.add(btn2, BorderLayout.SOUTH);btn2.setVisible(false);frame.setVisible(true);/ 添加事件监听器,监听窗口事件,/通过继承WindowAdapter(适配器)来实现WindowListenerframe.addWindowLi
26、stener(new WindowAdapter() public void windowClosing(WindowEvent e) e.getWindow().dispose(););btn1.addMouseListener(new MouseAdapter() public void mouseEntered(MouseEvent e) btn1.setVisible(false);btn2.setVisible(true);frame.setVisible(true););btn2.addMouseListener(new MouseAdapter() public void mou
27、seEntered(MouseEvent e) btn2.setVisible(false);btn1.setVisible(true);frame.setVisible(true););第九章1、已知在数据库jdbc中有一个名称为user的表,表中包含三个字段id,name,password。要求使用JDBCUtils工具类编写一个程序,使程序执行后,可以向user表中插入一条数据。(1)JDBCUtils的代码如下:import java.sql.*;/* * 工具类 */public class JDBCUtils / 加载驱动,并建立数据库连接public static Connect
28、ion getConnection() throws SQLException,ClassNotFoundException Class.forName(com.mysql.jdbc.Driver);String url = jdbc:mysql:/localhost:3306/jdbc;String username = root;String password = itcast;Connection conn = DriverManager.getConnection(url, username, password);return conn;/ 关闭数据库连接,释放资源public sta
29、tic void release(Statement stmt, Connection conn) if (stmt != null) try stmt.close(); catch (SQLException e) e.printStackTrace();stmt = null;if (conn != null) try conn.close(); catch (SQLException e) e.printStackTrace();conn = null;public static void release(ResultSet rs, Statement stmt, Connection
30、conn) if (rs != null) try rs.close(); catch (SQLException e) e.printStackTrace();rs = null;release(stmt, conn);(2)实体类User的代码如下:public class User private int id;private String name;private String password;public int getId() return id;public void setId(int id) this.id = id;public String getName() retu
31、rn name;public void setName(String name) = name;public String getPassword() return password;public void setPassword(String password) this.password = password;(3)UserDao的代码如下:import java.sql.*;public class UserDao / 添加数据public void addUser(User user) Connection conn = null;Statement stmt =
32、null;try / 获得数据的连接conn = JDBCUtils.getConnection();/ 获得Statement对象stmt = conn.createStatement();/ 发送SQL语句String sql = INSERT INTO user(id,name,password) + VALUES(+ user.getId() + , + user.getName() + ,+ user.getPassword() + );int num = stmt.executeUpdate(sql);if (num 0) System.out.println(插入数据成功!);
33、catch (Exception e) e.printStackTrace(); finally JDBCUtils.release(stmt, conn);(4)测试类UserTest的代码如下:public class UserTest public static void main(String args) UserDao userDao = new UserDao();User user = new User();user.setId(1);user.setName(itcast);user.setPassword(1234);userDao.addUser(user);第十章1、编写
34、一个程序,创建两个线程,要求分别输出26个字母。在输出结果时,要显示是哪个线程输出的字母。public class Demo01public static void main(String args) Test t1 = new Test(线程一);Test t2 = new Test(线程二);t1.start();t2.start();class Test extends Threadprivate char ch = a;public Test(String name)setName(name);public void run()char temp;for (int i = 0; i 2
35、6; i+) temp = (char) (ch+i);System.out.println(this.getName() + : +temp);2、编写一个程序,使用Runnable接口的方式创建三个线程,分别输出从0到10的数,每个线程之间延迟500毫秒,要求输出的结果如下所示:线程一 : 0,1,2,3,4,5,6,7,8,9,10 线程三 : 0,1,2,3,4,5,6,7,8,9,10 线程二 : 0,1,2,3,4,5,6,7,8,9,10class MyThread implements Runnable Object lock = new Object();/ 定义任意一个对象
36、,用作同步代码块的锁public void run() synchronized (lock) / 定义同步代码块System.out.println();try System.out.print(Thread.currentThread().getName() + : );for (int i = 0; i = 10; i+) System.out.print(i);if (i != 10) System.out.print(,);Thread.sleep(500); / 经过的线程休眠500毫秒 catch (InterruptedException e) e.printStackTrac
37、e();public class Demo02 public static void main(String args) MyThread myThread = new MyThread();new Thread(myThread, 线程一).start();new Thread(myThread, 线程二).start();new Thread(myThread, 线程三).start();第十一章1、简述TCP/IP协议的层次结构。TCP/IP协议的层次结构比较简单,共分为四层,分别是链路层、网络层、传输层和应用层。其中链路层也称为网络接口层,该层负责监视数据在主机和网络之间的交换。网络层
38、也称网络互联层,是整个TCP/IP协议的核心,它主要用于将传输的数据进行分组,将分组数据发送到目标计算机或者网络。传输层主要使网络程序进行通信,在进行网络通信时,可以采用TCP协议,也可以采用UDP协议。应用层主要负责应用程序的协议,例如HTTP协议、FTP协议等。2、简述你对IP地址的认识。要想使网络中的计算机能够进行通信,必须为每台计算机指定一个标识号,通过这个标识号来指定接收数据的计算机或者发送数据的计算机。在TCP/IP协议中,这个标识号就是IP地址,它可以唯一标识一台计算机,目前,IP地址广泛使用的版本是IPv4,它是由4个字节大小的二进制数来表示,由于二进制形式表示的IP地址非常不
39、便记忆和处理,因此通常会将IP地址写成十进制的形式,每个字节用一个十进制数字(0-255)表示,数字间用符号“.”分开,如 “”。1、编写一个程序,实现多线程的UDP网络通信。要求如下: 1) 编写数据接收类ReceiveThread,该类实现了Runnable接口,重写run()方法实现不断接受客户端发送数据的功能。 2) 编写数据发送类SendThread,该类同样实现了Runnable接口,重写run()方法实现通过键盘录入数据,将数据向接收端发送的功能。 3) 编写测试类TestUDP,在main()方法中,同时启动接收端和发送端程序。(1)数据接收类的代码如下所示:i
40、mport java.io.IOException;import .DatagramPacket;import .DatagramSocket;public class ReceiveThread implements Runnable public void run() try / 创建接收端Socket对象DatagramSocket ds = new DatagramSocket(10086);/ 创建数据包while (true) byte bys = new byte1024;DatagramPacket dp = new DatagramPacket
41、(bys, bys.length);/ 接收数据ds.receive(dp);/ 解析数据String ip = dp.getAddress().getHostAddress();String s = new String(dp.getData(), 0, dp.getLength();System.out.println(接收端:从 + ip + 主机接收到的数据是: + s);if (bye.equals(s) System.out.println(*聊天室关闭*);ds.close();break; catch (IOException e) e.printStackTrace();(2
42、)数据发送类的代码如下所示:import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import .DatagramPacket;import .DatagramSocket;import .InetAddress;public class SendThread implements Runnable public void run() try / 创建发送端Socket对象DatagramSocket ds = new D
43、atagramSocket();/ 封装键盘录入BufferedReader br = new BufferedReader(new InputStreamReader(System.in);/ 创建数据,并打包String line = null;while (line = br.readLine() != null) byte bys = line.getBytes();DatagramPacket dp = new DatagramPacket(bys, bys.length,InetAddress.getByName(localhost), 10086);ds.send(dp);if (bye.equals(line) / 释放资源br.close();ds.close();break;/ 释放资源br.close();ds.close(); catch (IOException e) e.printStackTrace();(3)测试类的代码如下所示:public class TestUDP public static void main(String args) SendThread st = new SendThread();ReceiveThrea
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2030全球光学透明粘合带行业调研及趋势分析报告
- 2025合同范本劳务派遣合同模板书人力资源和企业新
- 2025用户服务合同
- 2025委托律师代理合同范本范文
- 土地转让居间合同
- 2025【合同范本】运输道路交通货物合同
- 美容师劳动合同书
- 消杀服务合同范文
- 2025公司用工合同范本
- 战略合作协议书合同
- 第1课+古代亚非(教学设计)【中职专用】《世界历史》(高教版2023基础模块)
- 新教科版六年级下册科学全册教案
- 物业客服管家的培训课件
- 2024年房地产行业的楼市调控政策解读培训
- 《统计学-基于Python》 课件全套 第1-11章 数据与Python语言-时间序列分析和预测
- 《GMP实务教程》 完整全套教学课件 项目1-14 GMP基础知识-药品生产行政检查
- 装饰定额子目(河南省)
- 【高速铁路乘务工作存在的问题及对策研究9800字】
- 北师大版英语课文同步字帖三年级下册课文对话原文及翻译衡水体英语字帖三年级起点
- GB/T 2550-2016气体焊接设备焊接、切割和类似作业用橡胶软管
- GB/T 21295-2014服装理化性能的技术要求
评论
0/150
提交评论