CS1103 Learning Journal Unit 1 - Exceptions, the switch statement, Analysis of Algorithms

CS1103 Learning Journal Unit 1 - Exceptions, the switch statement, Analysis of Algorithms

This week, I reviewed some topics from CS1102 and learned some new subjects from CS1103. First, I checked the usage of the switch statement. One thing to note is that the expression in the statement “switch (expression) {}” cannot be a double or float value. Second, I reviewed array creation and initialization techniques in the non-graded exercise. Third, I studied some basic programming style rules and practiced them in the programming assignments. Fourth, I learned a crucial algorithmic analysis technique (asymptotic analysis) and Big-Oh notation. Finally, I learned about Java’s exception hierarchy and two ways of handling exceptions: using try…catch…blocks to handle them or adding throws in method headers.

In the discussion forum, I elaborated on two types of exceptions in Java: Unchecked exceptions and checked exceptions. Then I explained the roles responsible for handling them: The computer (JVM) is responsible for handling exceptions derived from the class Error. The computer program handles exceptions derived from the class Exception. After posting my work, I read my classmates’ assignments in detail. One of the main points of these works is that the computer program is responsible for handling exceptions. Admittedly, the programmer can handle exceptions derived from the class Exception using try…catch… blocks or throwing them in method headers. However, programmers should not handle exceptions derived from the class Error because they are usually fatal and beyond control. Instead, it’s better to let the computer (JVM) handle it, and then fix the program according to the error message generated by the JVM.

When I evaluate my colleague’s work, I politely give detailed and specific comments. But it’s not easy to get others to accept these comments, primarily when pointing out deficiencies in their work. So I’m careful every time I give feedback and assess assignments. At the same time, I have been thinking, is there any way to point out the work’s flaws without hurting others’ feelings? I got some inspiration from the professor’s comments in the forum. The professor guides students to think by asking questions, which reminds me that Plato also used this method to lead his students to think critically and acquire knowledge. Instead of giving answers directly, this approach leads students to explore critically and find the answers independently. As we all know, asking questions is more acceptable than pointing out mistakes. So when I evaluate my peers’ work next time, I will also try to ask more questions rather than pointing out mistakes.

Most of the comments I received this week were encouraging words. I am very grateful for that. Also, there are some questions in the comments to help me understand better. For example, Professor Perez’s question deepened my understanding of how the JVM handles exceptions derived from the Error class; classmate Sabiha’s question made me want to confirm whether the JVM or the compiler is responsible for handling exceptions such as StackOverflowError. I appreciate all the feedback, and these comments are invaluable to me.

Regarding the programming assignments and self-quiz, they are well-designed to help review and consolidate knowledge. For instance, in the no-graded exercise, the prompt requires the program to continue until the user inputs “no,” The programmer handles the exceptions simultaneously. In the beginning, my code looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class SolveEquations {
...
public static void main(String[] args) {
while (true) {
try (Scanner in = new Scanner(System.in)) {
// the codes getting the input from the user
...
}

// the codes handing the inputs
//...

try (Scanner in = new Scanner(System.in)) {
// the codes getting the input from the user
...
}
}
}
}

I use two try-with-resources to catch the exception generated by the statement “Scanner in = new Scanner(System.in)”. However, the program reports a NoSuchElementException at the second place where the Scanner appears.

I researched and found that “the code will automatically close the Scanner when a try-with-resources block is finished executing. But once one closes a Scanner associated with System.in, then System.in stays closed. Hence, one will get a NoSuchElementException if he/she tries to read anything else, even if he/she uses a new Scanner” (Stack Overflow, 2017).

A proper way to solve this problem is to avoid using several try-with-resources blocks. Instead, enclose all the codes in one try-with-resources block to ensure continuing user inputs before closing the System.in. The correct codes are as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.Scanner;

public class SolveEquations {
...
public static void main(String[] args) {
try (Scanner in = new Scanner(System.in)) {
while (true) {
// the codes getting the inputs from the user
...
// the codes handing the inputs
...
// the codes getting the inputs from the user
...
}
}
}

Word count: 762

References

1.Eck, D. J. (2019). Introduction to Programming Using Java.
http://math.hws.edu/javanotes

2.NoSuchElementException with Scanner in try/catch block. (2017). Stack Overflow.
https://stackoverflow.com/questions/47803693/nosuchelementexception-with-scanner-in-try-catch-block

​


This is the ending, thanks for reading.