开课实验室:现代信息交流中心402 开课时间: 2012年**月**日 实验报告: 年 月 日 学院名称 课程名称 信息工程学院 Java程序设计 年级、专业、班 实验项目名称 计算1101 学2011274号 8 姓名 查欣欣 指导教师 成绩 同组姓名 无 尉斌 三、面向对象程序开发 实验类型 教师评语 验证 √ 综合 □ 设计 □ 创新 □ 教师签名: 年 月 日 一、 实验目的:熟悉类、对象、方法、继承和多态的使用方法。
二、 实验内容:
1. 创建一个Point类,包含坐标x、y。然后创建一个Line类,定义两个读写属性start和end,数
据类型为Point,定义Line类方法(计算start和end之间的距离,并打印start和end坐标以及距离信息)。
2. 根据下面类图,设计一个名为Student的类,包括属性“学号”、“姓名”以及3门课程“数学”、
“英语”和“计算机”的成绩,包括的方法有计算3门课程的“总分”、“平均分”、“最高分”及“最低分”。
在此基础上,使用数组来实现对多个学生的管理。编写一个控制台应用程序,实现如下菜单功能。
共17页,第1页 制表单位:设备处
天津商业大学学生实验报告
3. 题目:品尝饮料
要求:
(1)使用键盘输入参数(饮料类型),输出该饮料类型的味道,如:当键盘输入参数为1时,结果见图1:
共17页,第2页 制表单位:设备处
天津商业大学学生实验报告
(2)如果没有该种饮料,结果见图2:
实现步骤:
(1) 建立一个Java抽象类Drink,应当:
a、 声明一个抽象方法taste( ),该方法负责输出饮料的味道; b、 声明int型常量来代表不同的饮料类型(咖啡、啤酒、牛奶)
c、 声明静态工厂方法getDrink(int drinkType),根据传入的参数创建不同的饮料对象,并
返回该对象,建议使用switch语句。
(2) 建立Drink的具体子类:
a、 分别建立Drink的子类:Coffee(代表咖啡),Beer(代表啤酒),Milk(代表牛奶); b、 实现taste()方法,要求在屏幕输出中打印各自的味道特征。 (3) 建立Test测试类,测试以上内容的正确性
a、 编写main方法,通过命令行传参的方式传入某种饮料的类型。 b、 在main方法中,调用Drink类的getDrink方法,获得相应的饮料对象。 c、 然后调用该饮料的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 stub }
共17页,第3页 制表单位:设备处
天津商业大学学生实验报告
package test2;
import homework.Point; public class Line { Point start, end;
public 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.getStart().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.distance()); }
public static void main(String[] args){ Line line = new Line();
line.setStart(new Point(0, 0)); line.setEnd(new Point(3, 3)); line.print(); }
}
共17页,第4页 制表单位:设备处
天津商业大学学生实验报告
2.源代码清单 package test3;
public class Student { String name; String stuno; float math; float english; float computer;
public Student(){ }
public Student(String stuno, String name, float math, float english, float computer){{ this.stuno = stuno; this.name = name; this.math = math;
this.english = english; this.computer = 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.stuno = stuno; }
共17页,第5页 制表单位:设备处
天津商业大学学生实验报告
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) { this.computer = computer; }
public float sum(){ float s;
s = this.math + this.getEnglish() + this.computer; 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(this.computer > max) max = this.computer; return max; }
共17页,第6页 制表单位:设备处
天津商业大学学生实验报告
public float min(){
float min = this.math; if(this.english < min) min = this.english; if(this.computer < min) min = this.computer; return min; } }
package test3;
public class StudentOperation { Student stuArray[]; int num = 0;
public StudentOperation() { stuArray = new Student[30]; }
public Student[] getStuArray() { return stuArray; }
public int getStuNum() { return num; }
public boolean addStudent(Student stu) { if (num < 30) {
stuArray[num] = stu; num++;
return true; } else {
System.out.println(\"数组越界,不能增加\"); return false; } }
public boolean delStudent(String stuno) { for (int i = 0; i < stuArray.length; i++)
if (stuArray[i].getStuno().equals(stuno)) { for (int j = i; j < num; j++)
stuArray[j] = stuArray[j + 1]; stuArray[29] = null;
共17页,第7页 制表单位:设备处
天津商业大学学生实验报告
return true; }
return false; }
public Student queryStuno(String stuno) { for (int i = 0; i < num; i++)
if (stuArray[i].getStuno().equals(stuno)) { System.out.println(stuArray[i].getStuno() + \"\\" + stuArray[i].getName() + \"\\" + stuArray[i].getMath() + \"\\" + stuArray[i].english + \"\\" + stuArray[i].getComputer());
return stuArray[i]; }
return null; }
public Student queryName(String name) { for (int i = 0; i < num; i++)
if (stuArray[i].getName().equals(name)) { return stuArray[i]; }
return null; }
public boolean updStu(Student stu) { for (int i = 0; i < num; i++)
if (stuArray[i].getStuno().equals(stu.getStuno())) { stuArray[i] = stu; return true; }
return false; }
public void print(){
System.out.println(\"学号\" + \"\\" + \"姓名\" + \"\\" + \"数学\" + \"\\" + \"英语\" + \"\\" + \"计算机\");
for (int i = 0; i < num; i++)
System.out.println(stuArray[i].getStuno() + \"\\" + stuArray[i].getName() + \"\\" + stuArray[i].getMath() + \"\\" + stuArray[i].getEnglish() + \"\\" + stuArray[i].getComputer()); }
public void statistics(){
System.out.println(\"学科\" + \"\\" + \"平均成绩\" + \"\\" + \"最高分\" + \"\\" + \"最低分\");
共17页,第8页 制表单位:设备处
天津商业大学学生实验报告
System.out.print(\"数学\" + \"\\");
float avg = 0, sum =0;
for (int i = 0; i < num; i++) sum += stuArray[i].getMath(); avg = sum/num;
System.out.print(avg + \"\\");
float max = 0;
for (int i = 0; i < num; i++) if(stuArray[i].getMath()>max) max = stuArray[i].getMath(); System.out.print(max + \"\\");
float min = 0;
for (int i = 0; i < num; i++) if(stuArray[i].getMath() 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(); int 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); } 共17页,第9页 制表单位:设备处 天津商业大学学生实验报告 } 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 Student(); System.out.println(\"请输入学号:\"); BufferedReader buf; buf = new BufferedReader(new InputStreamReader(System.in)); String str = \"\"; try { str = buf.readLine(); } catch (Exception e) { } if (menu == 3) { String stuno = sm.query(); so.queryStuno(stuno); } if (menu == 4) { Student s = sm.updStudent(); so.updStu(s); } if (menu == 5) { so.print(); } if (menu == 6) { so.statistics(); } if (menu == 7) { break; } 共17页,第10页 制表单位:设备处 天津商业大学学生实验报告 } 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(\"请输入数学:\"); // 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(\"请输入英语:\"); // 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(System.in)); str = \"\"; try { str = buf.readLine(); } catch (Exception e) { } float computer = Float.parseFloat(str); stu.setComputer(computer); 共17页,第11页 制表单位:设备处 天津商业大学学生实验报告 return stu; } public Student addStudent() { Student stu = new 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)); str = \"\"; 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(\"请输入英语:\"); // BufferedReader buf; buf = new BufferedReader(new InputStreamReader(System.in)); str = \"\"; try { str = buf.readLine(); } catch (Exception e) { 共17页,第12页 制表单位:设备处 天津商业大学学生实验报告 } 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.setComputer(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 InputStreamReader(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 Drink{ public void taste(){ 共17页,第13页 制表单位:设备处 天津商业大学学生实验报告 System.out.println(\"咖啡:苦\"); } } package test4; public class Milk extends Drink { public void taste() { // TODO Auto-generated method stub System.out.println(\"牛奶:甜\"); } } package test4; public class Beer extends Drink { public void taste() { // TODO Auto-generated method stub System.out.println(\"啤酒:冰\"); } } package test4; public abstract class Drink { final static int COFFEE = 1; final 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: 共17页,第14页 制表单位:设备处 天津商业大学学生实验报告 return null; } } } package test4; import java.io.BufferedReader; import java.io.InputStreamReader; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub System.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(\"对不起!没有您输入的饮料类型。\"); }else{ drink.taste(); } } } 四、 运行结果:(给出运行结果贴图) 共17页,第15页 制表单位:设备处 天津商业大学学生实验报告 共17页,第16页 制表单位:设备处 天津商业大学学生实验报告 共17页,第17页 制表单位:设备处
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- 7swz.com 版权所有 赣ICP备2024042798号-8
违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务