Java程序设计实验计算机软件及应用IT计算机专业资料_第1页
Java程序设计实验计算机软件及应用IT计算机专业资料_第2页
Java程序设计实验计算机软件及应用IT计算机专业资料_第3页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

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

文档简介

1、盒陂科扶亭院学生实验报告册(理工类)课程名称:学生学号:所属院部:专业班级:学生姓名:指导教师:2020学年第学期金陵科技学院教务处制 实验报告书写要求 实验报告原则上要求学生手写, 要求书写工整。 若因课程特点需 打印的,标题采用四号黑体,正文采用小四号宋体,单倍行距。纸张 一律采用 A4 的纸张。实验报告书写说明 实验报告中实验目的和要求、 实验仪器和设备、实验内容与过程、 实验结果与分析这四项内容为必需项。 教师可根据学科特点和实验具 体要求增加项目。填写注意事项(1)细致观察,及时、准确、如实记录。(2)准确说明,层次清晰。(3)尽量采用专用术语来说明事物。(4)外文、符号、公式要准确

2、,应使用统一规定的名词和符号。(5)应独立完成实验报告的书写,严禁抄袭、复印,一经发现, 以零分论处 。实验报告批改说明 实验报告的批改要及时、认真、仔细,一律用红色笔批改。实验 报告的批改成绩采用五级记分制或百分制, 按金陵科技学院课堂教 学实施细则中作业批阅成绩评定要求执行。实验报告装订要求实验批改完毕后, 任课老师将每门课程的每个实验项目的实验报 告以自然班为单位、按学号升序排列,装订成册,并附上一份该门课 程的实验大纲。实验项目名称:面向对象编程实验实验学时:8同组学生姓名:实验地点:实验日期:实验成绩:批改教师:批改时间:实验 1 面向对象编程实验一、实验目的和要求(1理解Java概

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

4、ge ;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

5、avg()return this.sum() / 3 ;public float max()float max = computer>math?computer:math ;max = max>english?max:english ; return max ;public float min()float min = computer<math?computer:math ; min = min<english?min:english ; return min ;public String getInfo() return " 学生信息: n" +

6、"t|- 姓名:" + this.getName() + "n" +"t|- 年龄:" + this.getAge() + "n" +"t|- 数学成绩: " + this.getMath() + "n" +"t|- 英语成绩: " + this.getEnglish() + "n" +"t|- 计算机成绩: " + this.getComputer() ;public void setName(String n)

7、name = n ;public void setAge(int a) age = a ; public void setEnglish(float e) english = e ;public void setComputer(float c) computer = c ;public void setMath(float m) math = m ;public String getName() return name ;public int getAge() return age ;public float getEnglish() return english ;public float

8、 getComputer() return computer ;public float getMath() return math ;public class ExecDemopublic static void main(String args)Student stu = new Student(" 张三 ",30,89.0f,91.0f,87.0f) ; System.out.println("总分: " + stu.sum() ;System.out.println(" 平均分: " + stu.avg() ;System.o

9、ut.println(" 最高分: " + stu.max() ;System.out.println(" 最低分: " + stu.min() ; System.out.println(stu.getInfo() ;对照范例写出如下题目:( 1)编写并测试一个代表地址的 Address 类,包括以下的属性:国家、省份、城市、 街道、邮编。可以从此地址类中得到一个完整的地址信息。(2) 定义并测试一个代表员工的 Employee类。它的属性包括“员工姓名”、“员工号码”、“员工基本薪水” 、“员工薪水增长额” ;它的方法包括“构造 方法”、“获取员工姓名

10、”、“获取员工号码”、“获取员工基本薪水” 、“计算薪 水增长额”及“计算增长后的工资总额” 。(3) 、定义一个圆形类,可以返回圆的面积与周长。( 4)、思考设计:一个人有一本书,一本书属于一个人。3、面向对象的继承性范例:要求定义一个数组类 Array ,里面定义了一个整型数组,但是此整型数组属于动 态分配大小,即:所有的大小由程序指定,并在此基础上实现以下的两个子类:反转类:可以将数组的内容反转排列排序类:可以对数组进行排序的操作class Arrayprivate int temp = null ; / private int foot = 0 ;public Array(int le

11、n) if(len>0)this.temp = new intlen ;elsethis.temp = new int1 ;public boolean add(int i) if(this.foot<this.temp.length) this.tempthis.foot = i ; this.foot+ ; / return true ; /只是声明数组,但是大小未知/ 用于保存下一个的记录点/ 此时大小由外部决定/ 至少开辟一个空间/ 加入数据操作/ 还有空位/ 加入内容 改变长度 加入成功返回 trueelsereturn false ; public int getArr

12、ay() return this.temp ;/ 加入失败/ 返回全部的数组class SortArray extends Arraypublic SortArray(int len)super(len) ;public int getArray()java.util.Arrays.sort(super.getArray() ; /排序操作return super.getArray() ;/ 返回的是排序后的内容class ReverseArray extends Arraypublic ReverseArray(int len)super(len) ;public int getArray(

13、)int rt = new intsuper.getArray().length ; / 根据 大小开辟 新数组int count = rt.length-1 ;for(int x=0;x<super.getArray().length;x+)rtcount = super.getArray()x ;count- ;return rt ;public class ArrayDemopublic static void main(String args)ReverseArray arr = new ReverseArray(6) ;System.out.println(arr.add(3)

14、 ;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.out.println(arr.add(11) ;System.out.println(arr.add(16) ; print(arr.getArray() ;public static void print(int i)for(int x=0;x<i.lengt

15、h;x+)System.out.print(ix + "、") ; 对照范例写出如下题目:( 1) . 创建 GrandFather 类,其中包括a) 属性:姓名(name ,年龄(age)b) 方法 getGrandFather() :显示爷爷的信息c) 构造方法:给爷爷的姓名,年龄赋值(2) .创建 Father 类,继承 Grandfather 类a) 属性:除了继承爷爷的属性以外,还要增加自己的属性:“职业”(occupation)b) 构造方法:显式调用 GrandFather 类的构造方法,为 GrandFather 类的姓名 和年龄赋初始值。再为自己职业输入初

16、始值。c) 方法 getFather(): 显式调用 GrandFather 类的 getGrandFather() ,再显示 父亲的相关信息 String 返回值(3) .创建主类,定义 main() 方法,构造 GrandFather 类的对象和 Father 类的对象,并分别显示详细信息。 思考:覆盖 将父类和子类的显示信息函数,写成统一的 getInfo 3、面向对象多态性 范例:计算柱体的体积。柱体体积计算公式是:底部面积乘以高度 柱体底部分为 圆形和矩形 要求:通过抽象类和多态实现package cn.jit.demo;abstract class Bottom / 父类抽象类 底

17、部public abstract double calculatorArea();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(dou

18、ble radius) super();this.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 = s

19、ideB;Overridepublic double 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 b

20、ottom.calculatorArea() * 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)

21、this.height = height;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.pri

22、ntln(" 圆柱体的体积是: " + result); bottom = new SquareBottom(1.0,1.0); zhuTi.changeBottom(bottom);result = zhuTi.calculatorVolumn();System.out.println(" 立方体的体积是: " + result); ,打印机等等也范例:接口和多态的应用,例如:电脑上实现了USB接 口,都实现了此标准。interface USBpublic void start() ; / 开始工作public void stop() ; /结束工作c

23、lass Computerpublic static void plugin(USB usb)usb.start() ;usb.stop() ;class Flash implements USBpublic void start() System.out.pri ntln ("Upublic void stop() System.out.pri ntln ("U;class Print impleme nts USBpublic void start() System.out.pri ntl n(”public void stop() System.out.pri ntl

24、 n(”;public class In terPolDemo02public static void mai n(Stri ng args) Computer.plug in(new Flash(); Computer.plug in(new Prin t();盘开始工作。盘停止工作。打印机开始工作打印机停止工作IIIIIIII;对照范例,写出以下程序:(1) 乐器(In strume nt)的标准为弹奏(play),而乐器类型分为:钢琴(Pia no) 和小提琴(Violin ),各种乐器的弹奏方法各不同。编写代码实现不同乐 器的弹奏。(2)计算机模拟四、实验结果与分析(程序运行结果及其分

25、析)五、实验体会实验项目名称: 实验学时:4同组学生姓名:实验地点:实验日期:实验成绩:批改教师:批改时间:实验 2 类集一、实验目的和要求(1)理解类集概念(2)熟悉 Collection 接口、List 接口、Set 接口和 Map接口( 3)掌握 ArrayList 类、 HashSet 类和 TreeSet 类( 4)理解 TreeMap、 HashMap二、实验仪器和设备奔腾以上个人计算机, windows 操作系统。配置好JDK环境,安装集成开发环境(Eclipse )三、实验内容与过程1、类集应用 范例:实现一个超市管理系统,要求可以添加货物,删除货物和查询货 物:。代码如下:p

26、ublic interface Goods 得到商品名称public String getName(); /public int getCount(); /得到商品数量public float getPrice(); /得到商品价格public class Book implements Goods private String name;private int count;private float price;public Book() public Book(String name, int count, float price) = name; this.count

27、 = count;this.price = price;public String getName() return name;public void setName(String name) = name;public int getCount() return count;public void setCount(int count) this.count = count;public float getPrice() return price;public void setPrice(float price) this.price = price;public boo

28、lean equals(Object obj) if (this = obj) return true; if (!(obj instanceof Book) return false;Book b = (Book) obj;if (.equals() && b.count = this.count && b.price = this.price) return true; else return false;public int hashCode() return .hashCode() + new Intege

29、r(this.count).hashCode() + new Float(this.price).hashCode();public String toString() return " 书名: " + + " ;书的价格: " + this.price + " 书的数量: "+ this.count;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class SuperMarket private

30、List<Goods> allGoods;public SuperMarket() this.allGoods = new ArrayList<Goods>();public void add(Goods goods) this.allGoods.add(goods);public void remove(Goods goods) this.allGoods.remove(goods);public List<Goods> search(String keyWord) List<Goods> temp = new ArrayList<Goo

31、ds>(); Iterator<Goods> iter = this.allGoods.iterator(); while (iter.hasNext() Goods g = iter.next();if (g.getName().indexOf(keyWord) != -1) temp.add(g);return temp;public List<Goods> getAllGoods() return this.allGoods;import java.util.Iterator;import java.util.List;public class Test p

32、ublic 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) Iterator iter = all.iterator(); while (iter.hasNext() System.out.println(iter.next();对照范例写出如下题目:(1 )宠物商店,要求可以添加、删除和查找宠物(2) 实现以下两个关系A、一个学校可以有多个学生,所有学生属于一个学校B、一门课程可以有多个学生选,一个学生可以选多门课程四、实验结果与分析(程序运行结果及其分析)五、实验体会实验项目名称:Java 10 操作实

温馨提示

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

评论

0/150

提交评论