2.8 Review Exercises 2

Exercise 2.8

  1. Evaluate. Use a decimal point in your answer if the result is double.
    a) 15 / 6 + 15 % 6   b) 3e-1 + 2e1
    c) 2 / 5 * 8.0       d) 1.6 * 20 % 8
    e) (double) (25/4)  f) (int) 2.7 + 6.3
    g) 20 - 10* (15%4)   h) 7/(-5) + 4%(-3)
    i) 2*3/(double)4    j) (int ) 4.8 % 1.1 
  2. Each of the following expressions is intended to evaluate the expression.

    Some are correct while others are not. Classify each as being correct or incorrect. For those that are incorrect, give the reason(s).
    a) (a *x *x + b)/ ((c *x) + d)
    b) (a * Math.pow(x,2) + b) / (c *x + d)
    c) ((a)(x)(x) + (b)) / ((c)(x) + d)
    d) (b + x * (x *(a))) / (d + x * (c))
    e) (a) * Math.pow(x,2) + (b) / ((c) *(x) + d)
    f) (a *(x *x) + (b) / c *(x) + d
    
  3. Suppose that the following declarations have been made.
    int i = 3, j = 4, k = 2; 
    Using these starting values in each part, find the value of each variable after the given statement has been executed.
    
    a) j = ++i * k--; 	b) i --j + k/2;
    c) k = i-- - j++; 	d) j = (2*i++)%(++k + 1);
    e) i += j -= --k; 	f) i *= j /= k++;
    
  4. State the value of each expression.
    a) (char)('b' + 6) 	b) (int)('M' - 'T')
    
  5. Evaluate.
    a) Math.round(Math.sqrt(20))
    b) Math.ceil(-4.6)
    c) Math.min(0.0024,1.2e-3)
    d) Math.pow(0.5,-4)
    
  6. Write as Java expressions.

  7. Write as Java statements.

  8. To switch the values contained in the variables x and y, a programmer wrote the following segment:
    x = y;
    y = x;
    
    
    (a) If, before execution of the segment, x contained the value 7 and y contained the value 4, what value would each have after the segment was performed?

    (b) Rewrite the segment so that it performs the intended task correctly.

  9. Write a program that reads three double values and computes their mean, rounded to two decimal places.

  10. The current deficit of Arrakis is 47 000 000 grods and it is estimated that the deficit will increase by 4.5% in each of the next two years. Write a program that will find the estimated deficit in these two years, rounded correctly to the nearest million grods.

  11. Write a program that asks the user for a three-digit number, finds the sum of the digits of the number, and then prints both the number and its digit sum.

  12. Projects

  13. As you are probably aware, the date of Easter can vary widely from year to year. The Council of Nicaea in the year 325 decreed that Easter should be held on the first Sunday following the full moon that occurs on or after March 21, the usual day of the vernal equinox. Because the date depends on solar, lunar, and calendar cycles, it is not easy to find an arithmetic procedure that determines the correct date for any given year. The one given below (for the Gregorian calendar) was created by J. M. Oudin in 1940. In the equations, y represents the year, m represents the month (3 for March and 4 for April), and d represents the day of the month. All variables are integers; any remainders produced by divisions should be discarded.

  14. In order for an object to escape a planet's gravitational pull, it must attain a minimum initial velocity called the escape velocity. The escape velocity varies from planet to planet but it is the same for all objects on a given planet. Assume that we are analyzing the data that a small probe has collected while exploring some mystery planet. The probe has managed to obtain the circumference of the planet and the acceleration due to gravity at the surface. The probe must now determine what initial velocity it requires for takeoff in order to remove itself from the planet's gravitational force. You are to create a Java program that will determine this velocity. Your program should first prompt the user for the circumference of the planet and then the acceleration due to gravity on the planet. From this information your program should determine the radius, mass, and escape velocity of the planet using the following equations.

    In these equations, m kg is the planet's mass, r km is the planet's radius, G is the gravitational constant approximated by 6.6726 x 10-11m3kg-1s-2, and a m/s2 is the acceleration due to gravity on the surface of the planet. Your program should read input and print output as shown in the following example. (The values shown after the question marks are supplied by the user.)
    Circumference (km) of planet? 38000
    Acceleration due to gravity (m/s^2) on planet? 9.8
    
    Calculating the escape velocity ...
    Planet radius: 6047.9 km
    Planet mass: 5372.0 x 10^21 kg
    Escape velocity: 10.9 km/s
    
    Note: All results are rounded to one decimal place for printing. Your program must handle the unit conversions as well as printing the planet's mass in terms of 1021 kg. Assume that this will always produce reasonable results. Assume that the planet is perfectly spherical.