官术网_书友最值得收藏!

InterpreterDriver class

The InterpreterDriver class is the class that drives the application and therefore contains the main() method. There are two class variables, originatingContent and theExpression; both are initially set to null.

In addition to the constructor method, there is also an interpret() method that does the following:

  1. Uses the Scanner class
  2. Instantiates a new Expression instance
  3. Calls the interpret() method on the new Expression instance

The InterpreterDriver class is provided next:

import java.util.Scanner;

public class InterpreterDriver {

// class variables
public Conversion originatingContent = null;
public Expression theExpression = null;

public InterpreterDriver(Conversion content) {
originatingContent = content;
}

public void interpret(String tString) {

Scanner in = new Scanner(System.in);
theExpression = new MapIntToCharacters(tString);
theExpression.interpret(originatingContent);
}


Execution of the InterpreterDriver main() method starts by printing the output header, CODE INTERPRETER, and prompts users for their input. Next, the Scanner class is used to obtain user input via their keyboard. That input is used to create a new Conversion instance. That instance is used to instantiate a new InterpreterDriver object. Finally, the interpret() method is called on that InterpreterDriver object:

    public static void main(String[] args) {
System.out.println("\n\nCODE INTERPRETER\n");
System.out.print("Enter your code: ");
Scanner in = new Scanner(System.in);
String userInput = in.nextLine();
System.out.println("Your code: " + userInput);
Conversion conversion = new Conversion(userInput);
InterpreterDriver userCode = new InterpreterDriver(conversion);
userCode.interpret(userInput);
System.out.println("\n\n");
}
}

The program's output is provided here:

Code Interpreter program console output

This section featured the source code, demonstrating the interpreter design pattern.

主站蜘蛛池模板: 龙口市| 涞源县| 察隅县| 宁阳县| 宣城市| 龙川县| 恩施市| 威远县| 盐亭县| 永登县| 莱西市| 新宾| 抚顺县| 苍山县| 开化县| 体育| 秦皇岛市| 东至县| 云和县| 玉田县| 襄城县| 河南省| 衡东县| 社旗县| 安新县| 民丰县| 西昌市| SHOW| 丹巴县| 阿拉尔市| 营口市| 宜兴市| 广饶县| 新田县| 四会市| 新余市| 黔南| 通化市| 上栗县| 奉新县| 湘乡市|