Life long sharing . . .

Archive for the ‘Java’ Category

Here is a summary from the Tutorials Point website.

Lesson13-note1

Three Types of Exceptions: (Checked exceptions, Unchecked exceptions, Errors)

Lesson13-note2

If you try to compile the below program, what will you get?

Lesson13-code1

Lesson13-note3

Try compile the below codes. Do you get any exception?

Lesson13-code3

Try execute the codes. What do you get?

Lesson13-note4

Here is the hierarchy of exception classes in Java:

Lesson13-note5

Here is the list of commonly used/encountered Java Unchecked (Runtime) exceptions:

Lesson13-list1

Here is the one for Checked exceptions:

Lesson13-list2

Lesson13-note6

Lesson13-note7

Try compile and execute the below codes. What is the output?

Lesson13-code6

Lesson13-note9

Try the below codes. What is the output?

Lesson13-code7

Lesson13-note11

Study the below codes:

Lesson13-code8

Lesson13-note12

Lesson13-note8

 

Source: http://www.tutorialspoint.com/java/java_exceptions.htm

Java provides rich library for Input/Output (I/O) related to files and networks. We will cover those commonly used one.

Lesson12-note1

Lesson12-note2

Lesson12-code1

Lesson12-note3

Lesson12-code2

Lesson12-note4

Lesson12-code3

Lesson12-note5

Lesson12-code6

Lesson12-note6

Lesson12-code4

Lesson12-note7

Lesson12-note8

Lesson12-code5

Source: http://www.tutorialspoint.com/java/java_files_io.htm

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.

Lesson9-note1

Lesson9-note2

Lesson9-note3

Lesson9-code1

You will get:

Lesson9-code2

Lesson9-note4

Lesson9-code3

You will get if no input parameter is supplied (study line 9 carefully):

Lesson9-code4

Here is another example for comparing dates:

Lesson9-code5

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)”.

Lesson9-note5

Lesson9-note6

Try to read and understand the below codes.  How long in second that the thread sleep?

Lesson9-code6

 

 

Lesson11-note1

Lesson11-code1

What is the output for the above codes?

Lesson11-note2

Let’s try the below program. What is the output?

Lesson11-code2

Source: http://www.tutorialspoint.com/java/java_methods.htm

method1

1. Method returning value:

Lesson10-code1

What is the result for the above codes?

2. Method without returning value:

Lesson10-code2

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.

Lesson10-note2

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?

Lesson10-code3

2. What is the name of dogA and dogB at the end of the program? And Java passes by ??

Lesson10-code5

Lesson10-note3

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?

Lesson10-code4

Lesson10-note6

Try to add in the missing codes in the below program.

Lesson10-code7

Lesson10-note4

Lesson10-code6

What is the output of the above codes?

Lesson10-note5

Source: http://www.tutorialspoint.com/java/java_methods.htm

Lesson7-note1

Lesson7-note2

Lesson7-note4

Loop Control Statements:

Lesson7-note3

What do you think is the output of the above two programs?

 

 

 

Source: http://www.tutorialspoint.com/java/java_loop_control.htm

Lesson8-note1

Lesson8-note2

Lesson8-note3

Let’s try this exercise. Try amend the codes to get 1) the total of values and 2) the largest value in the array.

Lesson8-code1

Now, try to add this line to the end of the code and try running the program. What will you get?

Lesson8-code2

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.

Lesson8-code3

This is the picture representing my2DimList:

Lesson8-code4

Oh ya, I almost forget this.

Lesson8-note4.png

For example:

Lesson8-code5

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:

http://www.tutorialspoint.com/java/java_arrays.htm

http://www.freejavaguide.com/arrays.htm

Here is a summary from http://www.tutorialspoint.com/java/switch_statement_in_java.htm

Lesson6-note1

Now, look at the below switch codes. What do you the output will be?

Lesson6-code1

No. It is NOT “You passed. Your grade is D”. The output is:

Lesson6-output1

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.

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.

Lesson5-note1

Lesson5-note2

Let’s do some quick exercise.

Lesson5-code1

What do you think is the output of the above codes?

Lesson5-note3

Let’s try this code. Can you first guess the output?

Lesson5-code2

Lesson5-note4

Lesson5-note6

Try to understand this code:
Lesson5-code3

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?

Lesson5-note7

This may seem a little hard for you now, but we will revisit this again.

Lesson5-code4

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.

Lesson4-note1

Let’s try some quiz on the Java Control Access Modifier.

Quiz 1:

Lesson4-code1

Note: use below command to compile and classes in package

compile

For the codes in the main method in AnotherClass class, which lines are valid and which lines are not?

Valid:

Not valid:

Quiz 2:

Lesson4-code2

What is the output of the above codes?

Output:

Quiz 3:

Lesson4-code3

Try to run the above codes. What did you get? How to fix it?

.

Lesson4-note2

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:

Lesson4-note3

Abstract:

Lesson4-note4

Synchronized:

Lesson4-note5

 


Archives

May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 191 other subscribers