Chapter 14
Primitive Operations and Expressions

   14.1 Background
   14.2 Assignment Operations
   14.3 Type Conversion
   14.4 Arithmetic Operators
   14.5 Comparison Operations
   14.6 Boolean Operations
   14.7 Short Cuts
   14.8 Initialization of Parameters and Returned Values
   14.9 Assignment #18: Primitive Data Types
   14.10 Assignment #19: Primitive Data Types

14.1 Background

14.2 Assignment Operations

14.3 Type Conversion

14.4 Arithmetic Operators

14.5 Comparison Operations

14.6 Boolean Operations

14.7 Short Cuts

+=’, ‘-=’, ‘*=’, ‘/=’, ‘%=’, ‘++’, ‘--

14.8 Initialization of Parameters and Returned Values

Exercises

14.9 Assignment #18: Primitive Data Types

Due: We, May 28, midnight

  1. Download the lab18a.java program from the given web page, and provide to the method tick the needed code for advancing the recorded time by one minute. The program should produce the following output.
    time =   1:05 
    time =  10:15 
    time =   0:00 
    time =   1:00 
  2. The equations for deriving the coordinates (xh,yh) of the hour hand of a clock, and the coordinates (xm,ym) of the minutes hand, are as follows.
        (xm,ym)            hours- 3+minutes/60
 --||||||        a = 2p-------12-------
--   |  --
-- (x,y)- |(xh,yh)    xh = x+ Rh cos(a)
 -||   |--
   |||||            yh = y+ Rh sin(a)
                           minutes- 15
                     b = 2p---60----

                    xm = x+ Rm cos(b)

                    ym = y+ Rm sin(b)

    In the equations, Rh and Rm denote the lengths of the clock hands, and (x,y) denotes the coordinates of the center of the clock.

    Download the lab18b.java program from the given web page. Provide the missing code in the calc method, so that it will compute the coordinates of the clock hands.

    For instance, the time 8:50 and the values x = 80, y = 80, and r = 60, imply xh = 50.1, yh = 82.6, xm = 41.0, and ym = 57.5 for clock hands of length r/2 and 3r/4. Similarly, the time 5:20 implies the values of xh = 90.3, yh = 108.2, xm = 118.9, and ym = 102.5.

  3. Download the clock.java program from the given web page, replace there the comments /* code from tick */ and /* code from calc */ with the code introduced to the tick and calc methods, and then compile and execute the program.

    Just for the fun of it, can you improve the look of the analog clock?

Assume ‘lab18’ for the submit command, and submit your clock clock.java program.

Note: Files that fail to compile, and execute when applicable, will not be examined. They will be awarded a grade of 0 points.

Q&A

14.10 Assignment #19: Primitive Data Types

Due: Fr, May 30, midnight

  1. Implement the ‘Compute’ class whose interface is given.
    class Compute { 
      static boolean cast(int, int, int); 
      static boolean equal(int, int, int); 
      static int interest(int, int, int); 
      static double root(int, int, int); 
      static boolean sign(int, int, int); 
    } 

    When used by the program

    class prg { 
      public static void main ( String [] args ){ 
        int v = Compute.interest(1000,8,2);System.out.println( v ); 
        double d = Compute.root(1,-5,6);   System.out.println( d ); 
        boolean b = Compute.sign(1,2,3);   System.out.println( b ); 
        b = Compute.equal(1,2,3);          System.out.println( b ); 
        b = Compute.cast(127,128,129); System.out.println( b ); 
        b = Compute.cast(128,128,128); System.out.println( b ); 
    } } 
    the output should be as follows.
    1172 
    3.0 
    false 
    true 
    true 
    false 
  2. Download the calc.java program from the given web page, add to it the ‘Compute’ class, and then compile and execute the program.

Assume ‘lab19’ for the submit command, and submit your ‘calc.java’ program.

Note: Files that fail to compile, and execute when applicable, will not be examined. They will be awarded a grade of 0 points.

Q&A