Note: Scanner handout of last class is corrected: (position
= 4 at the space, not past the space after “18.4” scanned, etc. (as shown on
pg. 387)
Q: How do we code the sum for #2
2345
+ 9119
We add 5 and 9 and get 14, and put 4 in the sum and use the 1 for carry.
We add the carry of 1 plus 4 plus 1, and end up with result digit 6, carry = 0
..
Using array notation
x[0] + y[0] = 14: sum[0] = 4 = 14 -10 or 14%10, carry = 1 = 14/10.
X[1] + y[1] + carry = 6 = sum[1], carry = 0
…
In general, digit i:
s = x[i] + y[i] + carry , sum[i] = s%10, carry = s/10
Make a loop with this body
Demo on using the new Wiki
Follow the link on the class web page to the Wiki, then find Programming Project 1 listed on the right-hand side and click it, then see a button at the top for posts and click it, read posts and/or post a new question.
Type in PointExample.java, pg. 505,with “import java.awt.*” to get the JDK Point class. (PointExample.java is not available online)
Add a Scanner on a String and print it out too, see position changing (its radix not reported, unfortunately)
Scanner input = new Scanner("ab 123");
System.out.println(" input Scanner = " + input); // see position = 0
String s = input.next();
System.out.println(" after input.next(), s = " + s + " input = " + input); // see position = 2
This uses the Point class from the JDK, and this requires the import you see on line 1 on pg. 505.
Point p = new Point(3,8); // Point constructor call
Compare to
Scanner in = new Scanner(“ab cd”); // Scanner constructor call
System.out.println(“initially p = “ + p); // this is asking for p to provide a String to describe it, which is then printed out.
Similarly we can print out the internal state of a Scanner by asking for a String from it.
Not all object classes do a good job at this. For example arrays are objects, but don’t provide useful Strings for output.
But it’s often worth a try for debugging with various JDK classes.
Print the point’s x coordinate as a bar:
Ex Point at (5,1), print “-----“ 5 hyphens