java程序设计例题_第1页
java程序设计例题_第2页
java程序设计例题_第3页
java程序设计例题_第4页
java程序设计例题_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

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

文档简介

Java 程序设计程序设计 1 根据如下图所示的 UML 图设计各类和接口 然后再创建一个包含 main 的类来测试这些类 和接口 要求输出以下文本 鱼喜欢吃草 狗喜欢啃骨头 鱼使用螵呼吸 狗使用肺呼吸 狗正在和一只可怜的老鼠逗着玩呢 类和接口的层次图 2 2 定义一个泛型类 Point 它包含横坐标 x 和纵坐标 y 两个变量 类型均为 T 该类具有两个参 数的构造方法 x 和 y 的设置器与访问器 输出方法 在 main 函数中分别传入 Double Float Integer 类型数据加以验证 3 编程题 先创建一个 List 集合 cards 再 54 张扑克牌存放到其中 请按下列要求编写程序 1 用 3 种不同方法输出 54 张牌 2 调用 Collections 方法随机分派 54 张牌 4 从命令行中输入两个双精度的浮点数 计算它们的商 输入的内容不一定符合要求 希望程 序能够捕获 NumberFormatException 异常和 ArithmeticException 异常 5 利用 File 类和递归方法 列出指定目录下的文件及各级子目录包含的内容 6 编程实现 利用 FileOutputStream 类向 myfile txt 文件写入 0 9 和字符串 文件和输入输出 流 内容 然后利用 FileInputStream 类以 逐字节 方式输出其内容 7 若要将信息 Java 面向对象编程 书名 孙卫琴 作者 65 8 价格 等信息 分别以 UTF double 类型数据保存到文件 books txt 中 请用 数据流 类编程实现 提示 可用数据 流包装文件字节流方法实现 8 使用字节流实现文件的拷贝 要求一次能够读写多个字节 如 512 字节等 9 编程实现 从键盘输入若干名学生的信息资料 学号 姓名 年龄 按 Ctrl z 结束 然后将 输入的所有信息保存到文件 students txt 中 然后再打开 读取该文本文件 并输出其内容 10 请创建一个类 Book 它的属性有 bookName 书名 authors 作者 ISBN ISBN 号 price 价格 pressHouse 出版社 并让它具备对象序列化功能 然后用 2 个对象来演示序列化 反序列化的实现步骤 第一题 interface Pet void play abstract class Animal protected int legs protected Animal int legs this legs legs abstract void eat abstract void breathe class Fish extends Animal implements Pet public Fish super 0 public void eat System out print 鱼喜欢吃草 public void breathe System out print 鱼使用螵呼吸 public void play System out println class Dog extends Animal implements Pet public String name public Dog super 4 public void setName String name this name name public String getName return name public void eat System out println 狗喜欢啃骨头 public void breathe System out println 狗使用肺呼吸 public void play System out println 狗正在和一只可怜的老鼠逗着玩呢 public class 一 public static void main String args Fish fish new Fish Dog dog new Dog fish eat dog eat fish breathe dog breathe dog play 第二题 class Point private T information public Point public Point T info this information info public void setInfo T info this information info public T getInfo return this information public class 二 public static void main String args Point p1 new Point new Double 22 58 System out println p1 getInfo Point p2 new Point new Float 50 7 System out println p2 getInfo Point p3 new Point new Integer 100 System out println p3 getInfo 第三题 import java util public class 三 public static void main String args TODO Auto generated method stub String number A 2 3 4 5 6 7 8 9 10 J Q K String color 黑桃 红桃 梅花 方块 List Card new ArrayList for int i 0 i color length i for int n 0 n number length n Card add color i number n Card add 小王 Card add 大王 Collections shuffle Card Iterator it Card iterator System out println 第一种输出方法 while it hasNext System out print it next System out println System out println 第二种输出方法 Collections shuffle Card for int i 0 i Card size i System out print Card get i System out println System out println 第三种输出方法 Collections shuffle Card for String s Card System out print s 第四题 class myException extends Exception myException String msg super msg class 四 public static void main String args myException f new myException Message int x y try x 10 y 0 x 10 0 System out println x x catch NumberFormatException e System out println In built Message e getMessage System out println In NumberFormatException Message f getMessage catch ArithmeticException e System out println In built Message e getMessage System out println In ArithmeticException Message f getMessage 第五题 import java io public class 五 public static void main String args throws IOException File file new File D 123 getTotal file public static void getTotal File file if file null if file isDirectory File str file listFiles if file null for int i 0 i str length i getTotal str i else System out println file 第六题 import java io class 六 public static void main String args throws IOException File f new File myfile txt FileOutputStream outfile new FileOutputStream f try for int i A i Z i outfile write i 写入int型数据 outfile write t 写入制表位 outfile write 123456789 byte buf 文件和输入输出流 getBytes 将字符串转换 字节数组 outfile write buf 写入字节数组数据 outfile write r 写入回车符 outfile write n 写入换行符 System out println 文件内容写入完毕 catch IOException e System out println e getMessage finally outfile close 关闭输出流 import java io class 六 public static void main String args throws IOException File f new File d myfile txt FileOutputStream outfile new FileOutputStream f try for int i 0 i 字符流 缓冲流 import java io public class 九 public static void main String args throws IOException 读取 键盘是字节输入流 先转换成字符输入流 再包装成缓冲字符输入流 InputStreamReader isr new InputStreamReader System in BufferedReader br new BufferedReader isr 写入 先得到文件字符输出流 再包装成缓冲字符输出流 FileWriter fw new FileWriter students txt BufferedWriter bw new BufferedWriter fw System out println 请输入若干名学生的信息资料 学号 姓名 年龄 按 Ctrl Z结束 String data 逐行读取 写入 while data br readLine null bw write data bw newLine br close bw close System out println 文件创建完毕 第十题 序列化 反序列化的例子 import java io Book类 可序列化 实现Serializabler接口 class Book implements Serializable String bookName 书名 String authors 作者 String ISBN ISBN号 double price 价格 String pressHouse 出版社 Book String bo String au String is double pri String pre 构造方法 bookName bo authors au ISBN is price pri pressHouse pre public void displsy System out print bookName t System out print authors t System out print ISBN t System out print price t System out print pressHouse n public class 十 public static void main String args 创建两个对象 Book J new Book Java程序设计 蔡木生 FE01 45 0 大连理工大 学出版社 Book C new Book C 程序设计 谭浩强 CC01 33 0 清华大学出版 社 以下为序列化操作 try 创建文件字节输出流 并以此生成对象输出流 FileOutputStream fi

温馨提示

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

评论

0/150

提交评论