Java上机2019/11/01
相关知识点
接口体中只有常量的声明(没有变量)和抽象方法声明。而且接口体中所有的常量的访问权限一定都是public(允许省略public、final修饰符),所有的抽象方法的访问权限一定都是public(允许省略public、abstract修饰符)。
接口由类去实现以便绑定接口中的方法。一个类可以实现多个接口,类通过使用关键字implements声明自己实现一个或多个接口。如果一个非抽象类实现了某个接口,那么这个类必须重写接口的所有方法。
实验1 评价成绩
具体代码:
Estimator.java
package Evaluation_results
;
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
[]) {
int count
= x
.length
;
double aver
= 0, sum
= 0;
for (int i
= 0; i
< count
; i
++) {
sum
= sum
+ x
[i
];
}
aver
= sum
/ 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
);
System
.out
.printf("%n");
System
.out
.printf("体操选手最后得分:%5.3f\n",result
);
computer
= new School();
result
= computer
.average(b
);
System
.out
.printf("班级考试平均分数:%-5.2f", result
);
}
}
运行结果: 实验后的练习: School类如果不重写public double average(double x[])方法,程序编译时提示怎样的错误?
实验2 货车的装载量
具体代码:
CheckCarWeight.java
package Truck_load
;
interface ComputerWeight {
public double computeWeight();
}
class Television implements ComputerWeight {
public double computeWeight() {
return 12.5;
}
}
class Computer implements ComputerWeight {
public double computeWeight() {
return 2.6;
}
}
class WashMachine implements ComputerWeight {
public double computeWeight() {
return 12;
}
}
class Truck {
ComputerWeight
[] goods
;
double totalWeights
= 0;
Truck(ComputerWeight
[] goods
) {
this.goods
= goods
;
}
public void setGoods(ComputerWeight
[] goods
) {
this.goods
= goods
;
}
public double getTotalWeight() {
totalWeights
= 0;
for(int i
=0;i
<goods
.length
;i
++) {
totalWeights
=totalWeights
+goods
[i
].computeWeight();
}
return totalWeights
;
}
}
public class CheckCarWeight {
public static void main(String
[] args
) {
ComputerWeight
[] goods
= new ComputerWeight[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
.getTotalWeight());
goods
= new ComputerWeight[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
.getTotalWeight());
}
}
运行结果:
实验3 小狗的状态
具体代码:
CheckDogState.java
package Puppy_Village
;
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 MeetAnotherDog 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 MeetAnotherDog());
yellowDog
.show();
}
}
运行结果:
实验后的练习 水的状态
具体代码:
WaterForm.java
package Water_form
;
interface State {
public void showState();
}
class GasState implements State {
public void showState() {
System
.out
.println("该温度下水为气态(水蒸气)");
}
}
class LiquidState implements State {
public void showState() {
System
.out
.println("该温度下水为液态(自来水)");
}
}
class SolidState implements State {
public void showState() {
System
.out
.println("该温度下水为固态(冰雪糕)");
}
}
class Cup {
State state
;
public void show() {
state
.showState();
}
public void setstate(State s
) {
state
= s
;
}
}
public class WaterForm {
public static void main(String
[] args
) {
Cup soda
= new Cup();
System
.out
.print("水的温度为0~100摄氏度:");
soda
.setstate(new LiquidState());
soda
.show();
System
.out
.print("水的温度为小于0摄氏度:");
soda
.setstate(new SolidState());
soda
.show();
System
.out
.print("水的温度为大于100摄氏度:");
soda
.setstate(new GasState());
soda
.show();
}
}
运行结果:
转载请注明原文地址: https://mac.8miu.com/read-511610.html