# Q1304 – Introduction to Java Programming Homework Session 3

1) Type casting

1. Declare the following new variables: b (byte), i (int) and f (double).
Assign the value 120 to b and print it out.
Assign the value of b to i and print it out.
Assign the value of i to f and print it out.
What do you notice?

2. Opposite direction:
Assign the value 150.257 to f.
Assign the value of f to i and print it out.
Assign the value of i to b and print it out.
Why can’t you compile the class? Fix the problem (keyword ‘casting’).

2) Arithmetics

1. Given the following variable initializations:

int m = 13;
int n = 25;
int b = 3 * n++;
int a = 5 * ++m;

What values do the variables m, n, b and a have after the above steps? What is the explanation?

3) Arrays

1. Declare and define an array that contains the names of all days of the week. Do the declaration, definition and value assignments separately. Afterwards, print out the complete contents of the array to the command line using a single System.out.println statement. Every value should be separated using a comma. In addition, print out the length of the array.

2. Same as 3a, but initialize the array in a single step.

4) Control Structures

1. Using a for loop, add the numbers 1 to 100 to a new array. After the for loop is done, print out all the values of the array in the following manner:

o        Use a while loop.

o        Print out ten numbers in one single line delimited by a space character. Then start a new line for the next 10 numbers.

1. Modify 4a by using a switch statement in the following way:
After each block of 10 numbers, print out the sentence “These were the numbers x to y”. Substitute x and y with the appropriate values.

5) Coordinate class

1. Create a new class “Coordinate”. This class should contain the attributes x and y which describe it’s position in a coordinate system. Equip this class with the following methods:
• Constructor with x and y input parameters
• Constructor which takes an existing Coordinate object to instantiate a new one.
• "moveTo(int x, int y)" – for moving the coordinate to a new position.
• "moveTo(Coordinate coord) – also for moving the coordinate using an existing coordinate.
• "moveBy(int x, int y)" – for moving the coordinate by a vector
• "moveBy(Coordinate coord)" – like previous method, but different input parameter
• "distanceTo(int x, int y)" – calculates the distance between this and another coordinate x,y. Returns the distance.
• "distanceTo(Coordinate coord)" – like previous method, but different input parameter
• "getX()" und "getY()" – return the x and y position of the coordinate.
• "setX()" und "setY()" – set the x and y position of the coordinate.
• "print()" – print out the coordinates in the following form: [x,y].
2. Write a test program that uses instances of the class “Coordinate” and tests all of it’s methods.

Note: Use the following formula to calculate the distance between two coordinates, where (x1,y1) and (x2,y2) are two coordinates:

`        xd = x2-x1;`
`        yd = y2-y1;`
`        double distance = Math.sqrt(xd*xd + yd*yd)`