Java程序的设计实验2.doc

上传人:夺命阿水 文档编号:13696 上传时间:2022-06-28 格式:DOC 页数:19 大小:104KB
返回 下载 相关 举报
Java程序的设计实验2.doc_第1页
第1页 / 共19页
Java程序的设计实验2.doc_第2页
第2页 / 共19页
Java程序的设计实验2.doc_第3页
第3页 / 共19页
Java程序的设计实验2.doc_第4页
第4页 / 共19页
Java程序的设计实验2.doc_第5页
第5页 / 共19页
点击查看更多>>
资源描述

《Java程序的设计实验2.doc》由会员分享,可在线阅读,更多相关《Java程序的设计实验2.doc(19页珍藏版)》请在课桌文档上搜索。

1、 实验1面向对象编程实验一、实验目的和要求1理解Java概念、掌握JDK环境配置2熟悉Java开发过程3掌握Java面向对象编程根底:封装、继承、多态4掌握Java接口编程,理解开发模式二、实验仪器和设备奔腾以上个人计算机, windows操作系统。配置好JDK环境,安装集成开发环境Eclipse三、实验容与过程1、JDK环境配置2、面向对象的封装性例:设计一个表示学生的类,里面有学生的三项成绩:计算机成绩、数学成绩、英语成绩。要求可以求总分、平均分、最高分、最低分,并且可以输出一个学生的完整信息。代码如下:class Studentprivate String name ;private i

2、nt age ;private float english ;private float computer ;private float math ;public Student()public Student(String n,int a,float e,float c,float m)this.setName(n) ;this.setAge(a) ;this.setEnglish(e) ;this.setComputer(c) ;this.setMath(m) ;public float sum()return english + computer + math ;public float

3、 avg()return this.sum() / 3 ;public float max()float max = computermath?computer:math ;max = maxenglish?max:english ;return max ;public float min()float min = computermath?computer:math ;min = min0)this.temp = new intlen ;/ 此时大小由外部决定elsethis.temp = new int1 ;/ 至少开辟一个空间public boolean add(int i)/ 参加数据

