CSCI 157 - HW 1

Homework 1 - thru 1.2

  1.  *  What are the values of the following expressions? Assume
          int    a = -5;
          int    b =  4;
          double c =  8.0; 
    
      a)   a + b < c || ( 2 * b + c / 9 > a && a + b <= c )
    
      b)   a / 9 + b >= c || ( 1 * b % 3 - c / 2 > a && b - a <= c )
    
      c)   a / 3 + b > c || (-1 * b + c > a && 2 * a <= c )
    
      d)   a + b % 2 > c || ( a != b && - c > 2 + a && b >= c )
    
      e)   3 / a > c && ( a == b || - c > 2 - 2 * a || b >= c )
    

  2. Using System.out.printf and the appropriate Math class methods to perform the following.

    Use string literals to fully label what you've computed. For example, to find the absolute value of -92 you'd provide the following:

                   System.out.printf("\nAbsolute value of -92 is: %d", Math.abs(-92) );
        
    1. compute and output the value of   812

    2. compute and output the value of the square root of 157
  3. (Program!) Using only various variable declarations and mathematical and logical expressions, write a program that takes two integer command line arguments m and d and prints true if day d of month m is between 8/20 and 11/20. Program should otherwise print false
  4. (Program!) Write a program that calculates and prints the amount of money you would have after t years if you invested P dollars at an annual interest rate r where the desired value is given by formula
                                       Pert
         
    t,P, and r should all be obtained from command line arguments.
  5. (Program!) If you don't already know, look up the definition of a "Great Circle".

    Now, write a program that takes four double command line arguments -- lat1,lng1,   lat2,lng2-- (the latitude and longitude, in degrees) of two points on the earth's surface and prints the great circle distance between them. The great circle distance, measured in nautical miles, is calculated by the following equation:

                                       60 arccos(
                                                 sin(lat1)sin(lat2) 
                                                 + 
                                                 cos(lng1)cos(lng2)cos(lng1-lng2)
                                                )
          
    Be attentive to both the units provided by your user (degrees) and the units expected and returned by the various Math methods you are using.

About that  *  ...

At all times I require you to "Show all your work". If the answer is 12, I'm going to want to know exactly how you came to that conclusion. On this homework, particularly on exercises resembling those in 4.8.5,etc (the  * 'd ones!) many of you will be tempted to keep all or some steps in your head. Don't. This will not be worth full credit. So ... show it all.

Consider the following hypothetical problem.

    What is the value of the following expression if w = 3, q = -2, t = 1?

    ( w + t > q*3 || 2 * q + t > 5 / w ) && !( q >= t)
I recommend you re-write the expression on your page, exactly as given. THEN, substitute in the values. Only when you've done both of those steps, should you start going through a careful sequence of evaluations of the various operators, keeping operator precedence in mind.


Here's an answer to which I would happily give full credit. Student has CLEARLY demonstrated a full understanding of working and precedence of all operators.

  i) ( w +  t   <  q*3 || 2 *  q  + t >  5 / w ) && !(  q >= t)
     ( 3 +  1   < -2*3 || 2 * -2  + 1 >  5 / 3 ) && !( -2 >= 1)
     (   4      < -6   ||  -4     + 1 >  1     ) && !( false  )
     (      false      ||     -3      >  1     ) && true         
     (      false      ||     false            ) && true         
                      false                      && true         
                                               false             

Versus a partial credit... person has shown work for evaluating logical operators but neglected to do so for comparison and mathematical operators.
 ii) ( 3 +  1   < -2*3 || 2 * -2  + 1 >  5 / 3 ) && !( -2 >= 1)
                                       false     && true         
                                               false    

Another partial credit... person has shown work for evaluating mathematical operators but neglected to do so for comparison and logical operators.

 ii) ( w +  t   <  q*3 || 2 *  q  + t >  5 / w ) && !(  q >= t)
     ( 3 +  1   < -2*3 || 2 * -2  + 1 >  5 / 3 ) && !( -2 >= 1)
     (   4      < -6   ||  -4     + 1 >  1     ) && !( -2 >= 1)
                                               false             

I would mark both of the following as completely wrong altho' you may at first think "Hey! One's for sure right!" Can you see why? No work has been shown, just substitution of values.
iii) ( 3 +  1   < -2*3 || 2 * -2  + 1 >  5 / 3 ) && !( -2 >= 1)
                                               true    


 iv) ( 3 +  1   < -2*3 || 2 * -2  + 1 >  5 / 3 ) && !( -2 >= 1)
                                               false