实验报告2.doc

上传人:laozhun 文档编号:3088626 上传时间:2023-03-10 格式:DOC 页数:15 大小:531.50KB
返回 下载 相关 举报
实验报告2.doc_第1页
第1页 / 共15页
实验报告2.doc_第2页
第2页 / 共15页
实验报告2.doc_第3页
第3页 / 共15页
实验报告2.doc_第4页
第4页 / 共15页
实验报告2.doc_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《实验报告2.doc》由会员分享,可在线阅读,更多相关《实验报告2.doc(15页珍藏版)》请在三一办公上搜索。

1、开课实验室:现代信息交流中心402 开课时间: 2012年*月*日 实验报告: 年 月 日 学院名称信息工程学院年级、专业、班计算1101学号20112748姓名查欣欣同组姓名无课程名称Java程序设计实验项目名称三、面向对象程序开发指导教师尉斌实验类型验证 综合 设计 创新 成绩教师评语 教师签名: 年 月 日一、 实验目的:熟悉类、对象、方法、继承和多态的使用方法。二、 实验内容:1. 创建一个Point类,包含坐标x、y。然后创建一个Line类,定义两个读写属性start和end,数据类型为Point,定义Line类方法(计算start和end之间的距离,并打印start和end坐标以及

2、距离信息)。2. 根据下面类图,设计一个名为Student的类,包括属性“学号”、“姓名”以及3门课程“数学”、“英语”和“计算机”的成绩,包括的方法有计算3门课程的“总分”、“平均分”、“最高分”及“最低分”。在此基础上,使用数组来实现对多个学生的管理。编写一个控制台应用程序,实现如下菜单功能。3. 题目:品尝饮料要求:(1)使用键盘输入参数(饮料类型),输出该饮料类型的味道,如:当键盘输入参数为1时,结果见图1:(2)如果没有该种饮料,结果见图2:实现步骤:(1) 建立一个Java抽象类Drink,应当:a、 声明一个抽象方法taste( ),该方法负责输出饮料的味道;b、 声明int型常

3、量来代表不同的饮料类型(咖啡、啤酒、牛奶)c、 声明静态工厂方法getDrink(int drinkType),根据传入的参数创建不同的饮料对象,并返回该对象,建议使用switch语句。(2) 建立Drink的具体子类:a、 分别建立Drink的子类:Coffee(代表咖啡),Beer(代表啤酒),Milk(代表牛奶);b、 实现taste()方法,要求在屏幕输出中打印各自的味道特征。(3) 建立Test测试类,测试以上内容的正确性a、 编写main方法,通过命令行传参的方式传入某种饮料的类型。b、 在main方法中,调用Drink类的getDrink方法,获得相应的饮料对象。c、 然后调用该

4、饮料的taste()方法,输出该饮料的味道。三、 源代码清单:1 源代码清单package homework;public class Point public double x;public double y;public Point(double x,double y)this.x=x;this.y=y;public static void main(String args) / TODO Auto-generated method stubpackage test2;import homework.Point;public class Line Point start, end; pub

