Professional Documents
Culture Documents
HenrikSamuelsson / Introduction_to_Java_Programming
1 contributor
Math.sqrt(4) // 2.0
(b)
(c)
(d)
Math.pow(2, 2) // 4.0
(e)
Math.log(Math.E) // 1.0
(f)
Math.exp(1) // 2.718281828459045
(g)
(h)
Math.rint(-2.5) // -2.0
https://github.com/HenrikSamuelsson/Introduction_to_Java_Programming/blob/master/Chapter_04/Checkpoints_Ch_04.md Page 1 of 12
Introduction_to_Java_Programming/Checkpoints_Ch_04.md at master · HenrikSamuelsson/Introduction_to_Java_Programming 8/30/18, 7)39 AM
(i)
Math.ceil(-2.5) // -2.0
(j)
Math.floor(-2.5) // -3.0
(k)
Math.round(-2.5f) // -2
(l)
Math.round(-2.5) // -2
(m)
Math.rint(2.5) // 2.0
(n)
Math.ceil(2.5) // 3.0
(o)
Math.floor(2.5) // 2.0
(p)
Math.round(2.5f) // 3
(q)
Math.round(2.5) // 3
(r)
Math.round(Math.abs(-2.5)) // 3
##4.3##
https://github.com/HenrikSamuelsson/Introduction_to_Java_Programming/blob/master/Chapter_04/Checkpoints_Ch_04.md Page 2 of 12
Introduction_to_Java_Programming/Checkpoints_Ch_04.md at master · HenrikSamuelsson/Introduction_to_Java_Programming 8/30/18, 7)39 AM
##4.4##
##4.5## (a)
(b)
(c)
##4.6## There is one package that is automatically imported into any Java program, this package is called java.lang.
One of the classes that belongs to the java.lang package is the Math class. This means that the Math methods can be
used directly without the need for explicit import.
##4.7##
##4.8##
System.out.println((int)'1'); // prints 49
System.out.println((int)'A'); // prints 65
System.out.println((int)'B'); // prints 66
System.out.println((int)'a'); // prints 97
System.out.println((int)'b'); // prints 98
System.out.println((char)40); // prints (
System.out.println((char)59); // prints ;
https://github.com/HenrikSamuelsson/Introduction_to_Java_Programming/blob/master/Chapter_04/Checkpoints_Ch_04.md Page 3 of 12
Introduction_to_Java_Programming/Checkpoints_Ch_04.md at master · HenrikSamuelsson/Introduction_to_Java_Programming 8/30/18, 7)39 AM
System.out.println((char)79); // prints O
System.out.println((char)85); // prints U
System.out.println((char)90); // prints Z
System.out.println((char)0x40); // prints @
System.out.println((char)0x5A); // prints Z
System.out.println((char)0x71); // prints q
System.out.println((char)0x72); // prints r
System.out.println((char)0x7A); // prints z
'l'
'\u3fFa'
'\b'
'\t'
Example of incorrect character literal, because the Unicode representation is too long:
'\u345dE'
##4.10##
System.out.println('\\'); // prints \
System.out.println('"'); // prints "
##4.11##
int i = '1';
equals
int i = 49;
because the ASCII code for 1 is 49, i will hence become 49.
equals
int k = 'a';
https://github.com/HenrikSamuelsson/Introduction_to_Java_Programming/blob/master/Chapter_04/Checkpoints_Ch_04.md Page 4 of 12
Introduction_to_Java_Programming/Checkpoints_Ch_04.md at master · HenrikSamuelsson/Introduction_to_Java_Programming 8/30/18, 7)39 AM
equals
int k = 97;
because the ASCII code for a is 97, k will hence become 97.
char c = 90;
equals
char c = 'Z';
because the 90 equals Z in the ASCII table, c will hence hold the value Z.
// i will be set to 65
char c = 'A';
int i = (int)c;
##4.13##
// prints b
System.out.println(++x);
https://github.com/HenrikSamuelsson/Introduction_to_Java_Programming/blob/master/Chapter_04/Checkpoints_Ch_04.md Page 5 of 12
Introduction_to_Java_Programming/Checkpoints_Ch_04.md at master · HenrikSamuelsson/Introduction_to_Java_Programming 8/30/18, 7)39 AM
##4.14##
##4.15##
##4.16##
Assuming the above lines will cause the following expressions to become the value indicated in the comments.
s1 == s2 // false
s2 == s3 // false
s1.equals(s2) // false
s1.equals(s3) // true
s2.compareTo(s2) // 0
s1.charAt(0) // W
s1.indexOf('j') // -1
https://github.com/HenrikSamuelsson/Introduction_to_Java_Programming/blob/master/Chapter_04/Checkpoints_Ch_04.md Page 6 of 12
Introduction_to_Java_Programming/Checkpoints_Ch_04.md at master · HenrikSamuelsson/Introduction_to_Java_Programming 8/30/18, 7)39 AM
s1.indexOf("to") // 8
s1.lastIndexOf('a') // 14
s1.lastIndexOf("o", 15) // 9
s1.length() // 15
s1.substring(5) // me to Java
s1.substring(5, 11) // me to
s1.startsWith("Wel") // true
s1.endsWith("Java") // true
s1.contains(s2) // false
String s3 = s1 - s2;
https://github.com/HenrikSamuelsson/Introduction_to_Java_Programming/blob/master/Chapter_04/Checkpoints_Ch_04.md Page 7 of 12
Introduction_to_Java_Programming/Checkpoints_Ch_04.md at master · HenrikSamuelsson/Introduction_to_Java_Programming 8/30/18, 7)39 AM
s1 >= s2
char c = s1(0);
char c = s1.charAt(s1.length());
##4.18##
a) 11
b) 50
c) 111
d) 12
e) 51
##4.19##
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
}
1Welcome 11
1Welcome 2
1Welcome 2
1Welcome a1
##4.20## (a)
https://github.com/HenrikSamuelsson/Introduction_to_Java_Programming/blob/master/Chapter_04/Checkpoints_Ch_04.md Page 8 of 12
Introduction_to_Java_Programming/Checkpoints_Ch_04.md at master · HenrikSamuelsson/Introduction_to_Java_Programming 8/30/18, 7)39 AM
(b)
(c)
int x = s1.compareTo(s2);
(d)
int x = s1.compareToIgnoreCase(s2);
(e)
boolean b = s1.startsWith("AAA");
(f)
boolean b = s1.endsWith("AAA");
(g)
int x = s1.length();
(h)
char x = s1.charAt(0);
(i)
String s3 = s1 + s2;
(j)
String s3 = s1.substring(1);
(k)
(l)
https://github.com/HenrikSamuelsson/Introduction_to_Java_Programming/blob/master/Chapter_04/Checkpoints_Ch_04.md Page 9 of 12
Introduction_to_Java_Programming/Checkpoints_Ch_04.md at master · HenrikSamuelsson/Introduction_to_Java_Programming 8/30/18, 7)39 AM
String s3 = s1.toLowerCase();
(m)
String s3 = s1.toUpperCase();
(n)
String s3 = s1.trim();
(o)
int x = s1.indexOf('e');
(p)
int x = s1.lastIndexOf("abc");
##4.21## We will get the answer by adding the first numbers in Set1, Set3, and Set4:
1 + 4 + 8 = 13
##4.23## (a)
Erroneous code because there are three items to be printed but only two format specifiers.
(b)
Erroneous code because there are two format specifiers but only one item to be printed.
(c)
https://github.com/HenrikSamuelsson/Introduction_to_Java_Programming/blob/master/Chapter_04/Checkpoints_Ch_04.md Page 10 of 12
Introduction_to_Java_Programming/Checkpoints_Ch_04.md at master · HenrikSamuelsson/Introduction_to_Java_Programming 8/30/18, 7)39 AM
Erroneous code because an int item is combined with a float format specifier.
(d)
Erroneous because second format specifier tries to set the width to zero and this is not allowed.
(e)
System.out.printf("%08s\n", "Java");
##4.24##
System.out.println("(b)");
System.out.printf("amount is %5.2f%% %5.4e\n", 32.327, 32.32);
System.out.println();
System.out.println("(c)");
System.out.printf("%6b\n", (1 > 2));
System.out.println();
System.out.println("(d)");
System.out.printf("%6s\n", "Java");
System.out.println();
System.out.println("(e)");
System.out.printf("%-6b%s\n", (1 > 2), "Java");
System.out.println();
System.out.println("(f)");
System.out.printf("%6b%-8s\n", (1 > 2), "Java");
System.out.println();
System.out.println("(g)");
System.out.printf("%,5d %,6.1f\n", 312342, 315562.932);
System.out.println();
System.out.println("(h)");
System.out.printf("%05d %06.1f\n", 32, 32.32);
}
Above program will print the following (note that it was run on computer with Swedish locale settings for displaying
numbers, might differ a bit on machines in other countries).
https://github.com/HenrikSamuelsson/Introduction_to_Java_Programming/blob/master/Chapter_04/Checkpoints_Ch_04.md Page 11 of 12
Introduction_to_Java_Programming/Checkpoints_Ch_04.md at master · HenrikSamuelsson/Introduction_to_Java_Programming 8/30/18, 7)39 AM
(a)
amount is 32,320000 3,232000e+01
(b)
amount is 32,33% 3,2320e+01
(c)
false
(d)
Java
(e)
false Java
(f)
falseJava
(g)
312 342 315 562,9
(h)
00032 0032,3
https://github.com/HenrikSamuelsson/Introduction_to_Java_Programming/blob/master/Chapter_04/Checkpoints_Ch_04.md Page 12 of 12