Archive for the ‘Java’ Category
Java – Exception
Posted December 31, 2015
on:Here is a summary from the Tutorials Point website.
Three Types of Exceptions: (Checked exceptions, Unchecked exceptions, Errors)
If you try to compile the below program, what will you get?
Try compile the below codes. Do you get any exception?
Try execute the codes. What do you get?
Here is the hierarchy of exception classes in Java:
Here is the list of commonly used/encountered Java Unchecked (Runtime) exceptions:
Here is the one for Checked exceptions:
Try compile and execute the below codes. What is the output?
Try the below codes. What is the output?
Study the below codes:
Source: http://www.tutorialspoint.com/java/java_exceptions.htm
Java – Files and I/O
Posted December 5, 2015
on:Java provides rich library for Input/Output (I/O) related to files and networks. We will cover those commonly used one.
Source: http://www.tutorialspoint.com/java/java_files_io.htm
Java – Date
Posted December 3, 2015
on:This website has a good explanation of the date class: http://www.tutorialspoint.com/java/java_date_time.htm.
Let’s recap those important one here.
You will get:
You will get if no input parameter is supplied (study line 9 carefully):
Here is another example for comparing dates:
Exercise: try to modify the above codes to use ” int compareTo(Date date)” method instead of “boolean before(Date date)” and “boolean after(Date date)”.
Try to read and understand the below codes. How long in second that the thread sleep?
Java – Constructor
Posted December 2, 2015
on:What is the output for the above codes?
Let’s try the below program. What is the output?
Java – Methods
Posted November 30, 2015
on:1. Method returning value:
What is the result for the above codes?
2. Method without returning value:
What is the result for the above codes?
The next topic we are going to learn is way parameters is being passed. For Java, parameter is passed by value.
In some other programming language, parameter can be passed by reference. Try to think it from a
1. What is the value of a and b after swapping?
2. What is the name of dogA and dogB at the end of the program? And Java passes by ??
In changeName() method, the code modifies the name field of the object.
In changeDog() method, the method reference points to a new object.
3. What is the value of array after swapping?
Try to add in the missing codes in the below program.
What is the output of the above codes?
Java – Loop Control
Posted November 26, 2015
on:Loop Control Statements:
What do you think is the output of the above two programs?
Source: http://www.tutorialspoint.com/java/java_loop_control.htm
Java – Arrays
Posted November 25, 2015
on:Let’s try this exercise. Try amend the codes to get 1) the total of values and 2) the largest value in the array.
Now, try to add this line to the end of the code and try running the program. What will you get?
Yes, ArrayIndexOutOfBounds errors occur whenever you exceed the maximum index in an array. Remember, array indices are 0-based.
Two dimensional Arrays:
Two dimensional arrays are declared, allocated and initialized much like one dimensional arrays. However we have to specify two dimensions rather than one, and we typically use two nested for loops to fill the array.
Try below codes.
This is the picture representing my2DimList:
Oh ya, I almost forget this.
For example:
Try to compile and run this program as below. What is the output for each of the command below?
java MainArray
java MainArray Jan
java MainArray Nov 2005
java MainArray 2015
Reference:
Here is a summary from http://www.tutorialspoint.com/java/switch_statement_in_java.htm
Now, look at the below switch codes. What do you the output will be?
No. It is NOT “You passed. Your grade is D”. The output is:
Why? Because there is a missing “break;” after line 16 in the code. The statements following that ‘D’ case will execute until a break statement is reached, which is at line 19.
Java – Basic Operations
Posted November 23, 2015
on:This website has a comprehensive list of Java operations: http://www.tutorialspoint.com/java/java_basic_operators.htm.
Here are some of the frequently used one.
Let’s do some quick exercise.
What do you think is the output of the above codes?
Let’s try this code. Can you first guess the output?
Now, can you add a String variable called color, and assign the value of “red” if (a is less than 5 and b is more than 20), and “blue” if (a is greater or equal to 10 or b is equal to 30). Add this line of code in line 8 and line 11. You then add a System.out.println code to display the value of color.
What is the output?
This may seem a little hard for you now, but we will revisit this again.
Java – Modifier Types
Posted November 22, 2015
on:Another thing to learn about Java programming language is “Modifier Types”. This website: http://www.tutorialspoint.com/java/java_modifier_types.htm has a very good coverage of it.
Let’s try some quiz on the Java Control Access Modifier.
Quiz 1:
Note: use below command to compile and classes in package
For the codes in the main method in AnotherClass class, which lines are valid and which lines are not?
Valid:
Not valid:
Quiz 2:
What is the output of the above codes?
Output:
Quiz 3:
Try to run the above codes. What did you get? How to fix it?
.
Here are details of final, abstract and synchronized modifiers. It might look a bit hard to comprehend now, but we will come to them again in future.
Final:
Abstract:
Synchronized: