Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.*; import java.io.*; public class YazInterpreter { public static void main(String[] args) throws FileNotFoundException { Scanner scan = new Scanner(System.in); Scanner input = new

import java.util.*;

import java.io.*;

public class YazInterpreter {

public static void main(String[] args) throws FileNotFoundException {

Scanner scan = new Scanner(System.in);

Scanner input = new Scanner(new File("simple.txt"));

intro();

while (input.hasNextLine()) {

String activation = input.nextLine();

choices(scan, activation);

}

}

public static void intro() {

System.out.println("Welcome to YazInterpreter!");

System.out.println("You may interpret a YazLang program and output");

System.out.println("the results to a file or view a previously");

System.out.println("interpreted YazLang program.");

System.out.println();

}

public static void choices(Scanner scan, String activation) throws FileNotFoundException {

System.out.print("(I)nterpret YazLang program, (V)iew output, (Q)uit? ");

String choice = scan.nextLine();

while (!(choice.equalsIgnoreCase("Q"))) {

if (choice.equalsIgnoreCase("V")) {

interpretInput(scan);

command(scan, activation);

} else if (choice.equalsIgnoreCase("I")) {

interpretInput(scan);

interpretOutput(scan);

System.out.println();

}

System.out.print("(I)nterpret YazLang program, (V)iew output, (Q)uit? ");

choice = scan.nextLine();

}

}

public static void command(Scanner scan, String activation) {

String choice = scan.next();

if (choice.equals("CONVERT")) {

convert(activation);

} else if (choice.equals("RANGE")) {

range(activation);

} else if (choice.equals("REPEAT")) {

repeat(activation);

}

System.out.println();

}

public static File interpretInput(Scanner scan) throws FileNotFoundException {

System.out.print("Input file name: ");

String inputName = scan.nextLine();

File inputFile = new File(inputName);

while (!inputFile.canRead()) {

System.out.print("File not found. Try again: ");

inputName = scan.nextLine();

inputFile = new File(inputName);

}

return inputFile;

}

public static void interpretOutput(Scanner scan) throws FileNotFoundException {

Scanner tokens = new Scanner(interpretInput(scan));

System.out.print("Output file name: ");

String outputName = scan.nextLine();

File outputFile = new File(outputName);

PrintStream output = new PrintStream(outputFile);

System.out.println("YazLang interpreted and output to a file!");

}

public static int convert(String temperature) {

Scanner tokens = new Scanner(temperature);

String type = tokens.next();

int temp = tokens.nextInt();

String unit = tokens.next();

if (unit.equalsIgnoreCase("C")) {

temp = ((int)(temp * 1.8) + 32);

System.out.println(temp + "F");

} else if (unit.equalsIgnoreCase("F")) {

temp = (int)((temp - 32) / 1.8);

System.out.println(temp + "C");

}

return temp;

}

public static void range(String increase) {

Scanner tokens = new Scanner(increase);

String type = tokens.next();

int first = tokens.nextInt();

int last = tokens.nextInt();

int increment = tokens.nextInt();

int sum = 0;

System.out.print(first + " ");

while (sum

sum = first + increment;

System.out.print(sum + " ");

}

System.out.println();

}

public static void repeat(String repetition) {

Scanner tokens = new Scanner(repetition);

String type = tokens.next();

while (tokens.hasNext()) {

String word = tokens.next();

word = word.replace("_", " ");

if (word.startsWith("\"")) {

word = "" + word.substring(1, word.length());

}

if (word.endsWith("\"")) {

word = word.substring(0, word.length() - 1) + "";

}

int printTimes = tokens.nextInt();

for (int i = 1; i

System.out.print(word);

}

}

System.out.println();

}

}

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
DIFF SPLIT DIFF YOUR OUTPUT EXPECTED Welcome to YazInterpreter! Welcome to YazInterpreter! You may interpret a Yazlang program and output You may interpret a Yazlang program and output the results to a file or view a previously the results to a file or view a previously interpreted Yazlang program. interpreted Yazlang program. (I)nterpret Yazlang program, (V)iew output, (I)nterpret Yazlang program, (V)iew output, (Q)uit? i (Q)uit? i Input file name: simple. txt Input file name: simple. txt Input file name: simple_out. txt File not found. Try again: q File not found. Try again: Exception in thread "main" java. util. NoSuchElementException: No line found at java . base/java. util. Scanner . nextLine (Scanner . java : 1651) at YazInterpreter . interpretInput (YazInterpreter . java : 60) at YazInterpreter . interpretOutput (YazInterpreter . jav a: 67) at YazInterpreter . choices (YazInterpreter . java: 34) at YazInterpreter . main(YazInterpreter . java: 13) + Output file name: simple_out. txt Yazlang interpreted and output to a file! + + (I)nterpret Yazlang program, (V)iew output, (Q)uit? q. Write a functionally correct Java program to produce specified console and file output. . Use Scanner and File to read input from a file. . Use PrintStream and File to write output to a file. . Use methods of the String class to process and manipulate string values. . Use methods to manage information flow and add structure to programs. . Follow prescribed conventions for spacing, indentation, naming methods, and header comments. Background Note: You do not need to read this section to complete the assessment, but it provides some helpful context that may make the assessment easier to understand. Throughout the quarter, we have been working with the programming language Java. Java is an example of a compiled language, meaning that before we can run our code, we need to run it through a tool called a compiler to translate it into a language that the computer itself can understand and execute. But not all languages work this way. Some languages are what are called interpreted languages, meaning that the source code in the language can be read and executed directly using a tool called an interpreter. The language you will work with on this assessment, YazLang, is an example of an interpreted language. Sample Execution Welcome to YazInterpreter! You may interpret a YazLang program and output the results to a file or view a previously interpreted YazLang program. (I)nterpret YazLang program, (View output, (Quit? I Input file name: input . txt File not found. Try again: yazlang. txt File not found. Try again: interpret. txt Output file name: interpret-out. txt YazLang interpreted and output to a file! (I)nterpret YazLang program, (V)iew output, (Quit? View (I)nterpret YazLang program, (View output, (Quit? vi (I)nterpret YazLang program, (V)iew output, (Quit? v Input file name: interpret-out. txt -9 -6 -3036 39F gucci ganggucci ganggucci ganggucci ganggucci ganggucci ganggucci gang 110 humuhumunukunukuapua'a 5 12 19 26 33 24F (I)nterpret YazLang program, (V)iew output, (Quit? g Program Behavior In this assessment, you will create an interpreter for the programming language YazLang. (This language was named a former CSE 142 Head TA and Instructor, Ayaz, who led development of the language andthis assignment.) When interpretting a YazLang file, the program prompts the user for input and output file names. Then the program reads and executes the YazLang commmands in the input file and outputs the results to a different file. The user can later view the output file that was created or quit the program. Menu The program's menu should work properly regardless of the order or number of times its commands are chosen. For example, the user should be able to run each command (such as I or V) many times if desired. The user should also be able to run the program again later and choose the V option without first choosing the I option on that run, or to run the program and immediately quit with the Q option if so desired. Menu options should be case-insensitive (e-g. both Q and q should cause the program to quit). If an invalid option (other than I, V, or Q in any casing) is entered, the user should be reprompted. Interpreting YazLang Files Sample Input File (interpret. txt) When the user enters I from the menu, they should then be RANGE -9 9 3 prompted to enter an input file and an output file. The in- CONVERT 4 C put file should contain YazLang commands. Your program REPEAT "gucci_gang" 7 CONVERT 53 F should then read the input file, execute each command, and REPEAT "humu" 2 "nuku" 2 "apua'a" 1 print the output to the output file. If the input file does RANGE 5 35 7 not exist, the user should be reprompted until they enter CONVERT -4 C a file that does exist. See the Sample Execution above for an example of this reprompting behavior. No reprompting is necessary for the output file. If the output file does not exist, it should be created. If it does already exist, its contents should be overridden. (These are the default behaviors for the file input/output approaches we use.) You should assume that the input and output files are not the same. Sample Output File (interpret-out . txt) -9 -6 -3036 39F gucci ganggucci ganggucci ganggucci ganggucci ganggucci ganggucci gang 11C humuhumunukunukuapua'a 5 12 19 26 33 24F Viewing YazLang Output When the user enters V from the menu, they should then be prompted to enter an input file to view an interpretted YazLang program. If the input file does not exist, the user should be reprompted until they enter a file that does exist. See the Ed tests for examples of this reprompting behavior. When you are viewing an interpretted YazLang program, you are simply reading and echoing its contents to the console. You do not need to test that the specified file is a YazLang output file. Just output the file's contents (even if it is a different type of file). YazLang Commands YazLang includes three commands: CONVERT, RANGE, and REPEAT. These commands are described in the table on the next page. The syntax for YazLang is much simpler (and more limited) than Java's. Every YazLang command follows this pattern: COMMAND arg1 0192 .. arg, That is, every command consists of a single token indicating the command be executed, followed by some number of arguments. Some commands take a specific number of arguments, while others may take any Page 2 of 7Development Strategy 2 Once again, this assessment will be best approached in smaller chunks. We recommend the following strategy: "Hard coding" refers to em bedding a value (1) View: Write code to read and print the contents of an input file to the console. This will be used directly in your program rather for viewing YazLang output. (Even though you can't intepret YazLang programs yet, you can test than accepting this on any input file you like, including a YazLang program itself.) t as input. For example, you (2) Interpret a single command: Write code to execute a single YazLang command and print the might start by having your results to the console. You may want to begin by hard-coding the command and changing it as you program al- debug. You can then move on to reading a single command from an input file. We recommend ways process approaching the commands one at a time and in the order the appear in the above table (CONVERT, the command CONVERT O C then RANGE, then REPEAT). instead of a dif- ferent command (3) Intepret a YazLang file: Modify your code to read each line from a YazLang input file, execute each time it the command, and print the output to the console. Hard-code the file name for now. runs. (4) File output: Modify your code to produce output to a file instead of the console. Again, hard-code the file name for now. (5) File prompting: Add code to prompt the user for the input and output file to use (be sure to remove your hard-coding when you reach this point)- (6) Menu/Reprompting: Add code to allow the user to select whether to intepret, view, or quit and to reprompt if invalid mode is chosen or if an input file does not exist. Page 4 of 7 Hints The following suggestions and hints may help you be more successful on this assessment: . When reading input from a file, you may need to use a mixture of line-based and token-based processing as shown in class and described in chapter 6 of the textbook. (The FindMinAndMax and Payroll programs from class will be particularly helpful.) . To check if a file exists, you should use methods from the File class. The textbook describes an alternate technique for dealing with missing files using try/catch statements, but you should NOT use this approach on this assessment. You may find the startsWith method of the String class useful for determining which type of command you are processing. You may also find the replace method of the String class useful for replacing occurrences of one character with another. For example, the code: String atr = "mississippi"; atr = atr . replace("s", ">") ; will result in the string str containing the value "mix*i**ippi. The output from RANGE should end with a space-you do not need to use a fencepost approach for the output of this command. . If your program is generating InputMismatchException errors, you are likely reading the wrong type of values from your Scanner (for example, using nextInt to read a string). . If your program is generating NoSuchElementException errors, you are likely attempting to read

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions