Exdbjava PDF
Exdbjava PDF
Exdbjava PDF
yvind Ryan
Solution: Since 13 is both larger than 10 and smaller than 15, the middle block
is executed. For the other two blocks, only one of the parts is true, so the code
isnt executed. If we use 10 instead, nothing is executed. This is because 10 < 10
is false.
2. This code block
boolean[] truthValues = {false, true};
for (boolean x : truthValues){
for (boolean y : truthValues){
System.out.printf("%b and %b is %b\n", x, y, x && y);
}
}
produces a truth table for . Modify it to make truth tables for eaxh statement.
Solution: In each case, we write the whole code. Note that the first two parts
simply have very minor changes, whereas the third part needs an additional
nested layer.
a.
Solution: can be used directly.
boolean[] truthValues = {false, true};
for (boolean x : truthValues){
for (boolean y : truthValues){
2
System.out.printf("%b or %b is %b\n", x, y, x || y);
}
}
b.
Solution: Truth table for implication, , using x y = x y:
c. z (x y)
Solution: We use that z (x y) = z x y
3. Compute the values of the smallest and the largest double as a decimal num-
ber.
Solution: Using the calculator, we find that the largest is
returns
3
1.7976931348623157E308
returns
4.9E-324
returns N aN .
5. Convert the float 124.234 f to long.
Solution:
6. The associative law for multiplication states that (ab)c = a(bc). Run this
code:
float a=0.25e-12f;
float b=4e12f;
float c=5e35f;
System.out.println((a*b)*c);
System.out.println(a*(b*c));
4
What happens? Change all the floats into doubles, and try again. What has
changed? Can you create a similar problem with doubles?
Solution: The output from running the code is
5.0E35
Infinity
Changing to doubles, we get 4.99 1035 from both computations. The problem
is that bc is too large for a float, so that infinity is returned when this compu-
tation comes first. To get the same problem for double, we need to input larger
numbers. Trial and error, and remembering the result from Exercise 3, we get
the same type of result from running
double a=0.25e-12;
double b=4e12;
double c=5e305;
System.out.println((a*b)*c);
System.out.println(a*(b*c));
7. In this exercise, we will use Java to solve quadratic equations. The formula
for solving the quadratic equation
ax 2 + bx + c = 0
is p p
b+ b 2 4ac b b 2 4ac
x0 = 2a x1 = 2a .
Choose values for a, b, c as floats, and write a programme that computes these
two solutions. Note that the square root is found in Java as Math.sqrt.
Then add a test to the programme, checking whether the numbers you have
calculated actually solves the original problem. Try out your programme on
these four sets of coefficients.
a b c
1 2 3
1 2 1
1 1 1
1 20000 1
5
float a =1.0f;
float b = 2.0f;
float c = -3.0f;
float x_0 = (float) (-b + Math.sqrt(b*b - 4*a*c))/(2*a);
float x_1 = (float) (-b - Math.sqrt(b*b - 4*a*c))/(2*a);
System.out.println(x_0);
System.out.println(x_1);
//test. This should return 0
System.out.println(a*x_0*x_0 + b*x_0+c);
System.out.println(a*x_1*x_1 + b*x_1+c);
This returns
1.0
-3.0
0.0
0.0
-1.0
-1.0
0.0
0.0
The third:
NaN
NaN
NaN
NaN
Looking at the computation, we see that we try to compute the square root of a
negative number, and this returns N aN as in Exercise 4.
The last:
20000.0
0.0
1.0
1.0
6
Here the two solutions give 1 instead of 0 when plugged into the original equa-
tion. The reason is that there are errors coming from rounding. If all the vari-
ables are changed into doubles (and we delete the casting to float), the output
will be
19999.999949999998
5.000000055588316E-5
-5.9604644775390625E-8
-8.617663249665952E-9
We see that the approximate solution 20000 is actually a bit too large, and 0 a bit
to small. Also, the test doesnt return 0, but numbers quite close to 0, which is to
be expected because of rounding errors.
8. One can use floats in loops, which is practical if we want for instance points
on a graph. Try to use a for loop of the form
for (double x = startValue, x < stopValue, x = x + increment){...}
to compute points on the graph of f (x) = sin x in the interval [7, 7]. To make
sure that the point are close enough to get a nice graph from plotting them, com-
pute the sine for each of the points 7, 6.99 and so forth.
Then try to do the same computation using an int as index in the loop.
Solution: Here is the code for writing the points to the terminal window:
for (double x = -7.0; x<7.0; x+= 0.01){
double y = Math.sin(x);
System.out.printf("Point (%f, %f)\n ", x, y);
}
To find the easiest formulation for the range of an integer variable, note that if
we multiply with a hundred, the increment is changed to +1. So we can write
for(int i = -700; i< 700; i++){
double x = ((double) i)/100;
double y = Math.sin(x);
System.out.printf("Point (%f, %f)\n ", x, y);
}
Note that we also need to compute x, and we need to make sure that i /100 is not
considered integer division.
9. In this exercise, we will compute as many powers of two that we can, using
the primitive numerical types in Java. Start by defining int power = 2, and
7
multiply repeatedly by 2. Check (how?) if the computation is correct using int,
and if it isnt, convert to long. When this also fails, convert to float. When
this fails, convert to double. Finally, write a statement about the largest powers
handled by the different types.
Can you modify the programme to work with powers of 3?
Solution: There are many ways to solve this exercise, so if your solution doesnt
resemble this one, dont worry.
int power = 1;
int powerOf2 = 2;
int oneLower = 1;
while (powerOf2 > oneLower){
System.out.println(powerOf2);
powerOf2 *= 2;
oneLower *= 2;
power ++;
}
System.out.printf("Longs from power %d\n", power);
long powerOf2L = 2*(long) oneLower;
long oneLowerL = (long) oneLower;
while (powerOf2L > oneLowerL){
System.out.println(powerOf2L);
powerOf2L *= 2;
oneLowerL *= 2;
power ++;
}
System.out.printf("Floats from power %d\n", power);
float powerOf2f = 2* (float) oneLowerL;
float oneLowerf = (float) oneLowerL;
Float infinity = new Float(Float.POSITIVE_INFINITY);
while (powerOf2f < infinity){
System.out.println(powerOf2f);
powerOf2f *= 2;
oneLowerf *= 2;
power ++;
}
System.out.printf("Floats from power %d\n", power);
double powerOf2Double = 2* (double) oneLowerf;
double infinityDouble = (double) infinity;
8
while (powerOf2Double < infinityDouble){
System.out.println(powerOf2Double);
powerOf2Double *= 2;
power ++;
}
System.out.printf("Final power is %d\n", power);
Solution: This code produces the tables. It is not the simplest construction for
the problem, and it might be better to format the output differently.
//multiplication table:
for (Float special : specialFloats){
for (Float special2 : specialFloats){
System.out.printf("%f * %f = %f\n", special, special2,
special * special2);
}
}
//addition table:
for (Float special : specialFloats){
for (Float special2 : specialFloats){
System.out.printf("%f + %f = %f\n", special, special2,
9
special + special2);
}
}
f (a) = k a mod m.
See Example ??. If you want a more challenging exercise, look only at the Exam-
ple, and produce a Java programme for these tasks. Otherwise, read the follow-
ing hints.
To initialize A, first give a value to n, define A as an array of this length, and
define A[i ] = i in a loop. Do the same for B .
Then declare the function f as an array of the same length as A, and define
f [i ] as in the pseudocode in the example.
The most involved part of this exercise is to compute the number numberOfValues
correctly. Do this!
Finally, write a statement if f is injective or surjective.
10
If you want to check your programme, use it for some of the values in the
main text.
Solution: Here is a possible programme. Note that the break statement pre-
vents repetitions to be counted; each value is counted only once.
11
}
13. Check how Java deals with division involving one negative and one positive
number.
Solution: We run the two possibilities we have (using 7/3), first with negative
numerator:
System.out.printf("-7/3 = %d\n", (-7)/3);//quotient
System.out.printf("-7 %% 3 = %d\n",(- 7) % 3); //remainder
returns
-7/3 = -2
-7 % 3 = -1
Then with negative denominator:
System.out.printf("7/-3 = %d\n", 7/(-3));//quotient
System.out.printf("7 %% -3 = %d\n", 7 % (-3)); //remainder
returns
7/-3 = -2
7 % -3 = 1
In both cases, the result of dividing yields the quotient rounded towards 0, and
the remainder is given such that
14. Check that the symbol ^ works as exclusive or, by displaying 43 ^ 3245
in binary. The number youll get is 3206.
Solution: Running
String binaryXor = String.format("%32s",
Integer.toBinaryString(43 ^ 3245)).replace( , 0);
System.out.println(binaryXor);
yields
00000000000000000000110010000110
We can check the answer; running
String binary3206 = String.format("%32s",
Integer.toBinaryString(3206)).replace( , 0);
System.out.println(binary3206);
12
yields the same result.
15. It is not allowed to use modulo 0, %0, in Java (because of division by zero).
Check how Java treats modulo 1.
Solution: We can run, for instance
for (int i = -5; i<5; i++){
System.out.println(i % 1);
}
The +256 is a trick to alleviate the problems from casting to int. The bit in the
16-place is set (the switch is turned on) if its value is 1.
Solution:
a. Explain why the bit in the 4-place is set by this code (stateByte repre-
sents the current value for the states). Print the binary strings for different
stateBytes so that you see both what happens if this bit was set and if it
wasnt set before running the code.
// stateByte is already defined
stateByte = (byte) (stateByte | 4);
13
b. Explain why the bit in the 4-place is turned off by this code. Again,
give examples both with the 4-bit set and not set before running this code
block.
c. What does exclusive or (^) with 0 do? What does exclusive or with 4
do? What does exclusive or with 1 do?
Solution: 0 has 0 in all bits. 1 0 = 1 and 0 0 = 0, so nothing happens.
4 has 0 in all places different from the four-bit, so nothing happens there.
In the four-bit, 0 1 = 1 and 1 1 = 0, so the value in this place is switched
from on to off or from off to on.
1 has 1 in all bits, so as in the previous step, we see that all bits are
changed. Thus a ^ -1 = ~a.
d. Given two integers intX and intY, what is the value of each of them
after running this code:
14
17. Find the sum of the integers underlying the chars in the English alphabet.
Solution: We need to cast each of the characters a, b, ..., z to int
and sum the results. This code performs these operations, keeping a running
total.
int sumOfChars = 0;
for (char c = a; c <= z; c++){
sumOfChars += (int) c;
}
The answer is 2847. Note that if we drop the explicit cast (int), Java casts im-
plicitly for us.
18. Find the smallest n such that
n
i2
X
i =1
A and B willl be some more or less random subsets. The initialization of what
weve discussed so far can be done as follows:
15
To check whether an element, say 3, is in the union of A and B , we can use the
code block
Modify this programme to find the intersection. Also find the complement of A.
For the more ambitious, modify the code so that fewer equalities are checked.
Use for instance break.
Solution: We answer this exercise by giving the entire programme. Note that
16
the structure for finding the boolean test variables is the same as in the text, it is
only the way they are used that is different.
//Initialization
int[] universe = {0,1,2,3,4,5,6,7,8,9,10};
int[] setA = {0,1,2,3,4,5};
int[] setB = {2,3,5,7};
//Complement of A:
for (int number : universe){
boolean testA = false;
for ( int numberA : setA){
if (number == numberA){
testA = true;
}
}
if (!testA){
System.out.printf("%d is in the complement of A\n", number);
}
}
17
20. In this exercise, we will use Java to check whether a relation is a partial order.
First, we define a set and a relation on it. This time, we use char.
char[] setA = {a,b,c,d,e,f};
char[][] relation ={{a,a},{b,b},{c,c},{d,d},
{e,e},{f,f},{a,d},{a,f},{c,d},
{c,e},{c,f},{d,f}};
To check for reflexivity, we create a pair (c, c) for each element c of A, and
checks if it is in the relation. We then count how many times this happens, and
if it happens as many times as there are elements in A, the relation is reflexive.
int reflexivePairs = 0;
for (char c : setA) {
char[] cPair = new char[2];
cPair[0] = c;
cPair[1] = c;
for (char[] pair : relation) {
if (pair[0] == cPair[0] && pair[1] == cPair[1]) {
reflexivePairs++;
}
}
}
if(reflexivePairs == setA.length) {
System.out.println("The relation is reflexive");
}
18
isAntiSymmetric = false;
}
}
}
if (isAntiSymmetric) {
System.out.println("The relation is antisymmetric");
}
The main task left to you is to check for transitivity. As for antisymmetry, the
condition holds unless we find a counter-example. We need to check, for all two
pairs (x, y) and (y, z), whether (x, z) is also in the relation.
Finally, write a statement if all three conditions are fulfilled.
Solution: We answer this exercise by giving the entire programme, with a note
to what is added to the text of the exercise.
//Initialization from the text
char[] setA = {a,b,c,d,e,f};
char[][] relation ={{a,a},{b,b},{c,c},{d,d},
{e,e},{f,f},{a,d},{a,f},{c,d},
{c,e},{c,f},{d,f}};
//reflexivity from the text
int reflexivePairs = 0;
for (char c : setA){
char[] cPair = new char[2];
cPair[0] = c;
cPair[1] = c;
for (char[] pair : relation){
if (pair[0] == cPair[0] && pair[1]==cPair[1]){
reflexivePairs++;
}
}
}
if(reflexivePairs == setA.length){
System.out.println("The relation is reflexive");
}
//reflexivity ends
19
if (newPair[0] == pair[1] && newPair[1] == pair[0] &&
pair[0]!=pair[1]){
isAntiSymmetric = false;
}
}
}
if (isAntiSymmetric){
System.out.println("The relation is antisymmetric");
}
//antiSymmetry ends
20
System.out.println("The relation is a partial order.");
}
21. We will count the number of true and false statements in a Java generated
truth table, as in Exercise 2. The expressions we will be considering have three
variables x, y, z. In the beginning of this exercise, consider the expression x
(y z).
Solution: By combining the code snippets in each part, you will get a complete
programme for this problem.
c. Iterate through the table to find the number of times true appears.
Write a statement about the number of true and false values.
Solution:
int numberOfTrue = 0;
for (boolean tableValue : truthTable){
if (tableValue){
numberOfTrue ++;
21
}
}
System.out.println("Number of true");
System.out.println(numberOfTrue);
System.out.println("Number of false");
System.out.println(8-numberOfTrue);
22