5、lic Point getStart() return start;public void setStart(Point start) this.start = start;public Point getEnd() return end;public void setEnd(Point end) this.end = end;public double distance() double d = Math.sqrt(this.getStart().x - this.getEnd().x)* (this.getStart().x - this.getEnd().x)+ (this.getSta

6、rt().y - this.getEnd().y)* (this.getStart().y - this.getEnd().y);return d;public void print()System.out.println(Start.x = + this.getStart().x + ; Start.y = + this.getStart().y);System.out.println(End.x = + this.getEnd().x + ; End.y = + this.getEnd().y);System.out.println(The distance is + this.dista

7、nce();public static void main(String args)Line line = new Line();line.setStart(new Point(0, 0);line.setEnd(new Point(3, 3);line.print(); 2源代码清单package test3;public class Student String name;String stuno;float math;float english;float computer;public Student()public Student(String stuno, String name,

8、 float math, float english, float computer)this.stuno = stuno;this.name = name;this.math = math;this.english = english;puter = computer;public String getName() return name;public void setName(String name) this.name = name;public String getStuno() return stuno;public void setStuno(String stuno) this.

9、stuno = stuno;public float getMath() return math;public void setMath(float math) this.math = math;public float getEnglish() return english;public void setEnglish(float english) this.english = english;public float getComputer() return computer;public void setComputer(float computer) puter = computer;

10、public float sum()float s;s = this.math + this.getEnglish() + puter;return s;public float avg()float avg;avg = (float)(this.sum()/3.0);return avg;public float max()float max = this.math;if(this.english max)max = this.english;if(puter max)max = puter;return max;public float min()float min = this.math

11、;if(this.english min)min = this.english;if(puter min)min = puter;return min;package test3;public class StudentOperation Student stuArray;int num = 0;public StudentOperation() stuArray = new Student30;public Student getStuArray() return stuArray;public int getStuNum() return num;public boolean addStu

12、dent(Student stu) if (num 30) stuArraynum = stu;num+;return true; else System.out.println(数组越界,不能增加);return false;public boolean delStudent(String stuno) for (int i = 0; i stuArray.length; i+)if (stuArrayi.getStuno().equals(stuno) for (int j = i; j num; j+)stuArrayj = stuArrayj + 1;stuArray29 = null

13、;return true;return false;public Student queryStuno(String stuno) for (int i = 0; i num; i+)if (stuArrayi.getStuno().equals(stuno) System.out.println(stuArrayi.getStuno() + t + stuArrayi.getName() + t + stuArrayi.getMath() + t + stuArrayi.english + t + stuArrayi.getComputer();return stuArrayi;return

14、 null;public Student queryName(String name) for (int i = 0; i num; i+)if (stuArrayi.getName().equals(name) return stuArrayi;return null;public boolean updStu(Student stu) for (int i = 0; i num; i+)if (stuArrayi.getStuno().equals(stu.getStuno() stuArrayi = stu;return true;return false;public void pri

15、nt()System.out.println(学号 + t + 姓名 + t + 数学 + t + 英语 + t + 计算机);for (int i = 0; i num; i+)System.out.println(stuArrayi.getStuno() + t + stuArrayi.getName() + t + stuArrayi.getMath() + t + stuArrayi.getEnglish() + t + stuArrayi.getComputer();public void statistics()System.out.println(学科 + t + 平均成绩 +

16、t + 最高分 + t + 最低分);System.out.print(数学 + t);float avg = 0, sum =0;for (int i = 0; i num; i+)sum += stuArrayi.getMath();avg = sum/num;System.out.print(avg + t);float max = 0;for (int i = 0; i max)max = stuArrayi.getMath();System.out.print(max + t);float min = 0;for (int i = 0; i num; i+)if(stuArrayi.

17、getMath()min)min = stuArrayi.getMath();System.out.println(min);package test3;import java.io.BufferedReader;import java.io.InputStreamReader;public class StudentManagement public static void main(String arg) StudentManagement sm = new StudentManagement();StudentOperation so = new StudentOperation();i

18、nt menu;while (true) menu = sm.printMenu();if (menu = 1) Student stu = sm.addStudent();so.addStudent(stu);if (menu = 2) String stuno = sm.query();so.delStudent(stuno);if (menu = 3) String stuno = sm.query();so.queryStuno(stuno);if (menu = 4) Student s = sm.updStudent();so.updStu(s);if (menu = 5) so.

19、print();if (menu = 6) so.statistics();if (menu = 7) break;public String query() System.out.println(请输入学号:);BufferedReader buf;buf = new BufferedReader(new InputStreamReader(System.in);String str = ;try str = buf.readLine(); catch (Exception e) return str;public Student updStudent() Student stu = new

20、 Student();System.out.println(请输入学号:);BufferedReader buf;buf = new BufferedReader(new InputStreamReader(System.in);String str = ;try str = buf.readLine(); catch (Exception e) stu.setStuno(str);System.out.println(请输入姓名:);/ BufferedReader buf;buf = new BufferedReader(new InputStreamReader(System.in);s

21、tr = ;try str = buf.readLine(); catch (Exception e) stu.setName(str);System.out.println(请输入数学:);/ BufferedReader buf;buf = new BufferedReader(new InputStreamReader(System.in);str = ;try str = buf.readLine(); catch (Exception e) float math = Float.parseFloat(str);stu.setMath(math);System.out.println(

22、请输入英语:);/ BufferedReader buf;buf = new BufferedReader(new InputStreamReader(System.in);str = ;try str = buf.readLine(); catch (Exception e) float english = Float.parseFloat(str);stu.setEnglish(english);System.out.println(请输入计算机:);/ BufferedReader buf;buf = new BufferedReader(new InputStreamReader(Sy

23、stem.in);str = ;try str = buf.readLine(); catch (Exception e) float computer = Float.parseFloat(str);stu.setComputer(computer);return stu;public Student addStudent() Student stu = new Student();System.out.println(请输入学号:);BufferedReader buf;buf = new BufferedReader(new InputStreamReader(System.in);St

24、ring str = ;待添加的隐藏文字内容3try str = buf.readLine(); catch (Exception e) stu.setStuno(str);System.out.println(请输入姓名:);/ BufferedReader buf;buf = new BufferedReader(new InputStreamReader(System.in);str = ;try str = buf.readLine(); catch (Exception e) stu.setName(str);System.out.println(请输入数学:);/ Buffered

25、Reader buf;buf = new BufferedReader(new InputStreamReader(System.in);str = ;try str = buf.readLine(); catch (Exception e) float math = Float.parseFloat(str);stu.setMath(math);System.out.println(请输入英语:);/ BufferedReader buf;buf = new BufferedReader(new InputStreamReader(System.in);str = ;try str = bu

26、f.readLine(); catch (Exception e) float english = Float.parseFloat(str);stu.setEnglish(english);System.out.println(请输入计算机:);/ BufferedReader buf;buf = new BufferedReader(new InputStreamReader(System.in);str = ;try str = buf.readLine(); catch (Exception e) float computer = Float.parseFloat(str);stu.s

27、etComputer(computer);return stu;public int printMenu() System.out.println(请选择(1-6):);System.out.println(1.添加学生);System.out.println(2.删除学生);System.out.println(3.查询学生);System.out.println(4.修改学生);System.out.println(5.打印学生);System.out.println(6.统计学生);BufferedReader buf;buf = new BufferedReader(new Input

28、StreamReader(System.in);System.out.println(请输入:);String str = ;try str = buf.readLine(); catch (Exception e) int num = Integer.parseInt(str);System.out.println(您选择 + num);return num;3. 源代码清单package test4;public class Coffee extends Drinkpublic void taste()System.out.println(咖啡:苦);package test4;publi

29、c class Milk extends Drink public void taste() / TODO Auto-generated method stubSystem.out.println(牛奶:甜);package test4;public class Beer extends Drink public void taste() / TODO Auto-generated method stubSystem.out.println(啤酒:冰);package test4;public abstract class Drink final static int COFFEE = 1;f

30、inal static int BEER = 2;final static int MILK = 3;public abstract void taste();public static Drink getDrink(int drinkType) switch (drinkType) case COFFEE:return new Coffee();case BEER:return new Beer();case MILK:return new Milk();default:return null;package test4;import java.io.BufferedReader;impor

31、t java.io.InputStreamReader;public class Test public static void main(String args) / TODO Auto-generated method stubSystem.out.println(请输入饮料种类:(1-3));BufferedReader buf;buf = new BufferedReader(new InputStreamReader(System.in);String str = ;try str = buf.readLine(); catch (Exception e) int drinkType = Integer.parseInt(str);Drink drink = Drink.getDrink(drinkType);if(drink = null)System.out.println(对不起!没有您输入的饮料类型。);elsedrink.taste();四、 运行结果:(给出运行结果贴图)

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

当前位置:首页 > 教育教学 > 成人教育


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号