This lab introduces how to handle files as both input and output. We’re coming back to Tracery (which you implemented in Lab 1) with this assignment but instead of always reading the grammar from a file and writing the expanded grammar to the command line, we will also be able to read a grammar from the command line and write the expanded grammar to a file. To do this, you’ll be learning about some of the IO (input/output) classes in Java, and how the Java concept of a Stream provides an abstraction layer above files and console I/O. You will be doing the following: ● Import the correct IO packages from the Standard Library ● Process command line arguments that determine whether the program is reading/writing from the console (command line) or from a file ● Load a grammar either from the console or from a file, based on the arguments the program was given ● Write the expanded grammar to either the command line or a file, based on the arguments the program was given Open the file TraceryRecursion.java that came with the lab. This is where you’ll make all your changes. Every place in the file where you’re supposed to make a change is marked with a comment labeled To Do. Importing packages in Java Try compiling the code we’ve given you. If you haven’t made any changes it’ll tell you it can’t find the following symbols: ● class Hashtable ● class PrintStream ● class InputStream But in Lab 1 we were able to use a Hashtable, so what’s different? The problem is that we’re trying to use something from the Java Standard Library without telling Java that we are. To tell Java that we’re using something from the standard library, you use the import statement. In Lab 1 we had already included the correct imports for you. For lab 2 the compiler is telling you that you need to import these three classes in order to use them. As an example of importing a class, to import Hashtable from the standard library so you can use it, you would put this statement at the beginning of your program: import java.util.Hashtable The java.util part is the package (a collection of classes) that Hashtable belongs to. To figure out the package of a class you want to import from the standard library, you look at its documentation page. The documentation for Java’s Hashtable is here. In addition to all the info about where it is in the type hierarchy and its accessible fields and methods, the documentation page says what package the class is part of at the top of the page right above the class name. To import PrintStream and InputStream you can look up the package they belong to at their respective documentation pages (here and here). Another trick to know about, though you don’t really need to use it in this lab, is how to import a bunch of classes from the same package at once. To import all of the classes from a package, you can use statement: import .* Command line arguments This program can take the following command line arguments: -in -out
The Codes Hive believes in helping students to write clean codes that are simple to read and easy to execute.Based in New York, United States, we provide assignment help, homework help, online tutoring and project help in programming to the students and professionals across the globe.
Disclaimer: The reference papers/tutorials provided are to be considered as model papers only and are not to submitted as it is. These papers are intended to be used for research and reference purposes only.
Why You Should Hire Codeshive Tutors
Guaranteed A or B Grade
Money back guarantee
Save Time
Achieve more in less time
24/7 Customer support
You're viewing: CMPS 12B Lab 2: File Input and Output solvedOriginal price was: $35.00.$35.00Current price is: $35.00.$17.50