首页 > 其他分享 >11.18

11.18

时间:2024-11-13 23:18:38浏览次数:1  
标签:distance direction String 11.18 AbstractNode action public

 

 

实验17:解释器模式选作

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容: 

1、理解解释器模式的动机,掌握该模式的结构;

2、能够利用解释器模式解决实际问题。

 

[实验任务一]:解释器模式

某机器人控制程序包含一些简单的英文指令,其文法规则如下:

expression ::= direction action distance | composite

composite ::= expression and expression

direction ::= ‘up’ | ‘down’ | ‘left’ | ‘right’

action ::= ‘move’ | ‘run’

distance ::= an integer //一个整数值

如输入:up move 5,则输出“向上移动5个单位”;输入:down run 10 and left move 20,则输出“向下移动10个单位再向左移动20个单位”。

实验要求:

1、类图

 

 

2、源代码

①、AbstractNode.java

package org.example.shiyanshiqi;

public abstract class AbstractNode {
    public abstract String interpret();
}

 

②、AndNode.java

package org.example.shiyanshiqi;

public class AndNode extends AbstractNode{
    private AbstractNode left;
    private AbstractNode right;

    public AndNode(AbstractNode left, AbstractNode right) {
        this.left = left;
        this.right = right;
    }

    @Override
    public String interpret() {
        return left.interpret() + " 再 " + right.interpret();
    }
}

 

③、SentenceNode.java

package org.example.shiyanshiqi;

public class SentenceNode extends AbstractNode {
    private AbstractNode direction;
    private AbstractNode action;
    private AbstractNode distance;

    public SentenceNode(AbstractNode direction, AbstractNode action, AbstractNode distance) {
        this.direction = direction;
        this.action = action;
        this.distance = distance;
    }

    @Override
    public String interpret() {
        return direction.interpret() + action.interpret() + distance.interpret();
    }
}

 

④、DirectionNode.java

package org.example.shiyanshiqi;

 

public class DirectionNode extends AbstractNode {

    private String direction;

 

    public DirectionNode(String direction) {

        this.direction = direction;

    }

 

    @Override

    public String interpret() {

        if (direction.equalsIgnoreCase("up")) {

            return "向上";

        } else if (direction.equalsIgnoreCase("down")) {

            return "向下";

        } else if (direction.equalsIgnoreCase("left")) {

            return "向左";

        } else if (direction.equalsIgnoreCase("right")) {

            return "向右";

        } else {

            return "无效指令";

        }

    }

}

 

 

⑤、DistanceNode.java

package org.example.shiyanshiqi;

public class DistanceNode extends AbstractNode{
    private String distance;
    public DistanceNode(String distance){
        this.distance=distance;
    }

    @Override
    public String interpret() {
        return this.distance;
    }
}

 

⑥、ActionNode.java

package org.example.shiyanshiqi;

public class ActionNode extends AbstractNode {
    private String action;

    public ActionNode(String action) {
        this.action = action;
    }

    @Override
    public String interpret() {
        if (action.equalsIgnoreCase("move")) {
            return "移动";
        } else if (action.equalsIgnoreCase("run")) {
            return "快速移动";
        } else {
            return "停止";
        }
    }
}

 

⑦、InstructionHandler.java

package org.example.shiyanshiqi;
import java.util.*;
public class InstructionHandler {
    private String instruction;
    private AbstractNode node;
    public void handle(String instruction){
        AbstractNode left=null,right=null;
        AbstractNode direction=null,action=null,distance=null;
        Stack stack=new Stack();
        String[] words=instruction.split(" ");
        for(int i=0;i<words.length;i++){
            if(words[i].equalsIgnoreCase("and")){
                left=(AbstractNode)stack.pop();
                String word1=words[++i];
                direction=new DirectionNode(word1);
                String word2=words[++i];
                action = new ActionNode(word2);
                String word3=words[++i];
                distance=new DistanceNode(word3);
                right=new SentenceNode(direction,action,distance);
                stack.push(new AndNode(left,right));
            }else{
                String word1=words[i];
                direction=new DirectionNode(word1);
                String word2=words[++i];
                action=new ActionNode(word2);
                String word3=words[++i];
                distance=new DistanceNode(word3);
                left=new SentenceNode(direction,action,distance);
                stack.push(left);
            }
        }
        this.node=(AbstractNode) stack.pop();
    }
    public String output(){
        return node.interpret();
    }
}

 

