70 likes | 163 Views
Homework 2. Reference DTD (xmath.dtd): http://xml.cs.ncuc.edu.tw/courses/xmlta/xmlta2009/HWs/hw2/xmath.dtd Reference sample xmath script: http://xml.cs.ncuc.edu.tw/courses/xmlta/xmlta2009/HWs/hw2/script1.xmath
E N D
Homework 2 • Reference DTD (xmath.dtd): • http://xml.cs.ncuc.edu.tw/courses/xmlta/xmlta2009/HWs/hw2/xmath.dtd • Reference sample xmath script: • http://xml.cs.ncuc.edu.tw/courses/xmlta/xmlta2009/HWs/hw2/script1.xmath • The above dtd defines a simple script language for defining double variables and single-variable polynomial function.
Sample script : Script1.xml • non-xml version: a = -2 define f(x) = 3x^2 - 5x + 7 define g(x) = 3x^3 -5x^2 +2 combine h = f + g list functions list variables print "The value of f(1) is" f(1) "." print "The value of f(a) is " f(a) "." print "The derivative of g' is " g' "."
Expected output a = -2.0 f(x) = 3x^2 - 5x + 3 g(y) = 3y^3 - 5y^2 + 2 h(x) = 3x^3 - 2x^2 - 5x + 5 The value of f(1) is 1.0. The value of h(a) is -17.0. The derivative of g is g'(y) = 9y^2 - 10y.
Script1.xml : XMLVersion <?xml version="1.0" ?> <!DOCTYPE script SYSTEM "xmath.dtd" > <script> <assign var="a"><number>-2</number></assign> <define name="f" var="x" > <polynomial var="x" > <term c="3" exp="2" /> <term c="-5" exp="1" /> <term c="3" exp="0" /> </polynomial> </define> <define name="g" var="y" > <polynomial var="y" > <term c="3" exp="3" /> <term c="-5" exp="2" /> < term c="2" exp="0" /> </polynomial></define>
<combine name="h" type="add" fun1="f" fun2="g" /> <list option="variables" /> <list option="functions" /> <print> <literal>The value of f(1) is </literal> <funEval name="f" ><number>1</number></funEval> <literal>.</literal> </print> <print><literal>The value of h(a) is </literal> <funEval name="h"><varRef>a</varRef></funEval> <literal>.</literal></print> <print><literal>The derivative of g is </literal> <derivative>g</derivative><literal>.</literal></print> </script>
HW2 • HW2 : • This homework is about using DOM API to write a Java interpreter that can load a script file in XML format that are valid w.r.t the previous DTD, and then execute it and produce correct result. • We have prepare an almost-completed program HW2.java for you. • Read carefully and try to understand how this program works. • Complete all missing part in HW2.java
Notes • We have prepared 3 classes for representing polynomial, terms and functions, respectively. • see http://xml.cs.nccu.edu.tw/courses/xmlta/xmlta2009/hws/hw2/src/xmlta/xmath" • To help grading, please remember that your main java classes should be named as HW2. it should have a main(String[]) method accepting as input an input file name. So we can invoke them as follows: >java HW2 script1.xmath • I have packaged all related files into one eclipse project hw2.zip, which you can download and import it into your local eclipse workspace. • After completing your homework, you need just submit HW2.java to c3@cs.ncuc.edu.tw.