Wednesday, 13 September 2023

Programming Errors in Java language

             Lesson 8:            

Programming errors can be categorized into three types (3)

1. SYNTAX ERROR

2. RUNTIME ERROR

3. LOGIC ERRORS.

1. Syntax Errors

Errors that are detected by the compiler are called syntax errors or compile errors. Syntax errors result from errors in code construction, such as mistyping a keyword, omitting some necessary punctuation, or using an opening brace without a corresponding closing brace. These errors are usually easy to detect because the compiler tells you where they are and what caused them. For example, in listing 1.4 as shown in Figure 1.10 below.

Four errors are reported, but the program actually has two errors:

■ The keyword void is missing before main in line 2.

■ The string Welcome to Java should be closed with a closing quotation mark in line 3.

Since a single error will often display many lines of compile errors, it is a good practice to fix errors from the top line and work downward. Fixing errors that occur earlier in the program may also fix additional errors that occur later.


Tip

If you don’t know how to correct it, compare your program closely, character by character, with similar examples in the text. In the first few weeks of this course, you will probably spend a lot of time fixing syntax errors. Soon you will be familiar with Java syntax and can quickly fix syntax errors.


2. Runtime Errors


Runtime errors are errors that cause a program to terminate abnormally. They occur while a program is running if the environment detects an operation that is impossible to carry out. Input mistakes typically cause runtime errors. An input error occurs when the program is waiting for the user to enter a value, but the user enters a value that the program cannot handle.

For instance, if the program expects to read in a number, but instead the user enters a string, this causes data-type errors to occur in the program.

Another example of runtime errors is division by zero. This happens when the divisor is zero for integer divisions. For instance, the program in Listing 1.5 would cause a runtime error, as shown in Figure 1.11.



3. Logic Errors

Logic errors occur when a program does not perform the way it was intended to. Errors of this kind occur for many different reasons.
For example, suppose you wrote the program in Listing 1.6 to convert Celsius 35 degrees to a Fahrenheit degree:


You will get Fahrenheit 67 degrees, which is wrong. It should be 95.0. In Java, the division
for integers is the quotient—the fractional part is truncated—so in Java 9 / 5 is 1. To get the correct result, you need to use 9.0 / 5, which results in 1.8. In general, syntax errors are easy to find and easy to correct because the compiler gives indications as to where the errors came from and why they are wrong. Runtime errors are not difficult to find, either, since the reasons and locations for the errors are displayed on the console when the program aborts. Finding logic errors, on the other hand, can be very challenging. In the upcoming chapters, you will learn the techniques of tracing programs and finding logic errors.

1.10.4 Common Errors

Missing a closing brace, missing a semicolon, missing quotation marks for strings, and misspelling names are common errors for new programmers.

Common Error 1: Missing Braces

The braces are used to denote a block in the program. Each opening brace must be matched by a closing brace. A common error is missing the closing brace. To avoid this error, type a closing brace whenever an opening brace is typed, as shown in the following example.



Common Error 2: Missing Semicolons

Each statement ends with a statement terminator (;). Often, a new programmer forgets to place a statement terminator for the last statement in a block, as shown in the following example.


Common Error 3: Missing Quotation Marks

A string must be placed inside the quotation marks. Often, a new programmer forgets to place
a quotation mark at the end of a string, as shown in the following example.

If you use an IDE such as NetBeans and Eclipse, the IDE automatically inserts a closing
quotation mark for each opening quotation mark typed.

Common Error 4: Misspelling Names

Java is case sensitive. Misspelling names is a common error for new programmers.
For example, the word main is misspelled as Main and String is misspelled as string in the following
code.

CHECK-POINT
1.42 What are syntax errors (compile errors), runtime errors, and logic errors?
1.43 Give examples of syntax errors, runtime errors, and logic errors.
1.44 If you forget to put a closing quotation mark on a string, what kind error will be
raised?
1.45 If your program needs to read integers, but the user entered strings, an error would
occur when running this program. What kind of error is this?
1.46 Suppose you write a program for computing the perimeter of a rectangle and you
mistakenly write your program so that it computes the area of a rectangle. What kind
of error is this?

_____________________________________________________
   ~o~ End of the lesson ~o~
_____________________________________________________

Friday, 1 September 2023

Lesson 7: Creating, Compiling, and Executing the program

Note: Before you start to code you need to download the API or JDK (Java Development tool kit) that  you can download on the internet. You may click the link below.

Java Development tool kit download link👈👈👀


CREATING

_________________________________________________________________

1. public class Welcome 

2. {

3.   public static void main(String[] args)

4.    {

5.   System.out.println("Welcome to Java!");

6.    }

7. }

__________________________________________________________________


COMPILING




EXECUTING THE PROGRAM
1st. You need to go to the folder where you save your Java program code.


Executing using CMD prompt or command prompt

1st Step

Step 2

3rd Step
4th step

CHECK-POINT

1.28 What is a keyword? List some Java keywords.

1.29 Is Java case sensitive? What is the case for Java keywords?

1.30 What is a comment? Is the comment ignored by the compiler? How do you denote a

comment line and a comment paragraph?

1.31 What is the statement to display a string on the console?

1.32 Show the output of the following code:

Note:

1. Please write your answers on a piece/s of bond paper

2. Please observe a margin.

3. No erasures; and

3. Write legibly.

_____________________________________________________

   ~o~ End of the lesson ~o~
_____________________________________________________
Thank you God bless!