Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- readme.txt -- for exps-have-numbers-ss
- THE TASK
- In this task modify SpreadSheet.java so that numbers can be directly
- entered as expression operands in formulas, freely mixed with cell
- references.
- For this task, a number is defined as a character string
- that can be interpreted as a double value using Java's
- Scanner::nextDouble() method WITH THE EXCEPTIONS that you may assume
- 1. Operator characters (in particular "+" and "-") are not present,
- so all numbers are non-negative, and there are no embedded
- operators, as in 1.3e-6 and 2e+5.
- REED'S NOTES:
- 1) Info on Operator characters: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html
- 2) non-negative: either positive or equal to zero.
- 3) embedded operators: Translation?
- 2. SignedNonNumbers such as NaN and Infinity are not present.
- REED'S NOTES:
- 1) SignedNonNumbers: Translation?
- 2) NaN: not a number, a numeric data type value representing an undefined or
- unrepresentable value(especially in floating-point calculations).
- Video demonstrations of a solution for this task are available.
- 1. During the exam: as exps-have-numbers-ss.html in the "Solution Demonstration Videos"
- folder on the desktop in the exam delivery environment.
- 2. During exam preparation, outside the exam environment.
- https://s3-us-west-2.amazonaws.com/proxor-video/video/exps-have-numbers-ss-transcoded.mp4
- REED'S NOTES:
- ***SCANNER***
- 1) Simple text scanner, can parse primitive types & strings using regular expressions.
- 2) Scanner breaks input into tokens using delimiter pattern(default matches whitespace).
- 3) The resulting tokens may then be converted into values of different types using the various *NEXT METHODS*.
- EX:
- // allows a user to read a number from System.in
- Scanner sc = new Scanner(System.in);
- int i = sc.nextInt();
- EX:
- // allows long types to be assigned from entries in a file myNumbers
- Scanner sc = new Scanner(new File("myNumbers"));
- while (sc.hasNextLong()) {
- long aLong = sc.nextLong();
- }
- 4) Scanner can use delimiters other than whitespace.
- EX:
- // reads several items in from a string:
- String input = "1 fish 2 fish red fish blue fish";
- Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
- System.out.println(s.nextInt());
- System.out.println(s.nextInt());
- System.out.println(s.next());
- System.out.println(s.next());
- s.close();
- Output:
- 1
- 2
- red
- blue
- 5) Same output can be generated with code bellow,
- uses regular expression to parse all four tokens at once:
- EX:
- String input = "1 fish 2 fish red fish blue fish";
- Scanner s = new Scanner(input);
- s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
- MatchResult result = s.match();
- for (int i=1; i<=result.groupCount(); i++)
- System.out.println(result.group(i));
- s.close();
- 6) more on Scanner:
- http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
- REED'S NOTES:
- ***NEXTDOUBLE()***
- 1) Scans the next token of the input as a double.
- 2) Returns the double scanned from the input.
- 3) will throw InputMismatchException if the next token cannot
- be translated into a valid double value.
- If the translation is successful,
- the scanner advances past the input that matched.
- 4)
- EX:
- public static void main(String[] args) {
- String s = "Hello World! 3 + 3.0 = 6 true";
- // create a new scanner with the specified String Object
- Scanner scanner = new Scanner(s);
- // use US locale to be able to identify doubles in the string
- scanner.useLocale(Locale.US);
- // find the next double token and print it
- // loop for the whole scanner
- while (scanner.hasNext()) {
- // if the next is a double, print found and the double
- if (scanner.hasNextDouble()) {
- System.out.println("Found :" + scanner.nextDouble());
- }
- // if a double is not found, print "Not Found" and the token
- System.out.println("Not Found :" + scanner.next());
- }
- // close the scanner
- scanner.close();
- }
- OUTPUT:
- Not Found :Hello
- Not Found :World!
- Found :3.0
- Not Found :+
- Found :3.0
- Not Found :=
- Found :6.0
- Not Found :true
- 5) more on nextDouble():
- http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextDouble()
- REED'S NOTES:
- ***EXCEPTIONS you may assume***
- 1) InputMismatchException -- if the next token does not match the Float regular expression, or is out of range
- 2) NoSuchElementException -- if the input is exhausted
- 3) IllegalStateException -- if this scanner is closed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement