Java Basics Exercises - Java Programming Tutorial
Java Basics Exercises - Java Programming Tutorial
www3.ntu.edu.sg
1. Coding Style:
Read "Java Code Convention" (@ http://www.oracle.com
/technetwork/java/codeconvtoc-136057.html or google "Java
Code Convention").
1 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
Hints:
2 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
if ( ...... ) {
System.out.println( ...... );
} else {
System.out.println( ...... );
}
System.out.println( ...... );
}
}
3 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
System.out.println( ...... );
}
System.out.println( ...... );
}
}
Hints:
if (number == 1) {
System.out.println( ...... );
} else if ( ...... ) {
......
} else if ( ...... ) {
......
......
} else {
......
}
switch(number) {
case 1: System.out.println( ...... );
break;
4 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
Hints:
5 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
......
......
}
}
Try:
4. Modify the program to sum from 111 to 8899, and compute the
6 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
5. Modify the program to sum only the odd numbers from 1 to 100,
and compute the average. (HINTS: n is an odd number if n % 2 is
not 0.)
7. Modify the program to find the "sum of the squares" of all the
numbers from 1 to 100, i.e. 1*1 + 2*2 + 3*3 + ... + 100*100.
Try: Repeat the above, but use long to store the product.
Compare the products obtained.
7 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
Hints:
}
System.out.println("The sum from
left-to-right is: " + sumL2R);
......
8 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
......
}
}
9 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
}
}
Alternatively, you can use the term number as the loop index:
Hints:
10 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
if ( ...... ) {
System.out.print("Coza");
}
if ( ...... ) {
System.out.print(.....);
}
......
if ( ...... ) {
......
}
if ( ...... ) {
System.out.println();
} else {
System.out.print( ...... );
}
}
}
}
11 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
printed = false;
if ( ...... ) {
System.out.print( ...... );
printed = true;
}
if ( ...... ) {
System.out.print( ..... );
printed = true; !
}
......
if (!printed) {
......
}
......
}
Hints:
12 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
......
++n;
fnMinus2 = fnMinus1;
fnMinus1 = fn;
}
......
}
}
13 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
int n = .......;
while (n > 0) {
int digit = n % 10;
......
......
n = n / 10;
}
Exercises on Nested-Loop
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
Your program should use only two output statements, one EACH of
the followings:
System.out.print("# ");
System.out.println();
14 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
Hints:
Notes: The code pattern for printing 2D patterns using nested loop
is:
System.out.println();
}
# # # # # # #
# # # # # # #
15 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
# # # # # # #
# # # # # # #
# # # # # # #
# # # # # # #
# # # # # # #
Your program should use only three output statements, one EACH
of the followings:
System.out.print("# ");
System.out.print(" ");
System.out.println();
Hints:
if ((row % 2) == 0) {
......
}
for (int col = 1; ......; ......) {
......
}
......
}
}
}
16 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
* | 1 2 3 4 5 6 7 8 9
-------------------------------
1 | 1 2 3 4 5 6 7 8 9
2 | 2 4 6 8 10 12 14 16 18
3 | 3 6 9 12 15 18 21 24 27
4 | 4 8 12 16 20 24 28 32 36
5 | 5 10 15 20 25 30 35 40 45
6 | 6 12 18 24 30 36 42 48 54
7 | 7 14 21 28 35 42 49 56 63
8 | 8 16 24 32 40 48 56 64 72
9 | 9 18 27 36 45 54 63 72 81
# # # # # # # # # # # # #
# # # # #
# # # # # # # # # # # #
# # # # # #
# # # # # # # # # # #
# # # # # # #
# # # # # # # # # #
# # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # #
# # # # # # # #
# #
# # # # # # # #
17 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
# # # # # # #
# # #
# # # # # # # #
# # # # # #
# # # #
(a) (b)
(c) (d)
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # #
# # #
# # # # # # #
# # #
# # # # # # #
# # #
# # # # #
# # #
# # # # # # #
# # #
# # # # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # #
(e) (f)
(g) (h) (i)
18 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
Enter an integer: 12
Enter a floating point number: 33.44
Enter your name: Peter
19 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
Hints:
import java.util.Scanner;
public class KeyboardScanner {
public static void main(String[] args) {
int num1;
double num2;
String name;
double sum;
......
in.close();
}
}
20 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
You need to create a text file called "in.txt" (in Eclipse, right-click
on the "project" "New" "File") with the following contents:
12
33.44
Peter
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
......
21 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
in.close();
}
}
Ignore the Exception Handling codes for the time being. They will
be covered in due course.
22 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
following pattern:
int number;
......
......
Hints:
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
String inStr;
23 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
int inStrLen;
Hints:
24 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
Hints:
25 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
26 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
...+b1×21+b0×20.
import java.util.Scanner;
public class Bin2Dec {
public static void main(String[] args) {
String binStr;
int binStrLen;
int dec = 0;
......
......
if (binChar == '1') {
......
} else if (binChar != '0') {
System.out.println("error: invalid
binary string \"" + binStr + "\"");
System.exit(1);
}
}
......
}
}
binStr : 1 0 1 1 1 0 0 1
charAt(pos) : 0 1 2 3 4 5 6 7 (pos from
the left)
27 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
binStr.length() = 8
pos + order = binStr.length() - 1
Hints:
28 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
You need to transform char '0' to '9' to int 0-9; and char
'a' to 'f' (or 'A' to 'F') to int 10-15. However, you do not
need a big nested-if statement of 16 cases (or 22 considering the
upper and lower letters). Extract the individual character from the
hexadecimal string, says c. If char c is between '0' to '9', you
can get the integer offset via c-'0'. If c is between 'a' to 'f' or
'A' to 'F', the integer offset is c-'a'+10 or c-'A'+10.
String hexStr;
char hexChar;
......
hexChar = hexStr.charAt(pos);
......
29 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
Exercises on Array
30 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
Exercises on Method
Also write the main() method that prompts user for a number, and
prints "ODD" or "EVEN". You should test for negative input.
31 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
int number;
int sentinel = -1;
Hints: You can either convert the int to String and use the
String's charAt() to inspect each char; or repeatably use
n%10 and n=n/10 to extract each digit (in int).
32 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
the form of {a1, a2, ..., an}. Take note that there is no
comma after the last element. The method's signature is as follows:
Also write a test driver to test this method (you should test on empty
array, one-element array, and n-element array).
Also write a test driver to test this method (you should test on empty
array, one-element array, and n-element array).
33 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
34 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
Take note that the array passed into the method can be modified by
the method (this is called "pass by reference"). On the other hand,
primitives passed into a method cannot be modified. This is
because a clone is created and passed into the method instead of
the original copy (this is called "pass by value").
Hint: You need to use a temp location (an int) to swap the first
element with the last element, and so on.
35 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
Hint: You need to use a temp location (an int) to swap the
corresponding elements of the two arrays.
36 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
Hints:
37 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
......
}
0 - 9: ***
10 - 19: ***
20 - 29:
30 - 39:
40 - 49: *
50 - 59: *****
60 - 69:
70 - 79:
80 - 89: *
38 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
90 -100: **
*
* *
*
* *
* *
* * * *
* *
0-9 10-19 20-29 30-39 40-49 50-59 60-69 70-79
80-89 90-100
Hints:
Write the codes to print the star first. Test it. Then print the labels.
.......
for (int starNum = 0; starNum < bins[binNum];
starNum++) {
.......
}
......
}
39 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
To print the vertical histogram, you need to find the maximum bin
count (called maxBinCount) and use a nested loop:
for (int level = maxBinCount; level >= 1;
level--) {
for (int binNum = 0; binNum < bins.length;
binNum++) {
}
......
}
**** (4)
*** (3)
***** (5)
******* (7)
40 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
java Arithmetic 3 2 +
3+2=5
java Arithmetic 3 2 -
3-2=1
java Arithmetic 3 2 /
3/2=1
Hints:
41 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
args[2].length() is: 1
if (args.length != 3) {
System.err.println("Usage: java Arithmetic
int1 int2 op");
return;
}
operand1 = Integer.parseInt(args[0]);
operand2 = ......
theOperator = args[2].charAt(0);
System.out.print(args[0] + args[2] + args[1]
+ "=");
switch(theOperator) {
case ('-'): System.out.println(operand1 -
operand2); break;
case ('+'): ......
case ('*'): ......
case ('/'): ......
default:
System.err.println("Error: invalid
operator!");
}
}
}
42 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
Notes:
43 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
Exercise (JDK Source Code): Extract the source code of the class
Math from the JDK source code ("$JAVA_HOME" "src.zip"
"Math.java" under folder "java.lang"). Study how constants
such as E and PI are defined. Also study how methods such as
abs(), max(), min(), toDegree(), etc, are written.
Hints:
44 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
'__'
(©©)
/========\/
/ || %% ||
* ||----||
¥¥ ¥¥
"" ""
Hints:
Use escape sequence \uhhhh where hhhh are four hex digits to
display Unicode characters such as ¥ and ©. ¥ is 165 (00A5H)
and © is 169 (00A9H) in both ISO-8859-1 (Latin-1) and Unicode
character sets.
Try: Print the same pattern using printf(). (Hints: Need to use
%% to print a % in printf() because % is the suffix for format
specifier.)
45 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
# # # # # # # # # # #
# #
# # # # # # # # # # #
# # # #
# # # # # # # # # # #
# # # # # #
# # # # # # # # # # #
# # # # # # # #
# # # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # # #
# # # # # # # # # # # #
(a)
(b) # # # # # # # # #
# # # # # # #
# # # # #
# # #
(c)
1 1 2 3 4 5 6 7
8 1 8 7 6 5 4 3 2 1
1 2 1 2 3 4 5 6
7 2 1 7 6 5 4 3 2 1
1 2 3 1 2 3 4 5
6 3 2 1 6 5 4 3 2 1
1 2 3 4 1 2 3 4 5
46 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
4 3 2 1 5 4 3 2 1
1 2 3 4 5 1 2 3 4 5
4 3 2 1 4 3 2 1
1 2 3 4 5 6 1 2 3 6 5
4 3 2 1 3 2 1
1 2 3 4 5 6 7 1 2 7 6 5
4 3 2 1 2 1
1 2 3 4 5 6 7 8 1 8 7 6 5
4 3 2 1 1
(d) (e)
(f) (g)
1 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1
1 2 1 1 2 3 4 5 6
7 6 5 4 3 2 1
1 2 3 2 1 1 2 3 4 5
6 5 4 3 2 1
1 2 3 4 3 2 1 1 2 3 4
5 4 3 2 1
1 2 3 4 5 4 3 2 1 1 2 3
4 3 2 1
1 2 3 4 5 6 5 4 3 2 1 1 2
3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1 1
2 1
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 1
(h)
(i)
1 1 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1
1 2 2 1 1 2 3 4 5 6
7 7 6 5 4 3 2 1
1 2 3 3 2 1 1 2 3 4 5
47 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
6 6 5 4 3 2 1
1 2 3 4 4 3 2 1 1 2 3 4
5 5 4 3 2 1
1 2 3 4 5 5 4 3 2 1 1 2 3
4 4 3 2 1
1 2 3 4 5 6 6 5 4 3 2 1 1 2
3 3 2 1
1 2 3 4 5 6 7 7 6 5 4 3 2 1 1
2 2 1
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
1 1
(j)
(k)
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
6 7 8 9 0 1 0 9 8 7 6
7 8 9 0 1 2 3 2 1 0 9 8 7
8 9 0 1 2 3 4 5 4 3 2 1 0 9 8
(l)
1
1 2 1
1 2 4 2 1
48 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4
2 1
1 2 4 8 16 32 64 128 64 32 16 8
4 2 1
(a) PowerOf2Triangle
1 1
1 1 1 1
1 2 1 1 2 1
1 3 3 1 1 3 3 1
1 4 6 4 1 1 4 6 4 1
1 5 10 10 5 1 1 5 10 10 5 1
1 6 15 20 15 6 1 1 6 15 20 15
6 1
(b) PascalTriangle1 (c) PascalTriangle2
Compare the values computed using the series with the JDK
methods Math.sin(), Math.cos() at x=0, π/6, π/4, π/3, π/2
using various numbers of terms.
49 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
F(0) = 1
50 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
F(1) = 1
F(2) = 2
...
F(45) = 1836311903
F(46) is out of the range of int
Hints: The maximum and minimum values of a 32-bit int are kept in
constants Integer.MAX_VALUE and Integer.MIN_VALUE,
respectively. Try these statements:
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE + 1);
Take note that in the third statement, Java Runtime does not flag
out an overflow error, but silently wraps the number around. Hence,
you cannot use F(n-1) + F(n-2) > Integer.MAX_VALUE to
check for overflow. Instead, overflow occurs for F(n) if
(Integer.MAX_VALUE – F(n-1)) < F(n-2) (i.e., no room for
the next Fibonacci number).
The factorial of 1 is 1
The factorial of 2 is 2
...
The factorial of 10 is 3628800
51 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
The factorial of 1 is 1
The factorial of 2 is 2
...
The factorial of 12 is 479001600
The factorial of 13 is out of range
Hints: The maximum and minimum values of a 32-bit int are kept
in constants Integer.MAX_VALUE and Integer.MIN_VALUE,
respectively. Try these statements:
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE + 1);
Take note that in the third statement, Java Runtime does not flag
out an overflow error, but silently wraps the number around.
52 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
F(0) = 1
F(1) = 1
F(2) = 2
...
F(45) = 1836311903
F(46) is out of the range of int
53 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
java NumberGuess
Key in your guess:
50
Try higher
70
Try lower
65
Try lower
61
You got it in 4 trials!
54 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
Trial 3: te_t__g
Key in one character or your guess word: testing
Congratulation!
You got in 4 trials
Hints:
55 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
1. Based on the first two digit of the year, get the number from the
following "century" table.
1700- 1800- 1900- 2000- 2100- 2200- 2300- 2400-
4 2 0 6 4 2 0 6
Take note that the entries 4, 2, 0, 6 repeat.
3. Add to "the last two digit of the year divide by 4, truncate the
fractional part".
6. The sum modulus 7 gives the day of the week, where 0 for SUN, 1
for MON, ..., 6 for SAT.
56 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
57 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
System.out.println(isValidDate(2012, 2,
29));
System.out.println(isValidDate(2011, 2,
29));
System.out.println(isValidDate(2099, 12,
31));
System.out.println(isValidDate(2099, 12,
32));
System.out.println(getDayOfWeek(1982, 4,
24));
System.out.println(getDayOfWeek(2000, 1,
1));
System.out.println(getDayOfWeek(2054, 6,
19));
System.out.println(getDayOfWeek(2012, 2,
17));
System.out.println(toString(2012, 2, 14));
}
}
You can compare the day obtained with the Java's Calendar class
as follows:
58 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
System.out.println("It is " +
calendarDays[dayNumber - 1]);
Exercises on Recursion
factorial(n) = 1, for n = 0
59 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
factorial(5) = 5 * factorial(4)
factorial(4) = 4 * factorial(3)
factorial(3) = 3 * factorial(2)
factorial(2) = 2 * factorial(1)
factorial(1) = 1 * factorial(0)
factorial(0) = 1
factorial(1) = 1 * 1 = 1
factorial(2) = 2 * 1 = 2
factorial(3) = 3 * 2 = 6
factorial(4) = 4 * 6 = 24
factorial(5) = 5 * 24 = 120 (DONE)
factorial(n) = 1, if n = 0
factorial(n) = n * factorial(n-1), if n > 0
factorial(n) = 1*2*3*...*n
Hints:
60 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
if (n == 0) {
return 1;
} else {
return n * factorial(n-1);
}
r
// return (n == 0) ? 1 : n*factorial(n-1);
}
Notes:
F(0) = 0
F(1) = 1
F(n) = F(n-1) + F(n-2) for n >= 2
S(1) = 1
S(2) = 12
S(3) = 123
S(4) = 1234
......
61 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
S(9) = 123456789
S(10) = 12345678910
S(11) = 1234567891011
......
gcd(a,b) = a, if b = 0
gcd(a,b) = gcd(b, remainder(a,b)), if b > 0
JDK provides searching and sorting utilities in the Arrays class (in
package java.util), such as Arrays.sort() and
Arrays.binarySearch() - you don't have to write your
searching and sorting in your production program. These exercises
are for academic purpose and for you to gain some understandings
and practices on these algorithms.
62 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
For example, suppose that we want to search for the item 18 in the
list {11 14 16 18 20 25 28 30 34 40 45}:
63 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
If fromIdx = toIdx - 1
if key = A[fromIdx], return true
else, return false (not found)
else
middleIdx = (fromIdx + toIdx) / 2
if key = A[middleIdx], return true
else if key < A[middleIdx], toIdx = middleIdx
else firstIdx = middleIdx + 1
binarySearch(array, key, fromIdx, toIdx)
Pass 1:
9 2 4 1 5 -> 2 9 4 1 5
2 9 4 1 5 -> 2 4 9 1 5
2 4 9 1 5 -> 2 4 1 9 5
2 4 1 9 5 -> 2 4 1 5 9 (After Pass 1, the largest
item sorted on the right - bubble to the right)
Pass 2:
64 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
2 4 1 5 9 -> 2 4 1 5 9
2 4 1 5 9 -> 2 1 4 5 9
2 1 4 5 9 -> 2 1 4 5 9
2 1 4 5 9 -> 2 1 4 5 9 (After Pass 2, the 2
largest items sorted on the right)
Pass 3:
2 1 4 5 9 -> 1 2 4 5 9
1 2 4 5 9 -> 1 2 4 5 9
1 2 4 5 9 -> 1 2 4 5 9
1 2 4 5 9 -> 1 2 4 5 9 (After Pass 3, the 3
largest items sorted on the right)
Pass 4:
1 2 4 5 9 -> 1 2 4 5 9
1 2 4 5 9 -> 1 2 4 5 9
1 2 4 5 9 -> 1 2 4 5 9
1 2 4 5 9 -> 1 2 4 5 9 (After Pass 4, the 4
largest items sorted on the right)
No Swap in Pass 4. Done.
Write a method to sort an int array (in place) with the following
signature:
function bubbleSort(A)
n = length(A)
boolean swapped
do
swapped = false
for i = 1 to n-1
65 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
{} {9 6 4 1 5} -> {} {1 6 4 9 5}
{1} {6 4 9 5} -> {1} {4 6 9 5}
{1 4} {6 9 5} -> {1 4} {5 9 6}
{1 4 5} {9 6} -> {1 4 5} {6 9}
{1 4 5 6} {9} -> DONE
{1 4 5 6 9}
Write a method to sort an int array (in place) with the following
signature:
66 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
{} {9 6 4 1 5 2 7} -> {9} {6 4 1 5 2 7}
{9} {6 4 1 5 2 7} -> {6 9} {4 1 5 2 7}
{6 9} {4 1 5 2 7} -> {4 6 9} {1 5 2 7}
{4 6 9} {1 5 2 7} -> {1 4 6 9} {5 2 7}
{1 4 6 9} {5 2 7} -> {1 4 5 6 9} {2 7}
{1 4 5 6 9} {2 7} -> {1 2 4 5 6 9} {7}
{1 2 4 5 6 9} {7} -> {1 2 4 5 6 7 9} {}
{1 2 4 5 6 7 9} {} -> Done
Write a method to sort an int array (in place) with the following
signature:
2. Partitioning: reorder the list such that the samller elements come
before the pivot, and the larger elements after the pivot. After the
partitioning, the pivot is in its final position.
67 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
68 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
69 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
70 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
GCD(a, 0) = a
GCD(a, b) = GCD(b, a mod b), where (a mod b)
denotes the remainder of a divides by b.
For example,
71 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
GCD(15, 5) = GCD(5, 0) = 5
GCD(99,88) = GCD(88,11) = GCD(11,0) = 11
GCD(3456,1233) = GCD(1233,990) = GCD(990,243) =
GCD(243,18) = GCD(18,9) = GCD(9,0) = 9
GCD(a, b)
while (b != 0) {
temp ← b
b ← a mod b
a ← temp
}
GCD is a
Final Notes
72 of 73 2/10/17 2:34 AM
Java Basics Exercises - Java Programming Tutorial about:reader?url=https://www3.ntu.edu.sg/home/ehchua/progra...
73 of 73 2/10/17 2:34 AM