实验6.1 评价成绩
【实验要求】体操比赛计算选手成绩的办法是去掉一个最高分和最低分后再计算平均分,而学校考查一个班级的某科目的考试情况时,是计算全班同学的平均成绩。
【实验步骤】
文章来源地址https://www.toymoban.com/news/detail-472664.html
Estimator.java
interface CompurerAverage {
public double average(double x[]);
}
class Gymnastics implements CompurerAverage {
public double average(double x[]) {
int count=x.length;
double aver=0,temp=0;
for(int i=0;i<count;i++) {
for(int j=i;j<count;j++) {
if(x[j]<x[i]) {
temp=x[j];
x[j]=x[i];
x[i]=temp;
}
}
}
for(int i=1;i<count-1;i++) {
aver=aver+x[i];
}
if(count>2)
aver=aver/(count-2);
else
aver=0;
return aver;
}
}
class School implements CompurerAverage {
//重写public double average(double x[])方法,返回数组x[]的元素的算术平均
public double average(double[] x){
int count=x.length;
double aver=0;
for(int i=0;i<count;i++){
aver=aver+x[i];
}
aver=aver/count;
return aver;
}
}
public class Estimator{
public static void main(String args[]) {
double a[] = {9.89,9.88,9.99,9.12,9.69,9.76,8.97};
double b[] ={89,56,78,90,100,77,56,45,36,79,98};
CompurerAverage computer;
computer=new Gymnastics();
double result=computer .average(a);//computer调用average(double x[])方法,将数组a传递给参数x
System.out.printf("%n");
System.out.printf("体操选手最后得分:%5.3f\n",result);
computer=new School();
result=computer .average(b); //computer调用average(double x[])方法,将数组b传递给参数x
System.out.printf("班级考试平均分数:%-5.2f",result);
}
}
运行结果:
实验6.2 货车的装载量
【实验要求】 货车要装载一批货物,货物由三种商品组成:电视、计算机和洗衣机。卡车需要计算出整批货物的重量。
【实验步骤】
CheckCarWeight.java
interface ComputerWeight {
public double computeWeight();
}
class Television implements ComputerWeight {
public double computeWeight(){
return 3.5;
} //重写computeWeight()方法,返回自重值3.5。
}
class Computer implements ComputerWeight {
public double computeWeight(){
return 2.67;
} //重写computeWeight()方法,返回自重值2.67。
}
class WashMachine implements ComputerWeight {
public double computeWeight(){
return 13.8;
} //重写computeWeight()方法,返回自重值13.8。
}
class Truck {
ComputerWeight [] goods; //数组元素分别存放几个实现类的对象的引用
double totalWeights=0;
Truck(ComputerWeight[] goods) {
this.goods=goods;
}
public void setGoods(ComputerWeight[] goods) {
this.goods=goods;
}
public double getTotalWeights() {
totalWeights=0;
for(int i=0;i<goods.length;i++){
totalWeights+=goods[i].computeWeight();
} //计算totalWeights
return totalWeights;
}
}
public class CheckCarWeight {
public static void main(String args[]) {
ComputerWeight[] goods=new ComputerWeight[650]; //650件货物
for(int i=0;i<goods.length;i++) { //简单分成三类
if(i%3==0)
goods[i]=new Television();
else if(i%3==1)
goods[i]=new Computer();
else if(i%3==2)
goods[i]=new WashMachine();
}
Truck truck=new Truck(goods);
System.out.printf("\n货车装载的货物重量:%-8.5f kg\n",truck.getTotalWeights());
goods=new ComputerWeight[68]; //68件货物
for(int i=0;i<goods.length;i++) { //简单分成两类
if(i%2==0)
goods[i]=new Television();
else
goods[i]=new WashMachine();
}
truck.setGoods(goods);
System.out.printf("货车装载的货物重量:%-8.5f kg\n",truck.getTotalWeights());
}
}
运行结果:
实验6.3 小狗的状态
【实验要求】小狗在不同环境条件下可能呈现不同的状态表现,要求用接口封装小狗的状态。具体要求如下。
【实验步骤】
CheckDogState.java
interface DogState {
public void showState();
}
class SoftlyState implements DogState {
public void showState() {
System.out.println("听主人的命令");
}
}
class MeetEnemyState implements DogState {
public void showState(){
System.out.println("狂叫,并冲向去很咬敌人");
} //重写showState()方法,输出"狂叫,并冲向去很咬敌人"
}
class MeetFriendState implements DogState {
public void showState(){
System.out.println("晃动尾巴,表示欢迎");
} //重写showState()方法,输出"晃动尾巴,表示欢迎"
}
class MeetAnotherDog implements DogState {
public void showState(){
System.out.println("嬉戏");
} //重写showState()方法,输出"嬉戏"
}
class Dog {
DogState state;
public void show() {
state.showState(); //接口state回调showState()方法。
}
public void setState(DogState s) {
state = s;
}
}
public class CheckDogState {
public static void main(String args[]) {
Dog yellowDog =new Dog();
System.out.print("狗在主人面前:");
yellowDog.setState(new SoftlyState());
yellowDog.show();
System.out.print("狗遇到敌人:");
yellowDog.setState(new MeetEnemyState());
yellowDog.show();
System.out.print("狗遇到朋友:");
yellowDog.setState(new MeetFriendState());
yellowDog.show();
System.out.print("狗遇到同伴:");
yellowDog.setState(new MeetAnotherDog());
yellowDog.show();
}
}
运行结果:
文章来源:https://www.toymoban.com/news/detail-472664.html
到了这里,关于Java实验六 接口与实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!