Chapter 14
Primitive Operations and Expressions
14.1 Background
- Data types consist of values and operations.
- Values provide means to access data, and operations provide the means to process data.
- In the case of class data types, the values are represented by objects and the operations by methods.
- In the case of primitive data types, the values are represented by literals and the operations employ special
notations.
- In appearances, the values and operations of the primitive data types are distinguished from those of the class data
types. In nature, they are quite similar.
- The operators of the primitive data types correspond to the non-void methods of classes, and the operands
correspond to the arguments.
- Like non-void methods, the operators perform some task and return a value.
- The ‘String’ class data type is distinguished in having literals and a primitive operation, namely ‘+’, built-in for it.
14.2 Assignment Operations
14.3 Type Conversion
- Through overloading
double a;
a = 10;
- By casting
double a;
a = (double) 15;
- Overloading is governed by an upward promotion criteria for the right operand toward the type of the left
operand.
- Between integers: byte, short, int, long
- Between decimals: float, double
- Integers to decimals: long, float
- Characters to integers: char, int
class prg {
public static void main ( String[] args ){
char c = 'X';
int i = c;
System.out.println( i ); // 88
} }
- Overloading also follows a degrading criteria for the cases that the right operands are int and double
literals.
class prg {
public static void main ( String[] args ){
char c = 88;
System.out.println( c ); // 'X'
} }
- Downward casting may cause loss of content.
class prg {
public static void main ( String[] args ){
int i = 1100;
byte b = (byte) i;
System.out.println( b ); // 76
} }
14.4 Arithmetic Operators
14.5 Comparison Operations
- ‘>’, ‘>=’, ‘<’, ‘<=’, ‘==’, ‘!=’
14.6 Boolean Operations
14.7 Short Cuts
‘+=’, ‘-=’, ‘*=’, ‘/=’, ‘%=’, ‘++’, ‘--’
14.8 Initialization of Parameters and Returned Values
- Obey conversions rules similar to those that apply to the assignment operator ‘=’.
Exercises
14.9 Assignment #18: Primitive Data Types
Due: We, May 28, midnight
- 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
- 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.
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.
- 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
- 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);
}
- A call to ‘interest(amount, rate, years)’ should return the value of
- A call to ‘root(a,b,c)’ should return the root
of the
equation ax2 + bx + c.
- A call to ‘sign(a,b,c)’ should return a true value if and only if exactly two arguments have positive
values.
- A call to ‘equal(a,b,c)’ should check whether the sum of any two arguments can equal the third
argument.
- A call to ‘cast(a,b,c)’ should check whether any of the arguments can be stored in a variable of type
‘byte’.
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
- 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