-
- 实验目的
- 掌握抽象类的概念和使用方法;
- 掌握接口的概念和定义接口的方法;
- 理解继承与多态的优缺点。
- 实验工具
- 编程语言:Java;
- 开发环境:Eclipse(或MyEclipse、NetBeans、IntelliJ IDEA等)、StarUML(或Enterprise Architect、PowerDesigner、PlantUML、ROSE、VISIO等)。
- 实验题目
- 设计一个中国象棋游戏,ChessPiece为抽象类,提供代表一个棋子位置的属性和方法,和isMoveLegal();设计ChessPiece的具体实现类(如车,马,将军帅等),这些类能够根据自身特点实现不同的isMoveLegal()。
- 利用接口做参数,写个计算器,能完成加减乘除运算。
(1)定义一个接口Compute含有一个方法int computer(int n, int m);
(2)设计四个类分别实现此接口,完成加减乘除运算;
(3)设计一个类UseCompute,类中含有方法:public void useCom(Compute com, int one, int two),此方法能够用传递过来的对象调用computer方法完成运算,并输出运算的结果;
(4)设计一个主类Test,调用UseCompute中的方法useCom来完成加减乘除运算。
-
- 实验步骤
1. 安装JDK、Java集成式开发环境,并配置相应开发环境;
2. 按照题目要求,设计完整的UML类图,并编写相应Java程序。
(请附上详细代码、运行截图等内容)
一.
import java.util.Scanner;
abstract class ChessPiece{
public String name;
public int x;
public int y;
Scanner s = new Scanner(System.in);
public ChessPiece(String name,int x,int y){
this.name=name;
this.x=x;
this.y=y;
}
public void show(){
System.out.println("棋子"+name+"位于("+x+","+y+")");
}
public void isMoveLegal(){
}
}
class car extends ChessPiece{
public car(String name, int x, int y) {
super(name, x, y);
}
public void isMoveLegal(){
System.out.println("车飞直线,输入0前后走,输入1左右走");
int a=s.nextInt();
System.out.println("请输入走的位置");
int b =s.nextInt();
while(b>5||b<-5) {
System.out.println("走错了,重新走");
b = s.nextInt();
}
if(a==0)
y = b;
else if (a==1)
x=b;
}
}
class horse extends ChessPiece {
public horse(String name, int x, int y) {
super(name, x, y);
}
public void isMoveLegal(){
System.out.println("马走日");
System.out.println("请马输入走的位置");
int a=s.nextInt();
int b=s.nextInt();
int c=(a-x)*(a-x)+(b-y)*(b-y);
while(c!=5){
System.out.println("走错了,重新走");
a=s.nextInt();
b=s.nextInt();
c=(a-x)*(a-x)+(b-y)*(b-y);
}
x=a;
y=b;
}
}
class general extends ChessPiece{
public general(String name, int x, int y) {
super(name, x, y);
}
public void isMoveLegal() {
System.out.println("将只能走前后左右");
System.out.println("请输入将军走的位置,0前,1后,2左,3右");
int a=s.nextInt();
if(a==0)
y++;
else if (a==1)
y--;
else if(a==2)
x--;
else if(a==3)
x++;
while(x<-1||x>1||y<-5||y>-3){
System.out.println("走错了,重新走");
a=s.nextInt();
if(a==0)
y++;
else if (a==1)
y--;
else if(a==2)
x--;
else if(a==3)
x++;
}
}
}
public class Main {
public static void main(String[] args) {
car re=new car("车",5,-5);
re.show();
re.isMoveLegal();
re.show();
horse ho=new horse("马",4,-5);
ho.show();
ho.isMoveLegal();
ho.show();
general ge=new general("将",0,-5);
ge.show();
ge.isMoveLegal();
ge.show();
}
}
题目二
public class jsj {
public static void main(String[] args) {
UseCompute uc = new UseCompute();
uc.useCom(new jia(), 5, 3);
uc.useCom(new jian(), 15, 2);
uc.useCom(new Cheng(), 9, 3);
uc.useCom(new Chu(), 8, 4);
}
}
public class UseCompute {
public void useCom(Compute com, int one, int two) {
int a = com.computer(one, two);
System.out.println(a);
}
}
public class jia implements Compute {
public int computer(int n, int m) {
return m + n;
}
}
public class jian implements Compute {
public int computer(int n, int m) {
return n - m;
}
}
public class Cheng implements Compute {
public int computer(int n, int m) {
return n * m;
}
}
public class Chu implements Compute {
public int computer(int n, int m) {
return n / m;
}
}
-
- 实验心得
简要介绍你在实验中使用到的抽象类与接口的用途、功能、注意事项等?