Assignment:
Its all about Java
Tuesday, 25 June 2024
ARRAY (3rd Chapter - Week 2)
Wednesday, 11 October 2023
Lesson 2.1.2 Writing a simple Program
2ND QUARTER - CHAPTER II
(Elementary Programming)
2.1 Introduction
The focus of this chapter is on learning elementary programming techniques to solve problems.
In Chapter 1 you learned how to create, compile, and run very basic Java programs. Now you will learn how to solve problems by writing programs. Through these problems, you will learn elementary programming using primitive data types, variables, constants, operators, expressions, and input and output.
Suppose, for example, that you need to take out a student loan. Given the loan amount, loan term, and annual interest rate, can you write a program to compute the monthly payment and total payment? This chapter shows you how to write programs like this. Along the way, you learn the basic steps that go into analyzing a problem, designing a solution, and implementing the solution by creating a program.
2.2 Writing a simple program
Writing a program involves designing a strategy for solving the problem and then using a programming language to implement that strategy.
Let’s first consider the simple problem of computing the area of a circle. How do we write a program for solving this problem? Writing a program involves designing algorithms and translating algorithms into programming instructions, or code.
An algorithm describes how a problem is solved by listing the actions that need to be taken and the order of their execution. Algorithms can help the programmer plan a program before writing it in a programming language. Algorithms can be described in natural languages or in pseudocode (natural language mixed with some programming code). The algorithm for calculating the area of a circle can be described as follows in figure 1.1
1. public class ComputeArea {
2. public static void main(String[] args) {
3. // Step 1: Read in radius
4. // Step 2: Compute area
5. // Step 3: Display the area
6. }
7. }
Note:
Two important issues that the program needs to read the radius entered by the user from the keyboard.
1. Reading the radius
2. Storing the radius in the program
Let’s address the second issue first. In order to store the radius, the program needs to declare a symbol called a variable. A variable represents a value stored in the computer’s memory. Rather than using x and y as variable names, choose descriptive names: in this case, radius for radius, and area for area. To let the compiler know what radius and area are, specify their data types. That is the kind of data stored in a variable, whether integer, real number, or something else. This is known as declaring variables. Java provides simple data types for representing integers, real numbers, characters, and Boolean types. These types are known as primitive data types or fundamental types.
Real numbers (i.e., numbers with a decimal point) are represented using a method known as floating-point in computers. So, the real numbers are also called floating-point numbers. In Java, you can use the keyword double to declare a floating-point variable. Declare radius and area as double. The program can be expanded as follows:
Figure 1.2
1. public class ComputeArea {
2. public static void main(String[] args) {
3. double radius;
4. double area;
5. // Step 1: Read in radius
6. // Step 2: Compute area
7. // Step 3: Display the area
}
}
The program declares radius and area as variables. The reserved word double indicates that radius and area are floating-point values stored in the computer.
1st step is to prompt the user to designate the circle’s radius.
Note: You will soon learn how to prompt the user for information. For now, to learn how variables work, you can assign a fixed value to radius in the program as you write the code; later, you’ll modify the program to prompt the user for this value.
2nd step is to compute area by assigning the result of the expression radius * radius * 3.14159 to area.
3rd step., the program will display the value of area on the console by using the System.out.println method.
Figure 2.1 shows the complete program, and a sample run of the program is shown in
1. public class ComputeArea {
2. public static void main(String[] args) {
3. double radius; // Declare radius
4. double area; // Declare area
5.
6. // Assign a radius
7. radius = 20 ; // radius is now 20
8.
9. // Compute the area
10. area = radius * radius * 3.14159 ;
11.
12. // Display results
13. System.out.println("The area for the circle
14. of radius " + radius + " is " + area) ;
15. }
16. }
Wednesday, 13 September 2023
Programming Errors in Java language
Lesson 8:
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.
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.
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 using CMD prompt or command prompt
1st Step
Step 23rd 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.
_____________________________________________________
Wednesday, 23 August 2023
Lesson 6: Simple Program
A Java program is executed from the main method in the class.
Let’s begin with a simple Java program that is displays the message welcome to Java! On the console (output).
The word console is an old computer term that refers to the text entry and display device of a computer.
Console input means to receive input from the keyboard, and console output means to display output on the monitor.
Thursday, 17 August 2023
Lesson 5: Java Specification
Good day my dear students!!
We are almost there... Next week we are going to do the actual coding, so I suggest to study in advance for you to go along with the application next week. You may download the previous lessons from lesson 1 to 6 by clicking the link below and please answer the check-point 4 and 5 for your assignment.
Note: No assignment, No entry in the computer lab.
Download: Lessons 1 to 6 (Click here)ππ
*** LESSON FIVE (5) ***
**JAVA LANGUAGE SPECIFICATION **
π is a technical definition of the Java programming language’s Syntax and semantics.
**JAVA SYNTAX **
π is defined in the Java specification
**API**
πalso known as library, contains predefined classes and interfaces for developing Java programs.
Lesson 4: Java, the World Wide Web, and beyond