CPS 1: Computer Science Fundamentals

J. Forbes
September 21, 2001



Name _______________________________________ Login _______ Lab _________

Acknowledgment of Duke Honor Code ______________________________________

Quiz 3 (10 points)

For the problems below, parse the sentences with the rules of Java given from the Course Pack. You must put down the rule number for each rule you use. You can start from <statement> and work down to the actual code as in:

  add(m1);
#4 <statement> => <method>(<arguments>);
#11               add(<arguments>);
#5                add(<expression>);
#6                add(<oth-expresssion>);
#32               add(<name>);
#1                add(m1);

or you can work from the code up to <statement> as in:

    add(m1);
#11  <method>(m1);
#1   <method>(<name>);
#32  <method>(<oth-expression>);
#6   <method>(<expression>);
#5   <method>(<arguments>);
#4   <statement>

Just make sure that you are explicit about what rules you are using at each step.

  1. (5 pts.)

    y = y + 4 + x;
solution:

#2 <statement> => <expression>;
#6                <int-expression>;
#26               <int-expression><op><int-expression>;  
#28               <name><op><int-expression>;
#29                 y   <op><int-expression> ;
#30                 y     + <int-expression> ;
#26                 y     + <int-expression><op><int-expression> ;   
#29                 y     + <positive-int><op><int-expression> ;   
#25                 y     +       4       <op><int-expression> ;       
#30                 y     +       4         + <int-expression> ;
#28                 y     +       4         + <name> ;   
#1                  y     +       4         +   x    ;   
    
    
or working up to statement:

y = y + 4 + x;



#1                 y     +        4         + <name> ;
#28                y     +        4         + <int-expression> ;
#30                y     +         4       <op><int-expression> ;
#25                y     +   <positive-int><op><int-expression> ;
#29                y     + <int-expression><op><int-expression> ;
#26                y     + <int-expression> ;
#30                y   <op><int-expression> ;
#1              <name><op><int-expression>;
#28   <int-expression><op><int-expression>;
#26               <int-expression>;
#6                <expression>;
#2                <statement>

  1. (5 points)

     m1.setText("hello world!");
solution:
    #2 <statement> => <expression>;
    #6                <string-expression>;
    #23               <name>.<string-method>(<arguments>);
    #1                    m1.<string-method>(<arguments>) ;
    #22                   m1.setText(<arguments>);
    #5                    m1.setText(<expression>);
    #6                    m1.setText(<string-expression>);
    #8                    m1.setText(<string>);
    #9                    m1.setText("hello World");
    

or working the other way

    m1.setText("hello World")
    #9                    m1.setText(<string>);
    #8                    m1.setText(<string-expression>);
    #6                    m1.setText(<expression>);
    #5                    m1.setText(<arguments>);
    #22                   m1.<string-method>(<arguments>) ;
    #1                <name>.<string-method>(<arguments>);
    #23                <string-expression>;
    #6                 <expression>;
    #2                 <statement>;
    
    

End of quiz