4、操作if(this.footthis.temp.length)/ 还有空位this.tempthis.foot = i ;/ 参加容this.foot+ ;/ 改变长度return true ;/ 参加成功返回trueelsereturn false ;/ 参加失败public int getArray()/ 返回全部的数组return this.temp ;class SortArray extends Arraypublic SortArray(int len)super(len) ;public int getArray()java.util.Arrays.sort(super.getA

5、rray() ;/ 排序操作return super.getArray() ;/ 返回的是排序后的容class ReverseArray extends Arraypublic ReverseArray(int len)super(len) ;public int getArray()int rt = new intsuper.getArray().length ;/ 根据大小开辟新数组int count = rt.length-1 ;for(int x=0;xsuper.getArray().length;x+)rtcount = super.getArray()x ;count- ;ret

6、urn rt ;public class ArrayDemopublic static void main(String args)ReverseArray arr = new ReverseArray(6) ;System.out.println(arr.add(3) ;System.out.println(arr.add(23) ;System.out.println(arr.add(1) ;System.out.println(arr.add(5) ;System.out.println(arr.add(6) ;System.out.println(arr.add(8) ;System.

7、out.println(arr.add(11) ;System.out.println(arr.add(16) ;print(arr.getArray() ;public static void print(int i)for(int x=0;xi.length;x+)System.out.print(ix + 、) ;对照例写出如下题目:1.创建GrandFather类,其中包括a)属性:name,年龄ageb)方法getGrandFather():显示爷爷的信息c)构造方法:给爷爷的,年龄赋值(2).创建Father类,继承Grandfather类a)属性:除了继承爷爷的属性以外,还要增加

8、自己的属性:“职业(occupation)b)构造方法:显式调用父类的构造方法,为Father类的和年龄赋初始值。再为职业输入初始值。c 方法getFather(): 显示父亲的相关信息(3). 创建ClassMain()类,定义main()方法,构造GrandFather类的对象和Father类的对象,并分别显示详细信息。3、面向对象多态性例:计算柱体的体积。柱体体积计算公式是:底部面积乘以高度柱体底局部为 圆形和矩形要求:通过抽象类和多态实现package.jit.demo;abstract class Bottom /父类抽象类 底部public abstract double calc

9、ulatorArea();class CircleBottom extends Bottom/圆形底/* * 半径 */private double radius;Overridepublic double calculatorArea() return Math.PI * radius * radius;public double getRadius() return radius;public void setRadius(double radius) this.radius = radius;public CircleBottom(double radius) super();this.

10、radius = radius;class SquareBottom extends Bottom/矩形底private double sideA;private double sideB;public double getSideA() return sideA;public void setSideA(double sideA) this.sideA = sideA;public double getSideB() return sideB;public void setSideB(double sideB) this.sideB = sideB;Overridepublic double

11、 calculatorArea() return sideA * sideB;public SquareBottom(double sideA, double sideB) super();this.sideA = sideA;this.sideB = sideB;class ZhuTi /柱体类,完成形状的拼装/* * 底 */private Bottom bottom;/* * 高 */private double height;/* * 计算体积 * return */public double calculatorVolumn()return bottom.calculatorArea

12、() * height;public ZhuTi(Bottom bottom, double height) super();this.bottom = bottom;this.height = height;public Bottom getBottom() return bottom;public void setBottom(Bottom bottom) this.bottom = bottom;public double getHeight() return height;public void setHeight(double height) this.height = height

13、;public void changeBottom(Bottom bottom)this.bottom = bottom;public class VolumnTest /测试类public static void main(String args) Bottom bottom = new CircleBottom(1.0);double height = 1.0;ZhuTi zhuTi = new ZhuTi(bottom,height);double result = zhuTi.calculatorVolumn();System.out.println(圆柱体的体积是: + result

14、);bottom = new SquareBottom(1.0,1.0);zhuTi.changeBottom(bottom);result = zhuTi.calculatorVolumn();System.out.println(立方体的体积是: + result);例:接口和多态的应用,例如:电脑上实现了USB接口,U盘,打印机等等也都实现了此标准。interface USBpublic void start() ;/ 开始工作public void stop() ;/ 完毕工作class Computerpublic static void plugin(USB usb)usb.sta

15、rt() ;usb.stop() ;class Flash implements USBpublic void start()System.out.println(U盘开始工作。) ;public void stop()System.out.println(U盘停止工作。) ;class Print implements USBpublic void start()System.out.println(打印机开始工作。) ;public void stop()System.out.println(打印机停止工作。) ;public class InterPolDemo02public stat

16、ic void main(String args)Computer.plugin(new Flash() ;Computer.plugin(new Print() ;对照例,写出以下程序:(1) 乐器Instrument的标准为弹奏play,而乐器类型分为:钢琴Piano和小提琴Violin,各种乐器的弹奏方法各不同。编写代码实现不同乐器的弹奏。(2) 计算机模拟四、实验结果与分析程序运行结果与其分析五、实验体会实验项目名称:类集 实验学时: 4 同组学生:实验地点:实验日期:实验成绩:批改教师:批改实验2 类集一、实验目的和要求1理解类集概念2熟悉Collection接口、List接口、Se

17、t接口和Map接口3掌握ArrayList类、HashSet类和TreeSet类4理解TreeMap、HashMap二、实验仪器和设备奔腾以上个人计算机, windows操作系统。配置好JDK环境,安装集成开发环境Eclipse三、实验容与过程1、类集应用例:实现一个超市管理系统,要求可以添加货物,删除货物和查询货物:。代码如下:public interface Goods public String getName(); / 得到商品名称public int getCount(); / 得到商品数量public float getPrice(); / 得到商品价格 public class

18、Book implements Goods private String name;private int count;private float price;public Book() public Book(String name, int count, float price) this.name = name;this.count = count;this.price = price;public String getName() return name;public void setName(String name) this.name = name;public int getCo

19、unt() return count;public void setCount(int count) this.count = count;public float getPrice() return price;public void setPrice(float price) this.price = price;public boolean equals(Object obj) if (this = obj) return true;if (!(obj instanceof Book) return false;Book b = (Book) obj;if (b.name.equals(

20、this.name) & b.count = this.count& b.price = this.price) return true; else return false;public int hashCode() return this.name.hashCode() + new Integer(this.count).hashCode()+ new Float(this.price).hashCode();public String toString() return 书名: + this.name + ;书的价格: + this.price + ;书的数量:+ this.count;

21、import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class SuperMarket private List allGoods;public SuperMarket() this.allGoods = new ArrayList();public void add(Goods goods) this.allGoods.add(goods);public void remove(Goods goods) this.allGoods.remove(goods);public List

22、 search(String keyWord) List temp = new ArrayList();Iterator iter = this.allGoods.iterator();while (iter.hasNext() Goods g = iter.next();if (g.getName().indexOf(keyWord) != -1) temp.add(g);return temp;public List getAllGoods() return this.allGoods;import java.util.Iterator;import java.util.List;publ

23、ic class Test public static void main(String args) SuperMarket mak = new SuperMarket();mak.add(new Book(Java, 2, 30.9f);mak.add(new Book(C+, 3, 10.9f);mak.add(new Book(JSP, 5, 80.9f);print(mak.search(J) ;mak.remove(new Book(Java, 2, 30.9f) ;print(mak.search(J) ;public static void print(List all) Ite

24、rator iter = all.iterator();while (iter.hasNext() System.out.println(iter.next();对照例写出如下题目:1宠物商店,要求可以添加、删除和查找宠物2实现以下两个关系A、一个学校可以有多个学生,所有学生属于一个学校B、一门课程可以有多个学生选,一个学生可以选多门课程四、实验结果与分析程序运行结果与其分析五、实验体会实验项目名称: Java IO操作实验学时: 6 同组学生:实验地点:实验日期:实验成绩:批改教师:批改实验3 Java IO操作一、实验目的和要求1理解输入输出流概念2掌握文件输入输出流3掌握键盘的输入、显示

25、器的输出4理解其他输入输出流二、实验仪器和设备奔腾以上个人计算机, windows操作系统。配置好JDK环境,安装集成开发环境Eclipse三、实验容与过程1、编写类模拟命令Copy 例:实现文件的复制代码。参考代码如下:File file1 = new File(“d:+File.seperator +demo.txt); / 找到第一个文件的File对象File file2 = new File(“d:+File.seperator +cemo.txt); / 找到目标文件路径InputStream input = new FileInputStream(file1); / 输入流Outp

26、utStream output = new FileOutputStream(file2);/ 输出流int temp = 0; / 定义一个整数表示接收的容while (temp = input.read() != -1) / 表示还有容可以继续读output.write(temp);/ 写入数据input.close(); / 关闭output.close();/ 关闭2、通过键盘的输入,实现简单的选项操作。*#管理系统*1添加2删除3修改4查询5退出 3、编写一个简单管理系统,实现真实的操作。四、实验结果与分析程序运行结果与其分析五、实验体会实验项目名称: JDBC 实验学时: 6 同组

27、学生:实验地点:实验日期:实验成绩:批改教师:批改实验4 JDBC一、实验目的和要求1理解JDBC分类2掌握JDBC数据库连接步骤3掌握JDBC连接MySQL数据库代码4理解JDBC连接其他数据库方式二、实验仪器和设备奔腾以上个人计算机, windows操作系统。配置好JDK环境,安装集成开发环境Eclipse三、实验容与过程1、安装MySQL数据库,配置好数据库创建一个数据库表,按要求给出详细的字段设计pid name age birthday salary主要操作:2、创建Eclipse项目,配置驱动包每个数据库厂商都会提供对Java开发技术的支持,即都会提供对应的Java驱动,也就是一个jar包主要操作:3、项目中建立一个详细例子,按照要求连接、操作、关闭数据库按照标准的步骤完成对MySQL数据库的操作主要代码:添加、修改、删除和查询4、试着连接其他类型数据库。四、实验结果与分析程序运行结果与其分析五、实验体会19 / 19

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 在线阅读 > 生活休闲


备案号:宁ICP备20000045号-1

经营许可证:宁B2-20210002

宁公网安备 64010402000986号