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

_________________________________________________________________
----Tip!----
It’s always good practice to outline your program (or its underlying problem) in the form
of an algorithm before you begin coding.
-------------------------------------------------------------------------------------------------------------

    When you code—that is, when you write a program—you translate an algorithm into a program.
You already know that every Java program begins with a class definition in which the keyword class is followed by the class name. Assume that you have chosen ComputeArea as the class name. The outline of the program would look like this:
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. }

Console output
_____________________________________________________
        End of the lesson
_____________________________________________________

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!

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.


Line 1 - defines a class. Every Java program must have at least one class. Each class has a
name. By convention, class names start with an uppercase letter. In this example, the class
name is Welcome.

Line 2 - defines the main method. The program is executed from the main method. A class
may contain several methods. The main method is the entry point where the program begins execution.

A method is a construct that contains statements. The main method in this program contains the System.out.println statement. This statement displays the string Welcome to Java! On the console (Line 4).

String is a programming term meaning a sequence of characters. A string must be enclosed in double quotation marks. Every statement in Java ends with a semicolon (;), known a the statement terminator.

Reserved words, or keywords, have a specific meaning to the compiler and cannot be used for other purpose in the program.
For example, when a compiler sees the word class, it  understand that the word after class is the name for the class. Other reserved words in this program are public, static, and void.

Line 3 is a comment that documents what the program is and how it is constructed. Comment help programmers to communicate and understand the program. They are not programming language and thus are ignored by the compiler.

In Java, comments are preceded by two slashes ( //) on a line, called a line comment, or enclosed between / * and */ on one or several lines, called a block comment or paragraph comment. When compiler sees //, it ignores all text after // on the same line
The following below are the example of comments:


A pair of curly braces in a program form a block that groups the program’s components. In Java, each block begins with an opening braces ({) and ends with a closing braces (}).

Every classes has a class block that groups the data and methods of the class. Similarly, every method has a method block that groups the statement in the method.

Blocks can be nested, meaning that one block can be placed within another, as shown in the following code below.

`~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~'
You have seen several special characters
(e.g., { }, //, and ;) in the program. They are used in almost every program. The most common errors you will make as you learn to program will be syntax errors.
'~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.'




.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~

Check-Point

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

Thank you!

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.

**JDK**
👉Java Development Toolkit
Example of Java development toolkit are the following:
NetBeans, Eclipse, and Textpad

**IDE**
👉is defined in the Java API
(Integrated Development Environment)
___________________________________________________________________
Note:
Computer languages have strict rules of usage. If you do not follow the rules when writing a programs, the computer will not be able to understand it.
___________________________________________________________________
Java is a full-fledged and powerful language that can be used in many ways it come in three edition

Java Standard Edition (Java SE) to develop client-side applications. The applications
can run standalone or as applets running from a Web browser.

 Java Enterprise Edition (Java EE) to develop server-side applications, such as Java
servlets, JavaServer Pages (JSP), and JavaServer Faces (JSF).

 Java Micro Edition (Java ME) to develop applications for mobile devices, such as
cell phones.


Check-point

Instruction: Read the question carefully and write the correct answer after each and every question. You may use 1/2 sheet of paper this time. Please write your answer neatly and legibly.

1.24 What is Java Language specification?
1.25 What does JDK stand for?
1.26 What is IDE stand for?
1.27 What is Java Syntax?
1.28 Discuss the different version of Java.

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

Lesson 4: Java, the World Wide Web, and beyond

 





Java has become enormously popular. It is rapid rise and wide acceptance can be traced to its designed characteristics, particularly its promise that you can write a program once and run it anywhere.
As stated by its designer Java is:
SIMPLE
OBJECT ORIENTED
DISTRIBUTED
INTERPRETED
ROBUST
SECURE
ARCHETECTURE NEUTRAL, 
PORTABLE
HIGH PERFORMANCE
MULTI-THREADED; AND DYNAMIC
Java is full-featured, general-purpose programming language that can be used to develop  robust mission-critical applications.
Today, it is employed not only for Web programming but also for developing standalone applications across platforms on servers, desktop computers, and mobile devices.










Instruction: Read the question properly and Write your answer on a 1/2 sheet of paper and submit it next meeting (August 22, 2023).


_____________________________________________________
   ~o~ End of the lesson ~o~
_____________________________________________________
Thank you and God bless you...

Saturday, 12 August 2023

Lesson 3: Operating System

Greetings!

Good day everyone please have time to study because we are going to have a Weekly test on Tuesday (August 14, 2023) I already gave you a hard copy last meeting so that you could study from Lesson 1, 2 and 3 and you may download the ppt below for your convenient. Thank you and see you in my class.

Lesson 3: Operating System

What is an Operating System?
Is the important program that runs on a computer.
The OS manages and control a computer's activity.
Popular operating system for general-purpose computers;
* Windows
* Mac OS
* Linux
Function of OS
* Providing a user interface
* Running application
* Support for built in utility
* Control to the computer hardware
Components of OS
* Process Management
* Memory management
* I/O device management
* Network management
* User interface
Types of OS
* Single user, single task
This type of operating system only has to deal one person at a time,
running one user application at a time.
example: Nokia mobile phone (Symbian), Palm OS- hand held computers.
* Single user, multi-tasking
a. Windows - allows user to perform one or more than one task at a time
-Commonly as; MS windows and Apple Macintosh
-Windows is a series of operating systems developed by Microsoft
each version of windows includes graphical user interface, with desktop that allows users to view files and folder.
b. Android OS now a days is a Single user, multi-tasking because of its multi-tasking features.
c. Macintosh -OS X is version 10 of the Apple Macintosh operating system.
-OS X was described by apple as its first complete revision of the previous version OS 9, with a focus on modularity so that   future changes would be easier to incorporate.
  - Written in C++, C. Objective -C
Operating System interface
* Command line interface
* Graphic user interface
Advantages of OS
* Easy to use
* User friendly
* Intermediate between all hardware's and software of the system.
* No need to know any technical languages
* It’s the platform all programs.
Disadvantages
* If any problems affected in OS, you may lose all the contents which have been stored already
* Unwanted user can use your own system. (Wireless desktop) and computer hacking. 

Check-Point
1. What is an Operating System?
2. What are the 3 prominent operating systems for general-purpose computers?
3. What are the functions of an Operating system.
4. Name the 2 types of OS.
5. Name two (2) Operating system interface.
6. Name the advantage and disadvantages of an operating system.

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

Thank you and God bless!

Saturday, 5 August 2023

Lesson 2: PROGRAM




Please click and download the following links below for your reference and for your 1st performance task. (Please take note for the deadline of submission will be on Tuesday - August 8, 2023 and we're going to have a summative test on the same date.)



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

Thank you and God bless...

Tuesday, 25 July 2023

Exercises - Lesson 1

 

Saint Francis of Assisi School,
Peace Valley, Lahug Cebu City,
Mhuddy T. Dagatan, LPT. (Computer teacher).
Lesson 1
Introduction to Java Programming

Summative test Exercises

1.1. What is hardware and software?
1.2. List five major hardware components of a computer.
1.3 What does the acronym “CPU” stand for?
1.4. What unit is used to measure CPU speed?
1.5. What is a bit? What is a byte?
1.6. What is memory for? What does RAM stand for? Why is memory called RAM?
1.7. What unit is used to measure memory size?
1.8. What unit is used to measure disk size?
1.9. What is the primary difference between memory and a storage device?




Thank you and God bless...