⑧、Client.java

package org.example.shiyanshiqi;

public class Client {
    public static void main(String[] args) {
        String instruction="up move 6 and down run 11 and left move 6";
        InstructionHandler handler=new InstructionHandler();
        handler.handle(instruction);
        String outString=handler.output();
        System.out.println(outString);
    }
}

 

3、运行结果

 

标签:distance,direction,String,11.18,AbstractNode,action,public
From: https://www.cnblogs.com/zzqq1314/p/18545054

相关文章

  • 大二打卡(11.18)
    今天做了什么:三点一刻起床,眼珠子快炸了一样,困,只有一个字困,靠在墙根里面我都能睡着了,摄影师、化妆师啥玩意乱七八糟的全来了,拍素材,让我去找老爸老妈,让我去这儿,让我搬着个,好多事儿,好烦,好想睡觉,要不是这身衣服帅爆了我早昏死在床上了,我还有最重要的任务,堵门要红包。在姐夫他们来之前......
  • 11.18
    读完整本书后,我深感受益匪浅,以下是我的读书感受:首先,本书强调了实效的概念,强调软件开发应该关注实际效果而非形式主义。在软件开发领域,经常会陷入繁琐的流程和规范之中,而实际上,真正重要的是产出高质量、可维护、可扩展的软件。这一理念对于开发者来说是一种解脱,让人更专注于创造......
  • 每日总结-23.11.18
    publicclassStudent{privateStringid;privateStringname;privateStringage;publicStringgetAge(){returnage;}publicvoidsetAge(Stringage){this.age=age;}publicStudent(){}publicStude......
  • 11.18每日总结
    this.$set(全局Vue.set方法)this.$set(target,key,value)1target:要更改的数据源(可以是一个对象或者数组)key:要更改的具体数据(索引)value:重新赋的值注:当vue的data里边声明或者已经赋值过的对象或者数组(数组里边的值是对象)时,向对象中添加新的属性,如果更新此属性的值,是不会更新视......
  • 11.18 && 11.19
    发现好多学长都退役了......
  • 【2023潇湘夜雨】WIN11_Pro_23H2.22635.2771软件选装纯净版11.18
    【系统简介】=============================================================1.本次更新母盘来自WIN11_Pro_23H2.22635.2771。2.增加部分优化方案,手工精简部分较多。3.OS版本号为22635.2771。精简系统只是为部分用户安装,个别要求高的去MSDN下。4.集成《DrvCeo-2.15.0.5》网卡版、......
  • 11.18日记
    学习开发记录:文件的下载还有问题@GetMapping("/download/{fileName}")publicvoiddownload(@PathVariableStringfileName,HttpServletResponseresponse)throwsIOException{StringfilePath=ROOT_PATH+File.separator+fileName;System.out.println(filePath);......
  • 2023.11.18 日记
    今天noip打烂了。具体的都在文章里写过了。父亲几年来一直在备考,CSP-S2的那天他刚好考完。最近他轻松了很多。我们家也轻松了好多。所以最近都是他接送我。几个月前他听说我精神状态不佳,他说,要保持一个很严格的生物钟。这正是我所希冀的。他一直保持着很好的生活习惯,他讨......
  • 2023.11.18——每日总结
    学习所花时间(包括上课):9h代码量(行):0行博客量(篇):1篇今天,上午学习,下午学习;我了解到的知识点:1.mybatis明日计划:学习......
  • 11.17~11.18暨noip2023游寄
    11.17我们DZ不负众望又干了点nt事,但是为了按时间顺序记叙,所以说放到最后再讲上午平常的起床+吃饭,然后就发手机啥的,坐大巴去德州东再坐会高铁去秦皇岛,这些简单记一下就行了重点来了先拜谢一下__int_R__在大巴上的时候tkth拉int_R回粥,然后他在自己号整了几抽出了个6星,又在tkth......