Assignment 5, due Feb 15
Part of
the homework for CS:2820, Spring 2019
|
Assignments are to be turned in on paper. On every assignment, write your name, course and section number at the top of each page. Write your name as it appears in your university records! Use the section number for which you are registered! We will not do detective work to figure out who did what. Work must be legible.
Homework is due on paper in discussion section, usually at the start except when the assignment indicates that time to work on the assignment will be provided in class. Exceptions will be made only by advance arrangement with your TA (excepting "acts of God" outside your control). Never push homework under someone's door!
gate a xor 6.7 gate b threshold 1 3.6 wire a 3.2 b wire b 1.5 a 1.8 a
There is a serious mismatch here that makes it difficult for our code to recover from errors in the input file. The problem is, the scanner services treat newlines as just another form of space, while our input format wants newlines to be significant. We can fix this, to some extent, by using the nextLine method of Java's Scanner class.
a) Where, in the code for the constructors for Road(), Intersection() Gate(), or Wire() should the call to the nextLine() method be put? (0.5 points)
b) If you wanted to detect and complain about extraneous text after the definition of one of the objects mentioned above, what would you check for in the value returned by nextLine()? (0.5 points)
# test script for road network java RoadNetwork echo "that should complain about a missing parameter" java RoadNetwork "no such file" echo "that should complain it can't open 'no such file'" java RoadNetwork "no such file" "and more junk" echo "that should complain that there is 'and more junk' and about the file"
a) (0.5 points) Fix the first (non-comment) line of the script so that it uses the if command to test the exit status of RoadNetwork so that the script outputs FAILURE, it should have failed if RoadNetwork exited normally.
b) (0.5 points) Modify your answer to part a so that, in addition to testing the exit status, it also tests RoadNetwork's output to standard output and standard error. Specifically, if the program fails as it should under this test, your script should complain FAILURE, there was output to standard output if there was, and FAILURE, there was no output to standard error if there was not.
If you need an empty file to compare with, use the shell command touch emptyfile at the start of your script to create it and rm -f emptyfile at the end of your script to delete it.
a) Our first solution involves a new boolean utility method, hasNextName(sc). This returns true if the next token in the scanner's input stream exists and is not a legal float. Give code for hasNextName(sc) using only hasNext() and hasNextFloat(). (0.5 points)
b) Explore the documentation for Java's scanner class and identify methods offered by the scanner that can be used to build very specific hasNext() and next() methods that will only scan specific and arbitrarily described types of tokens. Identify the candidate methods. (Later, we will explore using them; they are complex enough that we'll defer actually using them to solve this problem right now.) (0.5 points)