当前位置:文档之家 > Java实验报告三

Java实验报告三

开课实验室:现代信息交流中心403 开课时间: 2015年**月**日 实验报告: 2015 年 04 月 17日

Java实验报告三

一、 实验目的:熟悉类、对象、方法、继承和多态的使用方法。

二、 实验内容:

1. 创建一个Point 类,包含坐标x 、y 。然后创建一个Line 类,定义两个读写属性start 和end ,数

据类型为Point ,定义Line 类方法(计算start 和end 之间的距离,并打印start 和end 坐标以及距离信息)。

2. 根据下面类图,设计一个名为Student 的类,包括属性“学号”、“姓名”以及3门课程“数学”、

“英语”和“计算机”的成绩,包括的方法有计算3门课程的“总分”、“平均分”、“最高分”及“最低分”。

Java实验报告三

在此基础上,使用数组来实现对多个学生的管理。编写一个控制台应用程序,实现如下菜单功能。

Java实验报告三

Java实验报告三

3.题目:品尝饮料

要求:

(1)使用键盘输入参数(饮料类型),输出该饮料类型的味道,如:当键盘输入参数为1时,结果见图1:

Java实验报告三

(2)如果没有该种饮料,结果见图2:

Java实验报告三

实现步骤:

(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.源代码清单

public class Point {

public static void main(String[] args){

Point point1=new Point(1,2);

Point point2=new Point(4,6);

Line line1=new Line(point1, point2);

System.out.println("The distance is "+line1.line()+"\nThe start is "+"("+line1.start.x+","+line1.start.y+")"+"\nThe end is "+"("+line1.end.x+","+line1.end.y+")");

}

double x;

double y;

Point(){

}

Point(double x, double y){

this.x=x;

this.y=y;

}

}

class Line {

Point start;

Point end;

Line(){

}

Line(Point newStart, Point newEnd){

start=newStart;

end=newEnd;

}

public double line(){

return Math.sqrt((end.x-start.x)*(end.x-start.x)+(end.y-start.y)*(end.y-start.y));

}

}

2.源代码清单

public class Student {

String stuno;

String name;

float math;

float english;

float computer;

Student(){

}

Student(String stuno, String name, float math, float english, float computer){ this.stuno=stuno;

http://www.doczj.com/doc/b7509c240975f46526d3e18b.html=name;

this.math=math;

this.english=english;

http://www.doczj.com/doc/b7509c240975f46526d3e18b.htmlputer=computer;

}

public String getStuno() {

return stuno;

}

public void setStuno(String s) { this.stuno = s;

}

public String getName() {

return name;

}

public void setName(String n) {

http://www.doczj.com/doc/b7509c240975f46526d3e18b.html = n;

}

public float getMath() {

return math;

}

public void setMath(float m) {

this.math = m;

}

public float getEnglish() {

return english;

}

public void setEnglish(float e) { this.english = e;

}

public float getComputer() {

return computer;

}

public void setComputer(float c) { http://www.doczj.com/doc/b7509c240975f46526d3e18b.htmlputer = c;

}

public double sum() {

return math+english+computer;

}

public double avg(){

return (math+english+computer)/3; }

public double max(double max){

if(math>=english){

if(math>=computer)

max=math;

else

max=computer;

}

else if(math

if(english

math=computer;

else

max=english;

}

return max;

}

public double min(double min){

if(math<=english){

if(math<=computer)

min=math;

else

min=computer;

}

else if(math>english){

if(computer>english)

min=english;

else

min=computer;

}

return min;

}

}

public class Manager extends Student {

private Student[] student = new Student[100];

private static int numberOfStudent = 0;

int getNumberOfStudent() {

return numberOfStudent;

}

Student[] getStudent() {

return student;

}

boolean addStudent(Student student) {

try {

this.student[numberOfStudent] = student;

numberOfStudent++;

return true;

} catch (Exception e) {

return false;

}

}

boolean removeStudent(String stuno) {

boolean target = false;

for (int i = 0; i < numberOfStudent; i++)

if (student[i].getStuno().equals(stuno)) {

student[i] = null;

target = true;

}

return target;

}

Student findStudent(String s) {

for (int i = 0; i < numberOfStudent; i++)

if (student[i] != null)

if (student[i].getStuno().equals(s)

|| student[i].getName().equals(s))

return student[i];

return null;

}

boolean changeStudent(Student student) {

for (int i = 0; i < numberOfStudent; i++) {

if (this.student[i].getStuno().equals(student.getStuno())

|| this.student[i].getName().equals(student.getName())) { this.student[i] = student;

return true;

}

}

return false;

}

void print() {

System.out.println("学号\t姓名\t数学\t英语\t计算机");

for (int i = 0; i < numberOfStudent; i++)

if (student[i] != null) {

System.out.println(student[i].getStuno() + "\t"

+ student[i].getName() + "\t" + student[i].getMath()

+ "\t" + student[i].getEnglish() + "\t"

+ student[i].getComputer());

}

}

void caculate() {

float max_math = 0, min_math = student[0].getMath(), sum_math = 0;

for (int i = 0; i < numberOfStudent; i++) {

sum_math += student[i].getMath();

if (student[i].getMath() > max_math)

max_math = student[i].getMath();

else if (student[i].getMath() < min_math)

min_math = student[i].getMath();

}

float avg_math = sum_math / numberOfStudent;

float max_english = 0, min_english = student[0].getEnglish(), sum_english = 0;

for (int i = 0; i < numberOfStudent; i++) {

sum_english += student[i].getEnglish();

if (student[i].getEnglish() > max_english)

max_english = student[i].getEnglish();

else if (student[i].getEnglish() < min_english)

min_english = student[i].getEnglish();

}

float avg_english = sum_english / numberOfStudent;

float max_computer = 0, min_computer = student[0].getComputer(), sum_computer = 0;

for (int i = 0; i < numberOfStudent; i++) {

sum_computer += student[i].getComputer();

if (student[i].getComputer() > max_computer)

max_computer = student[i].getComputer();

else if (student[i].getComputer() < min_computer)

min_computer = student[i].getComputer();

}

float avg_computer = sum_computer / numberOfStudent;

System.out.println("学科\t平均成绩\t最高分\t最低分");

System.out.println("数学\t" + avg_math + "\t" + max_math + "\t"

+ min_math);

System.out.println("英语\t" + avg_english + "\t" + max_english + "\t"

+ min_english);

System.out.println("计算机\t" + avg_computer + "\t" + max_computer + "\t"

+ min_computer);

}

}

import java.util.Scanner;

public class TestStudent {

public static void main(String[] args) {

Manager manager = new Manager();

System.out

.println("1.添加学生\n2.删除学生\n3.查询学生\n4.修改学生\n5.打印学生\n6.统计学生\n7.退出");

Scanner scanner = new Scanner(System.in);

boolean flag = true;

while (flag) {

int input = 9;

try {

input = scanner.nextInt();

} catch (Exception e) {

System.out.println("Wrong operation.");

flag = false;

}

switch (input) {

case 1:

System.out

.println("Enter the student's information(stuno,name,math,english,computer):");

Student student1 = new Student();

student1.setStuno(scanner.next());

student1.setName(scanner.next());

student1.setMath(scanner.nextFloat());

student1.setEnglish(scanner.nextFloat());

student1.setComputer(scanner.nextFloat());

if (manager.addStudent(student1))

System.out.println("success.");

else

System.out.println("fail.");

break;

case 2:

System.out.println("Enter the stuno:");

if (manager.removeStudent(scanner.next()))

System.out.println("success.");

else

System.out.println("fail.");

break;

case 3:

System.out.println("Enter the stuno or name:");

Student student3 = manager.findStudent(scanner.next());

if (student3 != null) {

System.out.println(student3.getStuno() + "\t"

+ student3.getName() + "\t" + student3.getMath()

+ "\t" + student3.getEnglish() + "\t"

+ student3.getComputer());

} else

System.out.println("There is no such a student.");

break;

case 4:

System.out

.println("Enter the changed student(stuno,name,math,english,computer):");

Student student4 = new Student();

student4.setStuno(scanner.next());

student4.setName(scanner.next());

student4.setMath(scanner.nextFloat());

student4.setEnglish(scanner.nextFloat());

student4.setComputer(scanner.nextFloat());

if (manager.changeStudent(student4))

System.out.println("change success.");

else

System.out.println("change fail.");

break;

case 5:

manager.print();

break;

case 6:

try {

manager.caculate();

break;

} catch (Exception e) {

System.out.println("there is no student.");

}

case 7:

flag = false;

System.out.println("退出。");

break;

default:

System.out.println("Wrong operation.");

}

}

}

}

3. 源代码清单

public abstract class Drink {

public abstract String getTaste();

int drinkType;

int Coffee=1;

int Beer=2;

int Milk=3;

Drink(){

}

Drink(int newDrinkType){

drinkType=newDrinkType;

}

static Drink getDrink(int drinkType){ switch(drinkType){

case 1: return new Coffee();

case 2: return new Beer();

case 3: return new Milk();

}

return null;

}

}

class Coffee extends Drink{

Coffee(){

}

public String getTaste(){

return "咖啡:味苦";

}

}

class Beer extends Drink{

Beer(){

}

public String getTaste(){

return "啤酒: 味涩";

}

}

class Milk extends Drink{

Milk(){

}

public String getTaste(){

return "牛奶: 味香";

}

}

import java.util.Scanner;

public class TestDrink {

public static void main(String[] args){

Scanner input=new Scanner(System.in);

System.out.println("Enter your number of the drink:");

int drinkType=input.nextInt();

Drink drink=Drink.getDrink(drinkType);

if(drinkType>3){

System.out.println("对不起!没有你要的饮料类型。");

}

else {

System.out.println(drink.getTaste());

}

}

}

四、运行结果:(给出运行结果贴图)

Java实验报告三

Java实验报告三

Java实验报告三