JAVA语言(抽象类与接口)

本文介绍了如何在Java中使用抽象类ChessPiece和接口来设计中国象棋游戏,以及如何通过继承和多态实现具体棋子类的移动逻辑。同时,通过Compute接口实现了加减乘除运算功能。
摘要由CSDN通过智能技术生成

    1. 实验目的
  1. 掌握抽象类的概念和使用方法;
  2. 掌握接口的概念和定义接口的方法;
  3. 理解继承与多态的优缺点。
    1. 实验工具
  4. 编程语言:Java;
  5. 开发环境:Eclipse(或MyEclipse、NetBeans、IntelliJ IDEA等)、StarUML(或Enterprise Architect、PowerDesigner、PlantUML、ROSE、VISIO等)。
    1. 实验题目
  1. 设计一个中国象棋游戏,ChessPiece为抽象类,提供代表一个棋子位置的属性和方法,和isMoveLegal();设计ChessPiece的具体实现类(如车,马,将军帅等),这些类能够根据自身特点实现不同的isMoveLegal()。
  2. 利用接口做参数,写个计算器,能完成加减乘除运算。

(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. 实验步骤

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;
    }
}

    1. 实验心得

简要介绍你在实验中使用到的抽象类与接口的用途、功能、注意事项等?

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值