Reading: Chapter 6.
Due (NOTE THIS HAS CHANGED): At 8:00am, January 22, 2001, submit is turned off automatically. Work turned in after that time will not be accepted for grading.
In mathematical modelling, it is often convenient to have the derivative df/dx of a function as well as the function f itself. Numerical differentiation evaluates f twice, to compute (f(x+v)-f(x))/v for some small value of v. Program differentiation computes the derivative symbolically. Derivative arithmetic, on the other hand, works simply and efficiently without the problems involved in symbolic differentiation. For this we augment an expression with its derivative, i.e. we represent it by the ordered pair (e, de/dx). Thus we have:
(a, b) from the first
item below.
(a , b ) Push this number onto the stack.
+, -, *, / The topmost element of the stack is the right operand for this
operator, and the one immediately below it is the left operand. Remove
them, perform the operation, and push the result.
W Remove the top element of the stack and write it on a new line. See
sample output below
D Push a copy of the top element of the stack onto the stack.
Q Stop. This will always be the last character.
(2,1)(2,1)*(2,1)/W (3,2)(1,0)+DW(1.5,2.3)(10,20)/*W Q
(2.0, 1.0) (4.0, 2.0) (0.6, 0.019999999999999962)
(2 ,1 )(2 ,1 )*(2 ,1 )/W (3 ,2 )(1 ,0 )+DW(1.5 ,2.3 )(10 ,20 )/*W QIn this case you can use
Console.in.readCharNonwhite() and
Console.in.readDouble() to do all of your input.
Console.in.readDouble() assumes that the number is terminated
by a white space character, hence the assumption.
To get full credit for this assigment your program will have to work WITHOUT making use of this assumption.
Console.in.readLineNonwhite(), storing the result in a
character array. (readLineNonwhite() has been recently added to tio. The
version in ~mcdowell/java/public contains the new method. You can obtain
a copy of the new tio from
here.
There is a static method, Double.parseDouble(), that will parse a String into a double. The String must consist of just the characters representing the number. In particular, there cannot be any leading or trailing white space. Given a String, the method trim() returns a new String, with all leading and trailing white space removed.