Assignment 7Due March 10, 2021
Part of
the homework for CS:2820, Spring 2021
|
A comment here seems unnecessary.
public String next( String pattern ) { this.skip( "\\s*" ); // skip whitespace this.skip( pattern ) return this.match().group() }
Now, how can we write
hasNext(String pattern) using more primitive parts of the
class (the parts that ignore delimiters)?
a) We can't because hasNext() would have to skip whitespace.
b) The hasNext() method can be built on top of
findWithinHorizon().
c) Just return false if skip throws an exception.
d) We can't because the more primitive Pattern matching
methods of class Scanner all
advance the scanner over whatever the pattern matches.
— correct
e) We can use skip() to see if the pattern is there and then,
if it is, use reset() to put it back so our version of
next() can re-skip it.
If we made hasNext() skip whitespace, you would have to work very hard to notice that it had done so, so a) is not a big problem. Both findWithinHorizon() and skip() advance the scanner if the pattern matches. As a result, there is no way these can be used to do hasNext() unless there is a way to back up the scanner and un-skip the pattern that just matched. This rules out b) and c) and also suggests that d) may be the answer. The answer would be e) except for one big detail, reset() on a scanner does not "put it back" to where it was before a call to skip().
abstract class C { protected int i; public abstract int m( int i ); }
The main point of abstract classes is to allow them to be used to form subclasses, so b) is very wrong. Declaring a field to be protected permits it to be seen from subclasses, so c) is wrong. You cannot create an instance of C, so d) is not quite right. Any wrong answers rule out e).
For MP5, the MyScanner class implementation works with
semicolons, dashes and parentheses so long as they are ...
a) both preceeded and followed by whitespace.
b) preceeded by whitespace, but may be followed by anything.
c) followed by whitespace, but may be preceeded by anything.
d) the last character on their line.
e) are not the first character on the line.
The version of MyScanner in question uses hasNext() to see if the pattern is there. If there was whitespace before the pattern, it will be ignored, but whtespace is not required there, as you can easily determine by running the highway network simulator to see how it works with spaces before and after semicolons. Using hasNext() does require whitespace after the semicolon. Therefore, a) and b) are false, and c) is true. Semicolon as the first character on the line is unconventional and as the last character on a line is conventional, but all the scanner ever cared about was whitespace, and end of line is just whitespace, so d) and e) are misleading, at best.
int a = some + unknown + expression; return "a = " + a;
In answering this, assume that if sb is a
StringBuilder, then sb.toString() is not
optimized; it always makes a copy of the stringBuilder into a string.
Also, do not forget about autoboxing.
a) 1
b) 2
c) 3
d) 4
— correct
e) 5
First, let's go through the exercise of removing the syntactic sugar from that return statement. First, let's use a StringBuilder to do the concatenation:
return new Stringbuilder( "a = " ).append( a.toString() ).toString();
That's just a start, because a is an int, not a real object. To apply the toString() method to it, we need convert it to class Integer; this is what autoboxing does:
return new Stringbuilder("a = ") .concat(new Integer(a).toString()) .toString();
Now, we can start counting:
That comes to 4, so d) is right.
The string constant "a = " does not involve creating a new object at run-time. This constant was created by the compiler and put in memory when your program begins running. No matter how many times you call the method that has this return statement, it will always use the same constant and not create any new objects.