javaweb实验报告 java第六次实验报告-接口
时间:2020-08-01 20:43:14 来源:天一资源网 本文已影响 人
实验一
实验题目
体操比赛计算选手成绩的办法是去掉一个最高分和一个最低分再计算平均分,而学校考察一个班级的某科目的考试情况时,是计算全班学生的平均成绩。Gymnastics 类和School 类都实现了ComputerAverage接口,但实现方式不同。
程序代码
interface ComputerAverage{
public double average(double x[]);
}
class Gymnastics implements ComputerAverage{
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[i];
? ? x[i]=x[j];
? ? x[j]=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 ComputerAverage{
public double average(double x[]){
?int count=x.length;
?double aver=0;
?for(int i=0;i<count;i++){
aver=aver+x[i];
}
if(count>0)
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};
ComputerAverage 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\n",result);
}
}
实验结果
实验分析
一个类可以实现多个接口,类通过使用关键字implements声明自己实现一个或多个接口,如果一个非抽象类实现了某个接口,那么这个类必须重写该接口的所有方法。
实验练习
School类如果不重写public double aversge(double x[])方法,程序编译时提示怎样的错误?
答:SChool不是抽象的,并且未覆盖ComputerAverage中的抽象方法。
实验二
实验题目
货车要装载一批货物,货物由三种商品组成:电视、计算机和洗衣机,卡车需要计算出整批货物的重量。
实验代码
interface ComputerWeight{
?public double computerWeight();
}
class Television implements ComputerWeight{
public double computerWeight(){
? return 3.5;
?}
}
class Computer implements ComputerWeight{
?public double computerWeight(){
return 2.67;
}
}
class WashMachine implements ComputerWeight{
?public double computerWeight(){
?return 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 = totalWeights + goods[i].computerWeight();
?}
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());
?}
}
实验结果
实验分析
接口回调是指:可以把使用某一接口的类型创建的对象引用赋给该接口声明的接口变量中,那么该接口变量就可以调用被实现的接口中的方法,当接口变量调用被类实现的接口中的方法时,就是通知相应的对象调用接口的方法,这一过程成为对象功能的接口回调。接口的方法不一定相同,接口回调可能产生不同的行为。
实验练习
请在实验基础上再编写一个实现ComputerWeight接口的类,比如Refrigerrator。这样一来,货车装载的货物中就可以有Refrigerrator类型的对象。当系统增加一个实现ComputerWeight接口的类后,Truck类需要进行修改吗?
答:代码:
class Refrigerrator implements ComputerWeight{
public double computerWeight(){
return 12.8;
? }
}
实验三
实验题目
小狗在不同环境条件下可能呈现不同的状态表现,要求接口封装小狗的状态。具体要求如下:
(1)编写一个接口DogState,该接口有一个名为void showState()方法。
(2)编写一个Dog类,该类中有一个DogState接口声明的变量state,另外,该类有一个show()方法,在该方法中让接口state回调 showState()方法。
(3)编写若干个实现DogState接口的类,负责刻画小狗的各种状态。
(4)编写主类,在主类中实现测试小狗的各种状态。
程序代码
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("狂叫,并冲过去狠咬敌人");
}
}
class MeetFriendState implements DogState{
?public void showState(){
? System.out.println("晃动尾巴,表示欢迎");
?}
}
class MeetAnotherdogState implements DogState{
public void showState(){
?System.out.println("嬉戏");
?}
}
class Dog{
?DogState state;
?public void show(){
?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 MeetAnotherdogState());
yellowDog.show();
}
}
实验结果
实验分析
面向接口编程是指当设计某种重要的类时,不让该类面向具体的类,而是面向接口,即所设计中的重要数据是接口声明的变量,而不是具体声明的对象。
5、实验练习
用面向接口的思想编写一个程序,模拟水杯中的水在不同温度下可能出现的状态。
代码:
interface WaterState{
?public void showState();
}
class SubzeroState implements WaterState{
public void showState(){
System.out.println("结冰");
}
}
class NormalState implements WaterState{
public void showState(){
? System.out.println("冰冷或凉快");
?}
}
class HotState implements WaterState{
?public void showState(){
?System.out.println("有热气冒出,温热");
?}
}
class BoiledState implements WaterState{
?public void showState(){
? System.out.println("沸腾,烫");
?}
}
class Water{
WaterState state;
public void show(){
state.showState();
?}
public void setState(WaterState s){
? state=s;
}
}
public class CheckWaterState{
public static void main(String args[]){
Water cupWater=new Water();
? System.out.print("水杯中的水在零下时:");
?cupWater.setState(new SubzeroState());
cupWater.show();
System.out.print("水杯中的水在常温时:");
cupWater.setState(new NormalState());
cupWater.show();
? System.out.print("水杯中的水在六十七度时:");
cupWater.setState(new HotState());
cupWater.show();
System.out.print("水杯中的水在一百度时:");
?cupWater.setState(new BoiledState());
cupWater.show();
?}
}
相关关键词: 我国第六次人口普查 第六次人口 第六次人口普查 第六次 在市文学艺术界联合会第六次代表大会开幕式上讲话