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

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.

主站蜘蛛池模板: 沅江市| 北川| 类乌齐县| 临海市| 古蔺县| 太白县| 天祝| 宜春市| 青阳县| 夏邑县| 绥中县| 明水县| 阳原县| 扎兰屯市| 石楼县| 阿瓦提县| 湟中县| 泾阳县| 桐城市| 古交市| 峨山| 正宁县| 商都县| 乌什县| 梧州市| 汝南县| 枞阳县| 江安县| 海安县| 噶尔县| 河北区| 安顺市| 沙河市| 随州市| 毕节市| 临江市| 绥棱县| 漳浦县| 镇远县| 通辽市| 太和县|