Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 111

LO -01

Programming Assignment

CSD 68
Prabhath Kodisinghe
Table of Contents
1 What is an algorithm?............................................................................................................5
2 Why(purpose) algorithm?......................................................................................................5
3 Algorithms............................................................................................................................. 5
3.1 These example for solving a leakage of the pipe line in the house.................................6
3.2 When someone fallen and bleeding in their wounded...................................................6
3.3 How to make a fruit salad............................................................................................... 6
3.4 Flow chart to input two numbers and thereafter calculate and display the average......7
3.5 Flowchart to input 2 numbers and thereafter evaluate and display the maximum value.
7
3.6 EX 2. 6: Flowchart to calculate and display the first 5 natural numbers..........................7
4 Chapter 1. 3 Flowcharts.........................................................................................................8
4.1 Flowchart to for solving a leakage of the pipe line in the house.....................................9
4.2 Flowchart to when someone fallen and bleeding in their wounded what we do before
medic......................................................................................................................................... 9
4.3 Flow chart to How to make a fruit salad.......................................................................10
4.4 Flow chart to input two numbers and thereafter calculate and display the average....10
4.5 Flowchart to input 2 numbers and thereafter evaluate and display the maximum value.
11
4.6 Flowchart to calculate and display the first 5 natural numbers....................................11
5 What is pseudocode?...........................................................................................................12
5.1 Pseudocode to input two numbers and thereafter calculate and display the average. 13
5.2 Pseudocode to input 2 numbers and thereafter evaluate and display the maximum
value. 14
5.3 Pseudocode to calculate and display the first 5 natural numbers.................................14
6 What is computer program & programming languages?.....................................................15
7 Introduction to JAVA Language............................................................................................17
8 Let’s see above program related algorithm in java codes….................................................19
8.1 Java code to input two numbers and thereafter calculate and display the average.. 19
8.2 Java code to input 2 numbers and thereafter evaluate and display the maximum value.
19
8.3 Java code to input two numbers and thereafter calculate and display the average.....20
1|Page
8.4 Java code to calculate and display the first 5 natural numbers.....................................21
9 Introduce program...............................................................................................................21
10 This is algorithm for this program........................................................................................22
11 This is the flow chart for this program.................................................................................23
12 This is the pseudocode for this algorithm............................................................................25
13 According to this algorithm the java codes are given below................................................26
14 Explaining about program codes..........................................................................................27
15 Explain about the relationship between the written algorithm and the code variant.........32
16 Why is programming paradigm?..........................................................................................35
17 What is programing paradigm?........................................................................................... 35
18 programming paradigm.......................................................................................................35
18.1 Procedural programming paradigm...........................................................................36
18.1.1 Advantage and disadvantage of procedural language...........................................37
18.1.2 Advantage..............................................................................................................37
18.1.3 Disadvantage..........................................................................................................37
18.1.4 Chapter 3.1. 2 Example’s for procedural language: -.............................................37
18.1.5 Example’s for C program: -.....................................................................................38
18.1.6 The compilation of C..............................................................................................38
18.2 Object oriented programming paradigm...................................................................39
18.2.1 OOP is mainly deal with six parts...........................................................................39
18.2.2 Example language for OOP paradigms...................................................................43
18.2.3 Java compilation (JVM, JRE)...................................................................................43
18.2.4 Advantage and disadvantage of OOP.....................................................................43
18.3 Event-driven programming paradigm........................................................................44
19 Let’s compare about the some main characterize of C, java and java script programing
languages..................................................................................................................................... 44
19.1 Input and output........................................................................................................44
19.1.1 In C language how to use input and output...........................................................44
19.1.2 In java language how to use input and output.......................................................46
19.1.3 In java Script language how to use input and output.............................................47
19.2 Variables.................................................................................................................... 50

2|Page
19.2.1 Let's see the variable for C language:.....................................................................50
19.2.2 variable in java language........................................................................................50
19.2.3 Variable in JS..........................................................................................................50
19.3 Data types..................................................................................................................51
19.3.1 Let’s see type of data types in C language: -..........................................................51
19.3.2 Java data types.......................................................................................................52
19.3.3 Data type in JavaScript...........................................................................................52
19.4 Operators...................................................................................................................52
19.4.1 Operators in C language.........................................................................................52
19.4.2 Operators in java language....................................................................................54
19.4.3 Operators in Javascript...........................................................................................54
19.5 Format specifier in C language...................................................................................54
19.6 Statement.................................................................................................................. 55
19.6.1 C and java and Javascript are use same syntax for the ‘if statement’....................55
19.6.2 C and java and Javascript are use same syntax for the ‘if else statement’.............56
19.6.3 C and java and Javascript are use same syntax for the ‘if-else-if ladder statement’
57
19.7 Loops......................................................................................................................... 58
19.7.1 The C language and the Java language and Javascript are the same in those Do-
while loops...........................................................................................................................59
19.7.2 The C language and the Java language and Javascript are the same in those while
loops 59
19.7.3 The C language and the Java language and Javascript are the same in those for
loops 60
20 Now let’s see same program in different paradigm.............................................................61
20.1 Let’s see the program in C language..........................................................................63
20.1.1 Explain about the code...........................................................................................63
20.2 Let’s see the program in java language.....................................................................64
20.3 In Javascript language code will be this.....................................................................65
21 explain about IDEs............................................................................................................... 66
22 Different capabilities between ides.....................................................................................71
23 Find details in sri lankan national identity card....................................................................73

3|Page
23.1 Let’s see what detail we can find...............................................................................73
23.2 Algorithm for NIC Detail finder program...................................................................73
23.3 The flow chart is given below....................................................................................75
23.4 Program codes in IntelliJ IDE......................................................................................79
23.5 Chapter 1. 5 Explain about the program....................................................................84
24 Evaluate the use of an IDE for development of applications contrasted with not using an
IDE 85
24.1 If no IDE used?...........................................................................................................90
25 What is the coding standard?..............................................................................................91
26 Why coding standard important as a team or individual?...................................................91
27 Benefits of coding standards................................................................................................92
28 Java coding standards..........................................................................................................92
28.1 Standard headers, put into the program...................................................................92
28.2 Naming conventions for local variables, global variables, constants and functions. .93
28.3 Use understandable comments in necessary places only..........................................94
28.4 Curly brackets use......................................................................................................94
28.5 About the spaces.......................................................................................................95
28.6 Variable declaration...................................................................................................97
28.7 The summery fragment.............................................................................................97
29 Debugging process...............................................................................................................98
29.1 Compile time errors...................................................................................................98
29.2 Run time error........................................................................................................... 99
29.3 Logical error.............................................................................................................100
30 Debugging process in IntelliJ IDE........................................................................................100
31 Go to the next step............................................................................................................ 102
32 Bibliography....................................................................................................................... 105
33 References......................................................................................................................... 105
34 References......................................................................................................................... 105

4|Page
LO 1
Task 1.1

1 What is an algorithm?
Algorithms are simply series of instruction that are followed step by step to solve a problem.
when we solve any problem, we need steps to achieve it and steps must have an order. first
step would be the very beginning of the problem and it must be flow clearly and smoothly.
(wei, 2021)

2 Why(purpose) algorithm?
When we want to design and implement a program it is important to develop a "blueprint of
the program, which details the step-by-step procedure in arriving at the solution. This is just
similar to creating an architectural plan for a house which we are planning to build. Such
"blueprint” is called an algorithm. The notation used for algorithm specification must conform
to a Basic set of criteria:
It must be concise - We must be able to describe the solution to a problem without writing
multiple pages of text.
It must be unambiguous – The description of the procedure must not be open to alternative
interpretations. We must remember that the algorithm will be executed by a machine which
has no ‘Common sense’ or inbuilt knowledge of the nature of the problem.
It must be capable of machine execution – The actions described must be capable of
translation into precise, machine-executable operations This precludes such statements as
“choose the biggest egg”. If this were the object of the operation, the steps involved in
evaluating the exact criteria for what constituted “biggest” must be clearly set out.
As natural languages do not offer the qualities mentioned above, following semi-formal
specifications and latterly even formal specifications were introduced.
Semi-formal specifications- Flow charts, Pseudocode, Structured English, Decision tables,
Decision trees, NS diagrams etc.
Formal specifications- Z specifications, Algebraic Specifications, Pre and Post conditions. etc.
(TEAM, 2020)

3 Algorithms
There are simply two types of algorithms. One is program related and other one is non-
relegated. Let’s see both of types with in examples.

3.1 These example for solving a leakage of the pipe line in the house
1. Firstly, we need to find place the broken.

5|Page
2. Close the water line.
3. Collect the all tools do you wanted.
4. We need clean that place for repair.
5. Cut and remove the broken part.
6. Dry the surface.
7. Apply pipe glue in surface.
8. Get the new pipe and put between cutting edge and press it.
9. After few minutes release the water line and check is that seal properly.

 In above example you can see begins of the algorithm, first of all algorithm did
find the place of broken. without find it we cannot do nothing. that’s why I tell you
first step would be place of the beginning the problem. secondly closed the water
line and other steps flow after this step. we cannot abandon second step and go
to third step. that is the flowing with smoothly.

3.2 When someone fallen and bleeding in their wounded


1.clean the wound in clean and falling water.
2.keep that wound over the head.
3.Get some bandage and wrapping around the wound tightly.
4.keep him relax and Allow to ventilate.
5. if bleeding strictly and it is not If not less again go to step 3.
6. Go to the nearest hospital quickly.
 This algorithm is different from the first algorithm. The reason for this is that in the
fifth step of this algorithm a condition is queried and if it is true then it goes back
to third steps and if it is false then it goes down.

3.3 How to make a fruit salad


1.Collect the all fruits, like mango, banana, pineapple.
2.Wash and peeling fruits
3.Cut all the fruit into small pieces.
4.Mix all the fruits together and mix.
5.add sugar to the mixture and mix well.
6.Test if the sugar suits your taste.
7.If sugar not enough go step 5.
8.Now put the mixture in a salad cup and place the ice cream on top.
 This algorithm, like the above algorithm, examines a condition in step seven and
if it is true it goes to step five and if it is false it goes down.

This is computer programming related examples …

6|Page
3.4 Flow chart to input two numbers and thereafter calculate and display the
average.
What this algorithm does is combine two numbers in the system and display their value
in the console. This is a linear flow, there is no condition check or return to the previous
steps.
1. Input two numbers
2. Get the first number as ‘A’ second number as ‘B’
3. accumulate A to B and divide total by 2.
4. Assign the total to Average.
5. print the output of ‘Average’.

3.5 Flowchart to input 2 numbers and thereafter evaluate and display the
maximum value.
This algorithm is a Selection algorithm. The third step of this algorithm asks for a
condition, and the next step changes the condition relative to the true falsehood. So it
include a condition. That’s why it is be a selection algorithm.
1. Get the two numbers
2. Assign the first value as ‘A’ and second value as ‘B’.
3. Check what number is large than other number
4. Assign the large number as ‘Max’.
5. Print the output of ‘Max’

3.6 EX 2. 1: Flowchart to calculate and display the first 5 natural numbers.


This is a Repetition loop. In the fourth step in this algorithm a condition is given and if
that condition is not fulfilled then it is sent back to the second step. It repeats the same
action over and over again until the condition is met, that’s this is be a Repetition loop.
(University, 2018)
1. Get the first number as ‘Sum’ and ‘Sum’ = 0
2. Sum+1 and get the total
3. Assign to total to the ‘Sum’
4. Check the ‘Sum’ lower than or equal to 5
5. If the ‘Sum’ lower than 5 go to step 2.
6. If ‘Sum’ equal to 5, print ‘Sum’.

7|Page
4 Chapter 1. 1 Flowcharts
After drawing an algorithm, we draw a flow chart. The algorithm is closer to human
language and the flow chart is closer to computer languages. Flow charts are used to
write the steps in an algorithm in a more detailed and easier way to translate into a
computer language. Here it is possible to see very clearly all the steps of the relevant
action separately. This is because certain symbols are used to write this. Different
action is represented in relation to different symbols.
History of flow charts - Flowcharts to document business processes came into use in
the 1920s and ‘30s. In 1921, industrial engineers Frank and Lillian Gilbreth introduced
the “Flow Process Chart” to the American Society of Mechanical Engineers (ASME). In
the early 1930s, industrial engineer Allan H. Morgensen used Gilbreth’s tools to
present conferences on making work more efficient to business people at his company.
(Software, 2019)
A flow chart is a diagrammatic representation of the processes involved in arriving at a
solution to a problem while many symbols are included in its notation, the most
important are:

8|Page
4.1 Flowchart to for solving a leakage of the pipe line in the house

4.2 Flowchart to when someone fallen and bleeding in their wounded what we do
before medic.

4.3 Flow chart to How to make a fruit salad


9|Page
4.4 Flow chart to input two numbers and thereafter calculate and display the
average.

4.5 Flowchart to input 2 numbers and thereafter evaluate and display the
maximum value.
10 | P a g e
4.6 Flowchart to calculate and display the first 5 natural numbers.

11 | P a g e
5 What is pseudocode?
Pseudocode is Structured English like way of representing the solution to a problem. It
is considered a “first draft” because Pseudocode eventually has to be translated in to a
programming language. Although Pseudocode is like English and has some precision to
it, it does not have the very definite precision of a programming language. A computer
cannot execute Pseudocode.
When using Pseudocode, it is easy to plan a program, concentrate on the logic and do
not worry about the rules of a specific language. It is also easy to change Pseudocode
when an error is discovered in logic; Once it is coded in a programming language, most
people find that it is more difficult to change logic. Pseudocode similar than Algorithm
for the computer language codes. (Geeks, 2018)
Pseudocode KEYWORD:
KEYWORD must be written in UPPERCASE
START
 this is for start the coding, whatever you write it must be inside below the Start.

INPUT, GET, READ

 input data, whatever data you insert for the calculate or initialization you need
represent by this. After this keyword write it.
PRINT
 for print output, show the prosses data.
CALCULATE
 calculate the result, prosses inserted data.
SET
 initialize value, for the value set to calculate.
INCREMENT
 increase value (x = x+1 )
DECREMENT
 reduce value (x = x-1)
WHILE
 For iteration, if you need rotate some codes. For while loops.

12 | P a g e
REPETE-UNTIL
 Loop iteration will end of this syntax

IF-THEN-ELSE
 A decision making, if Statement represent.
Program Wrapping:
This is the pseudocode block. This is the pseudocode block. Whatever you doing you
need follow this block given below.

PROGRAM program_name
// Pseudocodes
END

Lest see examples for Pseudocode….

5.1 Pseudocode to input two numbers and thereafter calculate and display the
average.

PROGRAM get average of two number


START
INPUT A, B
 (this line for get data to program)
GET A
 (this line, that given ‘A’ initialize to the next calculation)
GET B
 (this line, that given ‘B’ initialize to the next calculation)
CALCULATE Average = (A+B)/2
 (this is for calculation)
PRINT Average
 (now print the calculation result)
END

13 | P a g e
5.2 Pseudocode to input 2 numbers and thereafter evaluate and display the
maximum value.

PROGRAM get large number


START
INPUT num1, num2
 (This line initializes two variables named by num1 and num2)
GET num1
GET num2
IF num1 > num2
 (this line system check what number is bigger, if num1 bigger than num2 then
the system does the down line)
PRINT “Large number is num1”
ELSE
 (in the if line condition gets false then do this)
PRINT “Large number is num2”
END IF
 (in this line end the if statement)
END

5.3 Pseudocode to calculate and display the first 5 natural numbers.

PROGRAM display the first 5 natural numbers


START
GET sum = 0
GET N = 1
WHILE N <= 5
 (this is called loop, if N lower than or equal to 5, then again and again loop will
be executing the statement in the loop)
CALCULATE Sum = sum + N
 (this is the calculation in the loop)
14 | P a g e
DECREMENT N
 (this line will do ‘N+1 ‘round by round, first round N=1, second round N=2 third
round N=3… likewise)
END WHILE
 (this line while will be destroyed)
PRINT sum
 (after while loop ‘sum’ has the deferent value than beginning of ‘sum’, this line
will print the last value of ‘sum’)
END

6 What is computer program & programming languages?


There are two main parts to a computer: the hardware and the software. The
performance of all hardware components is controlled by the operating system. The
operating system has a limited amount of software. Ex – calculator, calendar, few
games, media player, image viewer etc.
But that software is not enough for us. We need different software for different
purposes. Software is an application that is used to get something done through a
computer. Often those tasks involve entering data, storing it, processing it, and
presenting it.
Computer programs are collections of instructions that a computer tell how to interact
with the user, interact with the computer hardware and process data. Software is made
up of several programs. A program is a piece of software made up of codes that
perform one of several Task.
We humans speak different languages to communicate. We have to communicate with
each other for everything we do on a daily basis. If we want a person to do something,
we need a language, that we can speak, and understand to explain how that person
does it. The same goes for a computer. When writing a program to a computer, the
person who writes it needs a language that can be understood and the computer can
understand what we are saying. Such languages are called computer languages.
(TEAM, 2020)
Let’s see in real world example ...
‘John’ has a grocery store in a very busy town. Every day many
customers come to his grocery store. Customers are buying lot of items from this
shop. After the customer come to his shop and buy the goods, he collects the
price by writing them in a book. This takes a lot of time for him and the accuracy
is minimal. He then began to use calculator for that. It was an accurate and
quick, application for him. But his business grows up then he wanted to keep his

15 | P a g e
income and expenses and analyze them. So now he has only solution was a
computer program. For that he will definitely need a computer program.

According to the image above client wants to do some work by using computer. but he
cannot do that directly so he need some computer program to serve him. for make that
computer program, to write the program need a computer programmer. how
16 | P a g e
programmer dealing and talking with computer.? for it he needs to learn language that
computer can understand and already he knows it. that’s why client give him problem to
the programmer to solve it.
computer language is the translator among the computer and the human. Computer
languages are the most closable with English language.
The first programmable computers required the programmers to write explicit instruction
to directly manipulate the hardware of the computer. This “machine language” was very
tedious to write by hand since even simple tasks such as printing some out put on the
screen require 10 or 20 machine language commands (low level language).
By contrast, higher level languages such as C, C++, java are called “complied
languages”. In a compiled language, the programmer writes more general instructions
and a compiler (special piece of software) automatically translate these high-level
instructions into machine language. The machine language is then executed by the
computer. A large portion of software in use today is programmed in this fashion.
(TEAM, 2020)

7 Introduction to JAVA Language


Java is a programming language developed by team of developers lead by
James gosling at Sun Microsystems (Which is now a subsidiary of Oracle Corporation).
The first version of Java language was released in 1995.
The language derives much of its syntax from C and C++ but has a simpler object
model and fewer low-level facilities. Java applications are typically compiled to bytecode
(Class file) that can run on any Java Virtual Machine (JVM) regardless of computer
architecture.
Why should you use Java?
 Java is platform independent
 Run on Windows, Linux, Mac OS etc.
 Java is object-oriented.
 Java is easy to learn.
 You need to write less code when using Java.
 Java promotes best practices of object-oriented programming.
 Java is secure.
What can you do with Java?
 Java programs
 Java applets
 Java desktop (GUI) applications
 Java web applications
 Java enterprise level applications
 Java web services
 Java mobile applications
The way JAVA works

17 | P a g e
In the java programming language, all source code is first written in plane test file
ending with the .java extension. Those source files are then complete into .class
file by the javac compiler. A .class file does not contain code that is native to your
processor; it instead contains bytecodes – the machine language of the java
virtual machine (Java vm). The java launched tool then runs your application with
an instance of the java virtual machine. (TEAM, 2020)

The java VM is available on many different operating systems, the same .class files are
capable of running on Microsoft windows, the Solaris TM, Linux or Mac OS.
 Java Is an object-oriented programming, so whatever you write it should be
surround by class.
This is simple example for java program.

All codes in the red box belong to that class and All codes in the pink box belong to that
main method. Open curly brackets indicate the beginning of a class or school and
closed curly brackets indicate their end.

18 | P a g e
If you need get some output in console, this is the key words for it. If you write anything
in upper comma’s it will be the output.

Java Main method


This too method, but it is main method. So, it is very special because whatever you
write in the class it will be executed by inside of this main method. What is the order of
executing method it will be decide under main method? First line first, in main method
you write something in first line it will be executed first of all, after first line it will go to
next line

8 Let’s see above program related algorithm in java codes….


8.1 Java code to input two numbers and thereafter calculate and display the
average.
8.2 Java code to input 2 numbers and thereafter evaluate and display the
maximum value.

19 | P a g e
As you can see in the picture above, I have initialized two variables in Integer data type.
One is a =10, and other one is b =20.
I have used an IF-ELSE Statement. Its condition is whether ‘b’ is larger than. If ‘a’ is
large then, the process in the ‘IF’ is done and if ‘b’ is large, then the process in the
ELSE, is done.
This is a Selection statement code.

8.3 Java code to input two numbers and thereafter calculate and display the
average.

As you can see in the picture above, I have initialized two variables in Integer data type.
One is x = 12 and other one is y = 34
Another variable has named as ‘avarage’ and its equal to (x+y) / 2, calculation.
And finally, I got as print of ‘avarage’

20 | P a g e
8.4 Java code to calculate and display the first 5 natural numbers.

As you can see in the picture above, I have initialized two variables in Integer data type.
One is sum = 0 and other one is n = 1
After it I create ‘while loop’, in the while loop the condition is ‘n<=5.
Before came to the loop n is 1 and sum is 0, after come to the loop and first round in the
loop sum will be 1, n will be 2.
First round >> sum = 0+1 = 1 and n = 1+1 = 2
Second round >> sum = 1+2 = 3 and n = 2+1 = 3
Third round >> sum = 3+3 = 6 and n = 3+1 = 4
Fourth round >> sum = 6+4 = 10 and n = 4+1 = 5
Fifth round >> sum = 10+5 = 15 and n = 5+1 = 6
Now n =6 so, the condition of while loop is n <= 5 , but now n exceed the value of
condition so now loop is brooked.
Finally, value of sum is printed out on the screen.

Task 1.2
Design a program using JAVA which is capable of finding the entered value is an Even
or an Odd number.

9 Introduce program
I created this program to check if a value is odd or even when a user enters a value.
Further improving this, the player is given the opportunity to play again without leaving
the program if desired.

21 | P a g e
10 This is algorithm for this program
1.Show welcome for player
2.Ask player name
3.Show player name with “Welcome”
4.Ask for value from player
5.Get the value and divide by two
6.Surplus after division is equal to zero then show the answer as “given number is
even.”
7.If surplus after is one then show the answer as “given number is odd.”
8.And ask from player if you need play another round
9.If player like it, run the game again.
10.If player need to exit, exit in game.
11.If player enter wrong answer ask again that play or exit question

22 | P a g e
11 This is the flow chart for this program

23 | P a g e
24 | P a g e
12 This is the pseudocode for this algorithm
PROGRAM find given number even or odd
START
INITIALIZE Scanner = enter
PRINT "Welcome even-odd game"
PRINT "Enter your name :"
PRINT "Welcome" + INPUT enter.nextInt()
SET number = INPUT enter.nextInt()
SET n = 1
DO
PRINT "Enter Number"
SET num = number.nextInt()
SET rem = num%2
IF rem = 0
PRINT num+ "is even number"
ELSE
PRINT num+ "is odd number"
END IF
SET in = 0
DO
PRINT " Type 1: Play again"
PRINT " Type 2: EXIT"
SET choice = number.nextInt()
IF choice = 1
SET n = 1, in = 1
ELSE IF choice = 2
SET n = 2
ELSE

25 | P a g e
SET in = 0
PRINT " Invalid choice"
END IF
WHILE in = 0
END WHILE
WHILE n = 0
END WHILE
END

13 According to this algorithm the java codes are given below.


This is the real codes of this program, program design by java language
use IntelliJ IDE.

26 | P a g e
14 Explaining about program codes
Scanner enter = new Scanner(System.in);
I used this line to get input from user. Here I have used the sun. That way I can get data
from outside. When we write a program, we have to get data inside as well as data
inside the program. By encoding as above we can get the data into the program from
the outside.
‘Scanner ‘is a keyword. enter is the name that ‘enter ‘is the data retrieval reference
variable. The ‘reference variable’ is the address of the location where the data is
stored in the RAM.

I used this line for print “Welcome to the Game” and “Enter your name:” on the console
And you can see there are not printed character in the middle. It is “\n “, Why I used
this? so This is used to breakup line down the phrase that precedes it.

This is very important line; you can see after + mark ‘enter. next () , let’s see what is
this,

27 | P a g e
You know already I initialize the Scanner named by ‘enter ‘so I tell you that is like
address, so I wrote that name and put ‘. ‘mark then I go that address. After I tell to the
java compiler get the last inputs of console and store it that location belongs to the
‘enter ‘.
So, after it now location of ‘enter ‘is not empty anymore, now he has player name.
The ‘+’ This mark is used to combine “Welcome” and other word. It is an Operator used
in java.

This is line of making variable and at the same time I initialize the value to the ‘n ‘.
In this line I have created a variable called ‘n’ from Integer data type. First there is a
data type, then there is a name of variable, = this sign tells RAM to remember the value
after this mark.

As you can see in the picture above, when I create a variable, the compiler creates a
container of the same data type(int), in the RAM and inserts the given value(1), into that
container.
Datatype can be ‘int’ if you using Integers numbers, if you use floating numbers you
have to use ‘double ‘keyword for ‘int ’ , if you use text, it will be ‘String’. In ‘String’ you
can use numbers and text, but in this data type number cannot use for calculation.
Because they all are recognize as a text.

This is ‘do- while loop, let’s see what is the loop and why use it.
This is a program designed to make a game. I want to give the player that opportunity if
he wants to play again. This requires the use of code that allows you to perform the

28 | P a g e
same process over and over again without leaving the program. In that case Then
there's the loop.
We have three types of loop, 1. While loop, 2. Do-while loop, 3. for loop
In this case I used do-while loop, let me explain it,

Coded above is the opportunity given to the player at the first arrival of the player. After
the first stage, the player can play or leave if he wishes. If he plays again, program will
have to do the same process again. So that loop is used to make the first round
unconditional and to make every subsequent round after the first-round conditional. If
the condition is true, it moves on to the next round, and if the condition is false, it exits
the loop after the first round.

The image above shows the condition related to the loop. If the value of the ‘n’ we have
created is 1, then the loop is active and when any other value is assigned to the ‘n’ then
loop will be destroyed.

29 | P a g e
Let’s see what going on in loop….

The first line appears on the screen as "Enter number ". The player then enters a value.
Inserting that value into a variable as 'num' is done in the second line. In the third line, the
value of 'num’ is divided by two and the remainder is inserted into the variable called 'rem'. '%'
This Operator is used to divide the number before the symbol by the number after the symbol
to obtain the remaining number.
A 'statement' is used in the fourth line. What happens in an 'if statement' is that if a condition is
true, then an action is taken accordingly, and if that part is false, another action is taken.

30 | P a g e
The condition here is whether the value of 'rem' is equal to 0 or not. ‘rem = 0’ its mean the num
is even number, if rem = 0, then system will print
value of num and “ is an even number”. Ex: num=22 print will be “22 is an even number”.
If num=1, then print will be
value of num and “is an odd number”. Ex: num=23 print will be “23 is an odd number”.

Inside the orange color box, there is another loop in this current loop. I will explain it next. In
the red box is the condition corresponding to this loop, the condition is to activate this loop
only if 'n' is equal to 1. The loop is disabled at any other value. I used this loop to re-run the
code that the player needs to play again and again if he wants.

Ok, let’s move on that loop in the loop……

31 | P a g e
In the green box – The screen asks the player if he wants to play again or not, if the player plays
again, he enters the number '1', if he wants to leave, he enters the number '2'.
In the orange box - The value entered by the player is initialized into 'choice' .
In the yellow box - if player need to play again, he will be entered ‘1’, so choice will be ‘1’ now
condition inside of if (condition ==1) is true. Now the program executes all the codes in
If(choice==1) { here }, there are that the part of the first loop is true(n=1) and the condition of
the current loop is false (in =1).
When the player enters the number 1, the lines ‘else if(choice ==2)’ and ‘else’ in the if
statement are not considered. Comes straight to the end of the statement.
If player entered number 2, if (choice ==1) { //…} this part will not execute, and go to the next
line and check condition of else if (choice == 2) , Now that condition is true. So, program will
execute the codes between the curly brackets of ‘else if (choice == 2)’,
In that brackets, ‘n’ will be 2, and this loop will destroy with ‘break’ keyword. ‘break’ keyword
used to destroy the loop. Deactivation of the first loop above, as it is ‘n’ equal to ‘2’, results in
the exit of the entire program.
The first loop above is also deactivated when the existing value of 'n' is equal to ‘1’. After
disabling both this loop and the first loop, there is no other action to perform and you will have
to exit the entire program.
If the player manually enters another number, then the final statement is executed.
Being 'in = 0' there sets the condition for reactivating this loop. Now ‘in = 0’.

32 | P a g e
In red box – it is the This is the condition that determines whether or not to activate this loop. If
‘in = 0’, this loop will be executing again.
After program run its look like this.

15 Explain about the relationship between the written algorithm and the
code variant.

An algorithm determines what to do now and what to do next. This is as common to the
step algorithm as it is to the code. The codes move step by step just like the algorithms.
The difference is that the algorithm writes in a language that we can read and the code
is written in a language that can be read by a computer. The first is a simple English
language written algorithm and the second is a flow chart. It is designed to be easy to
program on a computer subject to a specific framework. It then translates into a
pseudocode, where it is closer to a computer language than before and the steps are all
sharpened. Eventually it is coded.
When preparing a house, make a pre-plan of how those houses will be built. We call it a
housing plan. The same is true of writing computer programs. Writing a computer
program also requires a plan. even writing a simple program requires an algorithm.
When writing a program, Jvm reads it line by line, so it decides what to do first in the
program and what to do next by writing an algorithm.

33 | P a g e
According to the above program,
The above program is designed to make a very simple game. Here there is a player to
give values to the system. He can enter a number of his choice and check if it is odd or
even. He has also been given the ability to play again and again without leaving the
system. Here the system must flow in one direction if the number he entered is odd and,
in another direction, even if it is even. So, there must be an algorithm to write the code
related to this selection. A pre-configured algorithm can determine when and how to
apply the relevant code.
It then asks if the player wants to play again. And if you want to play again and again,
you have to use a loop. The algorithm gives an understanding of how to use it, the
conditions under which it should be used and where it should be used.
Very specially, two loops are used in the above program and they are do-and-miss loop.
The idea of using that type of loop was derived from the algorithm. Why do I say that?

The flow of the program, Flow of program codes, the algorithm helps to determine the
sequence in which the if-else Statement, Loops and many others control process is
used and at what point it should be used.

34 | P a g e
(TEAM, 2020)

35 | P a g e
LO 2

16 Why is programming paradigm?


Writing a computer program makes writing a simple program easier. This is because it is easy
and simple to write a program in a few short steps. But when writing complex programs, it is
not so because of the large number of steps and the inability to adapt the steps.
Any fool can write code that a computer can understand. Good programmers write code that
humans can understand.
― Martin Fowler
When programming, complexity is always the enemy. Programs with great complexity, with
many moving parts and interdependent components, seem initially impressive. However, the
ability to translate a real-world problem into a simple or elegant solution requires a deeper
understanding.
The biggest challenge for a computer programmer is to turn a very complex problem into a
simple step. There are many general approaches that reduce complexity in a program or make
it more manageable. One of the main approaches is a programming paradigm. (ThanoshanMV,
2019)

17 What is programing paradigm?


The term programming paradigm refers to a style of programming. It does not refer to a
specific language, but rather it refers to the way you program. There are lots of programming
languages that are well-known but all of them need to follow some strategy when they are
implemented. And that strategy is a paradigm. (ThanoshanMV, 2019)

18 programming paradigm
There are three programming languages we talk about:

36 | P a g e
18.1 Procedural programming paradigm
In this paradigm the whole program consists of several statements and after executing all those
statements the relevant result is stored and then displayed. This is like a step-by-step
instruction on what to do with a computer. Here the priority of the relevant steps is very
important. It is important to know which steps should be implemented first and which steps
should be taken next.
It has the ability to reuse the code and it was a gift at that time when it was in use because of
its reusability.
In procedural programming the code of the program executes linearly with logical steps, it
follows the top to bottom approach, the code is written first considered execute with some
conditions. The conditions are decided by the procedure calls. For example, if there are two
functions in the program the main function executes fist and the code will execute top to
bottom order.

As shown in the figure above, func()2 is execute after func()1 and then func()3 is execute. The
order in which it operates is determined from top to bottom order.

37 | P a g e
As shown in the figure above, the commands inside the int main () , are has changed the order
of statement. Then look at the result. Where func()2 is first and func()3 is second and third are
func()1. So finally, we can see there are fallow the same order for executing.
18.1.1 Advantage and disadvantage of procedural language
18.1.2 Advantage
 Procedural Programming is excellent for general-purpose programming.
 The coded simplicity along with ease of implementation of compilers and interpreters.
 A large variety of books and online course material available on tested algorithms,
making it easier to learn along the way.
 The source code is portable; therefore, it can be used to target a different CPU as well.
 The code can be reused in different parts of the program, without the need to copy it.
 Through Procedural Programming technique, the memory requirement also slashes.
18.1.3 Disadvantage
 The program code is harder to write when Procedural Programming is employed.
 The Procedural code is often not reusable, which may pose the need to recreate the
code if is needed to use in another application.
 Difficult to relate with real-world objects.
 The importance is given to the operation rather than the data, which might pose issues
in some data-sensitive cases.
 The data is exposed to the whole program, making it not so much security friendly.
 The program flow can be tracked easily.
18.1.4 Chapter 3.1. 1 Example’s for procedural language: -
 C++
 C
 Python

38 | P a g e
18.1.5 Example’s for C program: -

#include <stdio.h> includes the standard input output library functions. The printf () function is
defined in stdio.h.
int main () The main () function is the entry point of every program in c language.
printf () The printf () function is used to print data on the console.
return 0 The return 0 statement, returns execution status to the OS. The 0 value is used for
successful execution and 1 for unsuccessful execution.
18.1.6 The compilation of C
The compilation is a process of converting the source code into object code. It is done with the
help of the compiler. The compiler checks the source code for the syntactical or structural
errors, and if the source code is error-free, then it generates the object code.
The following are the phases through which our program passes before being transformed into
an executable form:
1. Preprocessor
2. Compiler
3. Assembler
4. Linker

39 | P a g e
18.2 Object oriented programming paradigm
OOP is an emerging major programming paradigm. Much of its approach to program and
system design is owed to concepts which also gave rise to structured programming. An object is
a particular instance of a class and consists, essentially, of data which defines its characteristics
and current status together with procedures. Or ‘methods’, which operate on the object.
For example, an object may be a bank account processing data items which record the name
and address of its owner and its current balance. It may have associated methods which allow
its creation, deletion and amendment with deposits and withdrawals. (ThanoshanMV, 2019)
18.2.1 OOP is mainly deal with six parts
1. Object
2. Class
3. Inheritance
4. Polymorphism
5. Abstraction
6. Encapsulation

40 | P a g e
18.2.1.1 Object
Object is a real-world entity. an Object can be defined as an instance of a class. An object
contains an address and takes up some space in memory. Objects can communicate without
knowing the details of each other's data or code. The only necessary thing is the type of
message accepted and the type of response returned by the objects.
For example –
If we think about a plane

Any object has state and behaviors. States are that what object look like and its property.
Behavior is what object can do.
18.2.1.2 Class
A class is a group of objects which have common properties. It is a template or blueprint from
which objects are created. It is a logical entity. It can't be physical. Another explain for class it is
class is a blue print of the object.
Let’s see example

41 | P a g e
Using class of plane, you can make any number of object and it have various color, weight,
seats. So, class is a template that can make object initializing the values for properties.
18.2.1.3 Inheritance
The idea behind inheritance is that you can create new classes that are built upon existing
classes. When you inherit from an existing class, you can reuse methods and fields of the parent
class. Moreover, you can add new methods and fields in your current class also. Inheritance is
mainly use for code reusability.

18.2.1.4 Polymorphism
Polymorphism in Java is a concept by which we can perform a single action in different ways.
Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many
and "morphs" means forms. So polymorphism means many forms.

42 | P a g e
18.2.1.5 Abstraction
Abstraction class is a class which should not be instantiated. Usually, you make a class abstract
if it does not make sense to instantiate an object of given class. Java keyword abstract mark
your class as abstract. The compiler will not let you instantiate an abstract class. Abstract
classes are meant for inheritance and/ or polymorphism.

18.2.1.6 Encapsulation
Encapsulation is the concept of hiding the implementation details of a class and allowing access
to the class through a public interface. For this, we need to declare the instance variable of the
class as privet or protected. Encapsulation in Java is a process of wrapping code and data
together into a single unit, for example, a capsule which is mixed of several medicines.

43 | P a g e
18.2.2 Example language for OOP paradigms
Java, Python, C++, C#
18.2.3 Java compilation (JVM, JRE)
JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine because it
doesn't physically exist. It is a specification that provides a runtime environment in which Java
bytecode can be executed. It can also run those programs which are written in other languages
and compiled to Java bytecode.
The JVM performs the following main tasks:

 Loads code
 Verifies code
 Executes code
 Provides runtime environment
JRE
JRE is an acronym for Java Runtime Environment. It is also written as Java RTE. The Java
Runtime Environment is a set of software tools which are used for developing Java applications.
It is used to provide the runtime environment. It is the implementation of JVM. It physically
exists. It contains a set of libraries + other files that JVM uses at runtime.
18.2.4 Advantage and disadvantage of OOP
Advantage Disadvantage
A real-world idea can be demonstrated, as Designing a program with OOP concept
everything in OOP is treated as an object. can be tricky.
As we use the concept of encapsulation, A programmer needs to plan beforehand
programs are easier to test and maintain. for developing a program in OOP.
Faster development of code is done, as we The size of programs developed with OOP
develop classes parallel instead of is bigger than those developed with a
sequentially. procedural approach.
OOP provides greater security due to data Since OOP programs are larger in size, the
abstraction. The outside world cannot execution time for these programs is also
access the hidden data. more.
Reusability can be achieved by using Programmer need more knowledge for
classes that have been already written. use OOP concepts

44 | P a g e
18.3 Event-driven programming paradigm
event-driven programming is a programming paradigm in which the flow of the program is
determined by events such as user actions (mouse clicks, key presses), sensor outputs, or
message passing from other programs or threads. Event-handlers can be seen as small blocks of
procedural code that deal with a very specific occurrence. They will usually produce a visual
response to inform or direct the user, and will often change the system’s state. (ThanoshanMV,
2019)
In an event-driven application, there is generally a main loop that listens for events and then
triggers a callback function when one of those events is detected. Event-driven programs can
be written in any programming language, such as javaFX, JavaScript.
JavaScript is the example for most popular language in event-driven paradigm.
JavaScript is not a compiled language, but it is a translated language. The JavaScript Translator
(embedded in the browser) is responsible for translating the JavaScript code for the web
browser.
Advantages of JavaScript
1. Speed. Client-side JavaScript is very fast because it can be run immediately within the
client-side browser. Unless outside resources are required, JavaScript is unhindered by
network calls to a backend server.
2. Simplicity. JavaScript is relatively simple to learn and implement.
Disadvantages of JavaScript
1. Client-Side Security. Because the code executes on the users’ computer, in some cases it
can be exploited for malicious purposes. This is one reason some people choose to
disable Javascript.
2. Browser Support. JavaScript is sometimes interpreted differently by different browsers.
This makes it somewhat difficult to write cross-browser code.

19 Let’s compare about the some main characterize of C, java and java
script programing languages
19.1 Input and output
This command uses for any kind of programming languages. Whatever program you write most
time will have to input data into the program or set the output in the console. So for do that
there are own syntax with the programming language. (unknown, 2018)
19.1.1 In C language how to use input and output
 printf ()

45 | P a g e
The printf () and scanf () functions are used for input and output in C language. Both functions
are inbuilt library functions, defined in stdio.h (header file).

Syntax for print anything in console: -


printf ("format string", argument_list);
The format string can be %d (integer), %c (character), %s (string), %f (float) etc.

 scanf ()
The scanf () function is used for input. It reads the input data from the console.
scanf ("format string”, argument_list);

46 | P a g e
Output will be and after entering
value

19.1.2 In java language how to use input and output


Java is an OOP language. So, whatever you write it must be surrounded by class.

public static void main (String [] args) is the main method that available inside in the class A. All
codes are executed from this main method.
47 | P a g e
Syntax for the output: - System.out.println(“write anything to print “); Anything written in
brackets is displayed on the screen. If you remove an upper comma and write a word, that
word must be a pre-set variable. According to the above image, ‘x’ is a variable and before print
it must be declared.
When you write a program using java language if you need get some data from user, you need
a setup scanner. Its syntax and assign are given below:

19.1.3 In java Script language how to use input and output


Unlike Java and C languages, there are three ways to get output in JS. They are different from
each other. Unlike other languages, in JS the program is implemented in the web browser.
1. In body of the web page
2. By using popup menu
3. In console
We wrote a file of the js code but we have to insert it into a html file to make it work.
Les see example image

48 | P a g e
In the < script src = “nameofjsFile.js” > </script> tag you need to assign the js file name.
Let’s see js file

Output will be shown in the default web browser.


The popup menu output will be like this

The output in the body will be like this

The output in the console will be like this

49 | P a g e
Where can find console in the browser: - right click on the browser in the empty place. And click
the ‘inspect’ and go to console tab.

And input syntax is given below:

50 | P a g e
19.2 Variables
A variable is a name of the memory location. It is used to store data. Its value can be changed,
and it can be reused many times. It is a way to represent memory location through symbol so
that it can be easily identified.
19.2.1 Let's see the variable for C language:
Datatype variable_name;

int x;

19.2.2 variable in java language


in java, variable’s declaring most similar to C variable.
Datatype variable_name;

19.2.3 Variable in JS
In Javascript language variable declaration is different than java and c. let’s see how it.

51 | P a g e
According to the above image unlike java or c you do not need to worry about data type, data
type will be auto detective by Javascript runtime. So output in the console like seen below
image : -

Syntax for JS variable: - var variable = value;

19.3 Data types


Variable is used to store data in the RAM while the program is running. What kind of data is
that stored data? It is the data type. Applying a data type to a variable is mandatory.
19.3.1 Let’s see type of data types in C language: -

Types Data Types

Basic Data Type int, char, float, double

52 | P a g e
Derived Data Type array, pointer, structure,
union

Enumeration Data Type enum

Void Data Type void

Mostly used Basic data types in C.

Basic data type Used for

int Integer numbers

char One character

float Decimal numbers

double Decimal numbers

53 | P a g e
19.3.2 Java data types
Data types specify the different sizes and values that can be stored in the variable. There are
two types of data types in Java:
Primitive data types: The primitive data types include Boolean, char, byte, short, int, long, float
and double.
Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.
This data types most similar to C language data types.
19.3.3 Data type in JavaScript
Unlike java and C you do not need specially add datatype in to variable. But there are several
datatypes given below.
Data Type Description
String represents sequence of characters e.g. "hello"
Number represents numeric values e.g. 100
Boolean represents Boolean value either false or true
Undefined represents undefined value
Null represents undefined value

19.4 Operators
An operator is simply a symbol that is used to perform operations. There can be many types of
operations like arithmetic, logical, bitwise, etc.
19.4.1 Operators in C language
There are following types of operators to perform different types of operations in C language.

 Arithmetic Operators
 Relational Operators
 Shift Operators
 Logical Operators
 Bitwise Operators
 Ternary or Conditional Operators
 Assignment Operator
 Misc Operator

Category Operator

54 | P a g e
Postfix () [] -> . ++ - -

Unary + - ! ~ ++ - - (type) * &


sizeof

Multiplicative * / %

Additive +-

Relational < <= > >=

Equality == !=

Bitwise AND &

Logical AND &&

Logical OR ||

55 | P a g e
Conditional ? :

Assignment = += -= *= /= %=>>= <<=


&= ^= |=

Comma ,

Let’s see little program to understand about the job of operator in the program

56 | P a g e
19.4.2 Operators in java language
Operators in java is most similar to operators in C language
Operator Type Category Precedence
Unary postfix expr++ expr--
prefix ++expr --expr +expr -expr ~ !
Arithmetic multiplicative */%

57 | P a g e
additive +-
Relational comparison < > <= >= instanceof
equality == !=
Bitwise bitwise AND &
bitwise exclusive ^
bitwise inclusive |
Logical logical AND &&
logical OR ||
Ternary ternary ?:
Assignment assignment = += -= *= /= %= &= ^= |=
<<= >>= >>>=

19.4.3 Operators in Javascript


In Javascript operators are same as java and c there are same categorize in js operators. Same
work same operators.

19.5 Format specifier in C language


The Format specifier is a string used in the formatted input and output functions. The format
string determines the format of the input and output. The format string always starts with a '%'
character.

Format specifier Description

%d or %i It is used to print the signed


integer value where signed
integer means that the
variable can hold both
positive and negative
values.

58 | P a g e
%f It is used for printing the
decimal floating-point
values. By default, it prints
the 6 values after '.'

%s It is used to print the


strings.

There is no format specifier in java language and JS too.

19.6 Statement
Pins are used to change the flow of a program based on a condition while writing a program
and to stop the program and ask for the condition.
19.6.1 C and java and Javascript are use same syntax for the ‘if statement’
The if statement is used to check some given condition and perform some operations
depending upon the correctness of that condition. It is mostly used in the scenario where we
need to perform the different operations for the different conditions.

59 | P a g e
The syntax of the if statement is given below: -
if(condition) {
//code to be executed
}

19.6.2 C and java and Javascript are use same syntax for the ‘if else statement’
The if-else statement is used to perform two operations for a single condition. The if-else
statement is an extension to the if statement using which, we can perform two different
operations, i.e., one is for the correctness of that condition, and the other is for the
incorrectness of the condition. Here, we must notice that if and else block cannot be executed
simultaneously. Using if-else statement is always preferable since it always invokes an
otherwise case with every if condition.
The syntax of the if-else statement is given below: -
if(expression) {
//code to be executed if condition is true
} else {
//code to be executed if condition is false
}

60 | P a g e
19.6.3 C and java and Javascript are use same syntax for the ‘if-else-if ladder statement’
The if-else-if ladder statement is an extension to the if-else statement. It is used in the scenario
where there are multiple cases to be performed for different conditions. In if-else-if ladder
statement, if a condition is true then the statements defined in the if block will be executed,
otherwise if some other condition is true then the statements defined in the else-if block will be
executed, at the last if none of the condition is true then the statements defined in the else
block will be executed. There is multiple else-if blocks possible. It is similar to the switch case
statement where the default is executed instead of else block if none of the cases is matched.
The syntax of the if-else-if ladder statement is given below: -
if(condition1){

//code to be executed if condition1 is true

}else if(condition2){

//code to be executed if condition2 is true

else if(condition3){

//code to be executed if condition3 is true

61 | P a g e
else{

//code to be executed if all the conditions are false

19.7 Loops
The looping simplifies the complex problems into the easy ones. It enables us to alter the flow
of the program so that instead of writing the same code again and again, we can repeat the
same code for a finite number of times. For example, if we need to print the first 10 natural
numbers then, instead of using the printf statement 10 times, we can print inside a loop which
runs up to 10 iterations.

62 | P a g e
19.7.1 The C language and the Java language and Javascript are the same in those Do-while
loops
The do-while loop continues until a given condition satisfies. It is also called post tested loop. It
is used when it is necessary to execute the loop at least once (mostly menu driven programs).
The syntax of the do-while loop is given below: -
do{
//code to be executed
}while(condition);
What is special about this loop is that all the codes in the loop are executed in the first round,
whether the relevant condition is true or false.

19.7.2 The C language and the Java language and Javascript are the same in those while loops
The while loop in c is to be used in the scenario where we don't know the number of iterations
in advance. The block of statements is executed in the while loop until the condition specified in
the while loop is satisfied. It is also called a pre-tested loop.
The syntax of the while loop is given below: -
while(condition){
//code to be executed
63 | P a g e
}
Example for while loop: -

19.7.3 The C language and the Java language and Javascript are the same in those for loops
The for loop is used in the case where we need to execute some part of the code until the given
condition is satisfied. The for loop is also called as a per-tested loop. It is better to use for loop if
the number of iterations is known in advance.
The syntax of the for loop is given below: -
for( initialization ; condition ; incr/decr ){
//code to be executed
}
Example for for loop: -

64 | P a g e
20 Now let’s see same program in different paradigm
First let’s see what is the program.
This program for Program to determine whether a given number is a Dicerium number. A
number is said to be the dicerium number when the sum of its digit raised to the power of their
respective positions is equal to the number itself. For example, 175 is a Dicerium number as
follows
11 + 72 + 53 = 1 + 49 + 125 = 175

65 | P a g e
Some of the other examples of dicerium number are 89, 135, 518 etc.
To find whether given number is dicerium or not, calculate the sum of digits powered with their
respective positions. If the sum is equal to the original number then, the given number is
dicerium number.
Algorithm for find dicerium number:
1. Make a method or function for calculate counts the digits present in a number.
a. Use a while loop to check whether the number is not equal to 0.
b. Divide the number by 10 and increment the variable length by 1.
c. Return length.
2. Make a user input system and initialize variable num.
3. Make a copy of num by storing the value of num in n.
4. Using a another while loop calculates remainder rem repeatedly by dividing num with
10.
5. Calculate the value of rem raised to power its position, i.e. remlen and store the
computed value in a variable sum.
6. Check whether sum is equal to number. If yes, then given number is Dicerium number.
Else, it is not a dibarium number.

66 | P a g e
20.1 Let’s see the program in C language

20.1.1 Explain about the code


In this program we have one function that named by calculateLength(int n). This function is
used to find that how many digits in the given number. this function gets a value and returns a
value. In main function (int main() ) other calculation is continued. In c, whatever you write out
of the main function, all function must be execute in the main function and there is fixed order
to execute in the main function, it is top to bottom. In the main function when call to the

67 | P a g e
‘calculateLength (int n) ‘function, you need to pass the value. In this case that value is ‘num’.
and function will be return the value. It will be caught by ‘int len’ variable.

20.2 Let’s see the program in java language

Java is an object-oriented programming. So, whatever you write it must include in the class. In
the java, one thing is same as C. it is, in the java class definitely we need main method and it's

68 | P a g e
like main function in the C language. and other hand in the C language there are function, in
java we have similar part as function, it is methods. So, you can write any number of methods.
But all methods need to execute in the main method. The next thing is the execution order is
same as C.
While loop is similar as C, if-else statement similar as C, variables similar as C.

20.3 In Javascript language code will be this

When it comes to the java Script, just not like java and C. there are several changes in the JS
pattern.
Let’s talk about same things: 1. In c language we have function and function can be gets a value
and return the values. So, in JS similar as it. 2. while loops, if-else condition similar as other C
and java languages.
The different than other both languages are :1. variables, in variables there are not datatype.
So, all variable assign using ‘var’ syntax. And other vice you can see Math. Class use in the
function of calculateLength(num).
why? in other both languages I use int data type. In this type cannot keep decimal numbers. So,
let’s think that given number is 175, 175/10 = 17.5 >> in int data type it will take the integer
part only. So, it will be smoothly 17. But that case is deferent to JS. Because JS will change the

69 | P a g e
data type to decimal and keep the decimal part. That’s why I used the ‘Math.trunc ‘. It will be
cut and remove decimal part and keep integer value only. 2. There are not specific method or
function to execute other function or methods. Any where can get the function and call it.

21 explain about IDEs


IDE means Integrated Development Environment that Enables programmers to consolidate the
different aspects of writing a computer program. IDEs increase programmer productivity by
combining common activities of writing software into a single application: editing source code,
building executables, and debugging.
Let’s see why ide is important using java language and intelije ide.
Before go to the ide lets see it is in the text editor.

in this image you can see this is the smallest program in the java. When I write this I got too
much problem and I given list of it below.
1. I cannot remember spelling of syntax.
2. I cannot remember syntax for write main method
3. I cannot remember Cases of letters; is it start with simple or capital.
4. Until compiled I cannot find syntax errors
5. When I met errors, I got little tips from cmd, but it not enough find the error.
6. For run the code I have to do many steps in cmd(command prompt in windows).
7. Will I make middle size program using many variables, I need remember all variable
names in full text.
8. I spend lot of time to write this code.
9. All text are same color, same size, same format, same lineup so it is a mixed pickle.
If I need make a Scanner for getting user input, I need import that util by typing syntax of it.
There are in IDE have answers for All above issues
1. Just when I was making a class the ide will create all syntax for class.

70 | P a g e
2. There is shortcut for the mostly uses syntax like main method get print etc.
Type psvm then press tab, ide will generate all syntax for main method automatically.

71 | P a g e
And for write a “System.out.println” , just type ‘sout’ and hit tab KEY.

3. Letter cases not matter anymore. Ide will be generating it automatically.


4. This is one of the most important thing. Because the finding errors it is will be very easy
with ide.
I had syntax mistake in the ‘println’ . I had write it as ‘printl’ without ‘n’. so at that moment
ide will detected it an show me.

According to the above image you can see there are lot of error fixed answers given by ide.
For run the code just click tree keys at same time. Just it, ide will run the entire program.

72 | P a g e
Keys are :- Ctrl + Shift + F10

5. If I need get a total of two integer variable, I need to type all text correctly of both
variable. But in the ide it is very easy. At time I write first one or two letter of the
variable name ide will be displaying all the variable in that two letters.

6. I do not go to explain about time-wasting without using an ide. because you can imagine
how much time waste with the above explanation if I do not use an ide when I write a
program.

73 | P a g e
7. And this is very important thing it is about format . So im going to show you how it will

help to us.
You can see above image
The colors of syntax, there are orange color for variables and main used keywords. And green
color text, text will be displaying in the console same as you write in the program. White color
using for all other variable names and some other syntax. The comment lines are viewing as in
gray color.
You can see that place of method call (calculateLength) That line is italic, so you can easily
recognize that is a method call.
When we come to the lineup, you can see that curly brackets are belongs to class and methods
and loops and statements. All each block has an open curly bracket and a closing curly bracket.
we can clearly see that all codes belong to what block, using that dash-line dropped vertically.
And the spaces between the vertical line there are same arrangement for clear view. And the

74 | P a g e
line numbers are there. In the line number we can find any error or mistake or specially we can
do any updates in the codes.

use this icon you can minimize and maximize the codes in the same curly brackets
(blocks).
8. If you use scanner, Math class, or some library function, you need to import it from
library. So for that you need type manually without any syntax errors. But in ide, it will
be generate import part automatically.

at time I hit
enter it will be generated import syntax automatically.

Finally, I talking about intelije ide. But all of ides has these things. But between ides have
some small different Capabilities.

22 Different capabilities between ides

75 | P a g e
 Intelije advantage
Smart code auto filling. If comes to the code auto filling all ide have it. But intelije always
provide correct and most useable rest of writing code.
GUI design is dark in the intelije, and there are lot of colors that cool for the developers
view.
This is used for mostly java, kotlin

 Intelije disadvantage
It is waste lot of ram so it mostly slower than net bean.

 NetBeans advantage
NetBeans fast and easy to beginning to program and in java swing controls it will be very
helpful.
NetBeans support to java script html languages too. And html is available.

 NetBeans disadvantage
It has not colorful or Pleasant GUI for developers. It is not providing On-the-fly Code
Analysis.

 Visual Studio Code advantage


It is very light software. And it is fast more than intelije.
And I is support to html and Javascript languages too.

 Visual Studio Code advantage


Apprentice have some struggles with VS code ide.
I recommended the ide for you by using that what is your programming language. If you
working with java only better you chose intelije. If you working with java script better to
use VS code. (NYC, 2019)

76 | P a g e
LO 3

23 Find details in sri lankan national identity card


In NIC include 11 digit or 12 digits in new NICs. in NIC number we can get some information.
That numbers have pattern to find detail in NIC number.

23.1 Let’s see what detail we can find

(zone, 2016)

23.2 Algorithm for NIC Detail finder program


1) Get the NIC number
2) Count the digits on the ID card number entered.
3) If it has eleven digits, it is an old ID number.
4) If it has twelve count of digits, it is a new id number.
5) If it is an old ID number, extract the first two numbers. extract from number three to
number five. extract the letter at the end. Add 1900 to First two numbers, and then input it
is as a birth year
6) From number 3 to 5 is that below than 500, input it is as a ‘male’, if it is more than 500 input
it is as a ‘female’
7) If it is more than 500, then reduce 500 from the amount and the calculate month as given
below step 8,9,10
77 | P a g e
8) Check the value of the month in which the number segmented from number three to
number five is relevant.
9) Divide the year by 4 and balance is 0 it is a long year. if it any other value is not a normal
year. If it is a leap year get count as given below
i) 01 – 31 = January
ii) 31 – 59 = February
iii) 60 – 90 = march
iv) 91 – 120 = April
v) 121 – 151 = May
vi) 152 – 181 = June
vii) 182 – 212 = July
viii)213 – 243 = August
ix) 244 – 273 = September
x) 274 – 304 = October
xi) 305 – 334 = November
xii) 335 – 365 = December
10) If it is a long year get count as given below
i) 01 – 31 = January
ii) 31 – 60 = February
iii) 61 – 91 = march
iv) 92 – 121 = April
v) 122 – 152 = May
vi) 153 – 182 = June
vii) 183 – 213 = July
viii)214 – 244 = August
ix) 245 – 274 = September
x) 275 – 305 = October
xi) 306 – 336 = November
xii) 336 – 366 = December
11) If last digit is ‘v’ then show as “sri lankan” if it is ‘x’ show as “dual citizen”
12) After finding the month, subtract the number of days corresponding to that month and find
the date of birth
13) If that NIC number have 12 digits extract the first four numbers, extract from number 5 to 7,
extract the eight number.
14) Show first four numbers as a birth year.
15) And that extract digit 5 to 7, get the number and go to step 6.
16) If eight digits is 0, show as “sri lankan” if it is 1, show as “dual citizen”
17) Print gender, birth year month day, voting type.

78 | P a g e
23.3 The flow chart is given below

79 | P a g e
flow chart part 1

flow chart part 2

80 | P a g e
flow chart part 3

81 | P a g e
flow chart part 4

82 | P a g e
flow chart part 5
23.4 Program codes in IntelliJ IDE

83 | P a g e
code in java part 1

84 | P a g e
code in java part 2

85 | P a g e
code in java part 3

86 | P a g e
code in java part 4

87 | P a g e
code in java part 5

23.5 Chapter 1. 2 Explain about the program

I have used four methods for this program. The same code segments are used except for these
methods for repetitive code segments.
1. public void leapyear(int days)
This was used to find the relevant month according to the number of days given by the system.
Here the month corresponding to the value of that day is obtained using an if-else statement.
And this method used for leap year why because leap year have 29 days for February month. So
the date count must be changed since in the February month. After get month it will be set to
the Sting month instance variable. This method need value for ‘int days’ parameter.
2. public void longyear(int days)

88 | P a g e
This method used for same purpose of find relevant month. And all code same except in
February month. Because in long year February month have 29 days. So after February month
in the condition of if-else statement, increased by 1.
3. public void print()
This method for print all data in good format. All data get in to this method and it will be the
final output.
4. public void findDetail(int findyear, int finddays, String
votingtype)
This method was used to obtain information on the identity card identity and voter status from
the reserved numbers. Also, to check if the year of birth is a leap year or long year.
In the main method, this main method performs all the functions of obtaining the identity card
number, separating the relevant number of parts from that number, sending values to the
relevant methods and activating those methods.

24 Evaluate the use of an IDE for development of applications


contrasted with not using an IDE
Let’s talk about the features and how it helped to me for write this program by the IDE
1. Codes are auto generating
In the java programming language, there are two types of syntax. 1.pre-defined by language,
and its called keywords. 2.variables that we insert to the program. When we write the program
if we use any keyword IDE will be auto generate itself. I do not need type all text of the word,
just type two or three text and IDE will show the all syntax belongs to that text. So no longer
need to memorized of all keywords, just two or three text enough.

In my codes I have initialize lot of variables methods in my own words. After creating the
methods or variable’s, I need to use it again and again in that case, I need to memorize all the
text of in the words. But because I used one of the IDE, I had not that issue. When I type two or
three text, I had shown all words that I created before by IDE.
89 | P a g e
2. Shortcut for syntax
When I start the program, I need type main method, so I need type long code line. But in IDE
given to me solution for it. It is shortcuts. There are two shortcuts that I used for this program.
For cerate main method, type “psvm” hit Tab

For create output code line, type “sout” hit Tab

90 | P a g e
3. Simple run and test
When I code, I need to clarify my code is correct so for it I need run and test always. So, for it I
need simple and fast way to do that. In IDE I have that feature. In one short cut I can run and
test it.
Right click and click ‘Run’ or press Ctrl+Shift+F10

4. Instantly shows compiler errors


This is very helpful. Because when I write the program errors can be include in one hand from
mistake, other hand from wrong syntax. So, if I used notepad or any text editor for write this
program until compile, I cannot find errors. But in IDE, line by line and word by word it will
show if there are any errors in the program.

91 | P a g e
5. Clear line count maximizes and minimize
When there is a large number of lines of code, the code is difficult to read. The code lines are
then minimized to a certain size to make the code easier to read. That facilities are available in
IDE.

6. Clear syntax formats and colors


This is very useful for seen clear and meaningful code lines in the program.

One more thing, it is the lineup of codes. IDE will auto arrange the code line up.

92 | P a g e
7. Instantly import library files
When I initialized the Scanner for get user input, I need import it from library. So, in addition to
Scanner initialized code, I need type codes for import it. But in the IDE, it will be auto
generating the code for import it.

after press Tab:

93 | P a g e
24.1 If no IDE used?
If I did not used IDE, that is if I used text editor for write that program. That all above facilities,
no to me.
Codes not auto generate, so I need memorized all syntax and there all text. No shortcuts, need
to memorized and type all parts of that main method or output.
Errors are cannot detect without compile. And compilation is to default. Because there are
many steps to do it. There no any formats or colors, so all codes are like same look. So, when
coding it is too difficult to get clear understanding of the codes that what I wrote, what I need
to write. When I initialized the scanner, I need to memorized all syntax for write codes that
import it from library.

94 | P a g e
LO 4

25 What is the coding standard?


Different people do different types of coding in computer coding. Here a program coded by one
person is a bit difficult for another person to understand. The solution to that problem is to put
all the coding patterns together in a common way. It makes it easy for anyone who knows the
common system to recognize those codes. The standard legal framework for programming is
called coding standard.

26 Why coding standard important as a team or individual?


Suppose there is a software development company called A. The company employs twenty
software developers. Do all twenty do the same coding? No. The nomenclature they use is
different. It's not a problem as long as they are developing software individually. But it never
happens individually in an organization. Inevitably, groups and groups come together in large
numbers to produce programs.

 Easy to Maintain, when one program is produced by several people, it is imperative that
one piece of code be easily understood by another. This is because when a part of the
same software is linked to another piece of code, it must be adjusted. Also, if you need
to modify the code, insert new code, or delete an existing code, it is mandatory to have
the same written code.
 Enhanced Efficiency, it is often seen that the software developers spend a larger part of
their time in solving those issues that could have been prevented. Implementing the
coding standards would help the team to detect the problems early or even prevent
them completely. This will increase efficiency throughout the software process.
 Risk of project failure is reduced, because all developers have same pattern same
formatted so, time wasting no more, time wasting is the main thing for failure any
project.
 Minimal Complexity, if code is complex developers will be unable to get maximum
knowledge, skills of themselves, with the minimal complexity developers run with
maximum.
 Bug Rectification, it becomes really easy to locate and correct bugs in the software if the
source code is written in a consistent manner.
 A Comprehensive Look, when writing a large program, it is essential to have a clear view
of the program so that any line of code in any station can be easily identified.
 The cost is minimal, Increasing the efficiency of the developer and reducing the time
taken for the project reduces the cost of the project.
Imagine that you are coding on your own. What are the benefits to you of coding standard or
not?

95 | P a g e
 No clear format of the codes, so you cannot find easily and quickly what are the
statements, loops variables, methods braking points etc.
 Whenever you coding you cannot find even your own keywords, because there is no
specific methodology.
 When you look at your code, it is mesh, so you cannot get your maximum skills or
knowledge to your program.
 If any error in the program you cannot find it short way.
 Time waste, because again and again you have to move among the codes that you
wrote.
 And very important part is, if you are re-code to your previous code or update it, it is
very difficult without using coding standard.
After all these things, if you can use coding standards for the individual project, you can save
your time, mind waste, money. But You can skip some deep points. Because if it is your own
project no one need to understand your project.

27 Benefits of coding standards


1. Gives a clear uniform look to the program.
2. It improves readability and code maintenance and reduces complexity.
3. Minimizes wastage of time
4. Makes codes easier to reuse and identifies errors
5. Efficiency increases because the developer can see a clear understanding and a clear
flow

28 Java coding standards


Two of the most important Java coding standards today are:

 Oracle Code Conventions for the Java TM Programming Language (Revised April 20,
1999): https://www.oracle.com/technetwork/java/codeconvtoc-136057.html
 Google Java Style Guide (Updated on May 22, 2018):
https://google.github.io/styleguide/javaguide.html (rios, 2020)
Nowadays google java style is the famous one. So lets talk about it.

28.1 Standard headers, put into the program.


The header of different modules should follow some standard format and information. The
header format must contain below things that is being used in various companies:

 Name of the module


 Date of module creation
 Author of the module
 Modification history
 Synopsis of the module about what the module does
96 | P a g e
 Different functions supported in the module along with their input output parameters
 Global variables accessed or modified by the module

28.2 Naming conventions for local variables, global variables, constants and
functions
Meaningful and understandable variables name help anyone to understand the reason of using
it.

 Local variable - Local variables should be named using camelCase lettering starting with
small letter (e.g. locVar) whereas Global variables names should start with a capital letter
(e.g. GloVar). Constant names should be formed using capital letters only (e.g. CONSDATA).
 The names of the methods should be written in camelCase starting with small letters. (e.g.,
callMethod)
 Classes must be written in simple letters and it must be start with capital letter. (e.g., Class)

97 | P a g e
28.3 Use understandable comments in necessary places only
When you are coding, apply a comment to the desired locations. Use comment, especially
when calling methods and when using statements or loops. Give a brief and clear idea when
using comments.

28.4 Curly brackets use


When using brackets, open brackets should be in the same row. After opening the brackets, the
code should start writing in the next rule. Once the code is written, the closed bracket should
be applied in a new line. The next code written after the closed bracket must start with a new
rule. But this is different when using an IF-ELSE ladder. In that case, the else keyword and the
corresponding open bracket should be applied in the same line as the closed brackets
corresponding to the if.

98 | P a g e
28.5 About the spaces
In all these cases one empty space must be left between each of the two keywords and
between the keyword and the curly bracket, between the variable and the curly bracket or
semicolon, on both sides of the equivalent sign and on either side of the operator.
When using a statement or loops there should be no empty space at the beginning or end of
the normal brackets that write the condition.
Use one tab space for the all method and the main method. Two-tab spaces for beginning
statements or loops, three-tab space for the method calling lines.
Line needs to be same in the same block of else if statements, and if statement.
specially don’t keep more than one spaces between any cases.

99 | P a g e
Keep the empty line between two methods. After one method closed, next method will start
the after the empty line.

100 | P a g e
28.6 Variable declaration
Every variable declaration (instance or local) declares only one variable: declarations such as int
a, b; are not used.

28.7 The summery fragment


This is very important. This is because their only identification is to know the action taken by a
method or class and what to return from it.

101 | P a g e
(google, 2019)

29 Debugging process
Before go to the debugging, what is the bugs in programming?
Simply put, a bug is an error in a program. When you write a program, there may be some code
inside that does not match the relevant computer-language. Errors can occur if these are
rejected by the relevant language.
There are three types of errors
1. Compile time
2. Run time
3. Logical

29.1 Compile time errors


These errors are errors which prevents the code from compiling, because of error in the syntax
such as missing a semicolon, keyword letters or wrong keywords, at the end of a statement or

102 | P a g e
due to missing brackets, incorrect methods, method not found, etc. These errors will be
detected by java compiler and displays the error onto the screen while compiling. In ide it will
displays as soon as the writing is finished.
e.g

29.2 Run time error


These errors are errors which occur when the program is running. Run time errors are not
detected by the java compiler. It is the JVM which detects it while the program is running.
Mostly run time error come with user inputs.
e.g.

103 | P a g e
29.3 Logical error
These errors are due to the mistakes made by the programmer. It will not be detected by a
compiler nor by the JVM. Errors may be due to wrong idea or concept used by a programmer
while coding. So it will be effort to the program output.
e.g.

30 Debugging process in IntelliJ IDE


There is a method for debugging in any IDE’s, I will use IntelliJ IDE for this program so I will
explain below how to do the dub process in it.
This is often used to find logical errors. When the final result of a written program is not
received correctly, the codes in each line of code should be checked one by one to find the
error. For that, “System.out.println()” print line should be included in every essential line. This is
a very complex and time-consuming process. But due to the debugging facility available in the
IDE, this can be done very easily and efficiently. (s.r.o., 2000-2001)
Let’s see the septs to do that and I’m going to check my program can display birth month
according to the given NIV number correctly.
1. Set the break point
Here I will check the leapYear () loop. So, I kept the break point in the first line of code in the
loop.

104 | P a g e
2. Run the program in debug mode
The program cannot run normally. There is a special icon for that. Click on it.

The console then asked me to enter a value.

When you enter the NIC number and press enter, you will see the debugger panel as shown
below.

3. Read the content


Here you can see the relevant replacement step by step.

105 | P a g e
31 Go to the next step
Really step next mean, the jvm what to do next. That the jvm moving steps that will be taken
here.
Click here to proceed to the next step

As the program progressed step by step, a value was obtained for the month variable at one
point.

106 | P a g e
Importantly, I was now able to see if the NIC number was correct for the month, without
running the entire program. Also, I can use this process to check all the code.
Normally after running a program you can see only the overall result of the program by running
it. Where the internal process in the program cannot be seen as a step. The most suitable
process for this is the debugging process.
After running the above program, you can see the following.

But when the program is executed in debugging mode, you can see the same process that takes
place in the peace process mentioned above. This will reduce the risk of both compilation
errors and runtime errors, as well as logical errors in a program. And when testing a program, it
should be tested in a variety of ways. This process helps to easily identify errors in that test.

107 | P a g e
108 | P a g e
32 Bibliography
NYC, 2019. What Is an IDE?. [Online]
Available at: https://www.codecademy.com/articles/what-is-an-ide
[Accessed 01 03 2021].
ThanoshanMV, 2019. What exactly is a programming paradigm?. [Online]
Available at: https://www.freecodecamp.org/news/what-exactly-is-a-programming-paradigm/
[Accessed 01 03 2021].
unknown, 2018. javaTpoint. [Online]
Available at: https://www.javatpoint.com/c-programming-language-tutorial
[Accessed 01 03 2021].

33 References
NYC, 2019. What Is an IDE?. [Online]
Available at: https://www.codecademy.com/articles/what-is-an-ide
[Accessed 01 03 2021].
ThanoshanMV, 2019. What exactly is a programming paradigm?. [Online]
Available at: https://www.freecodecamp.org/news/what-exactly-is-a-programming-paradigm/
[Accessed 01 03 2021].
unknown, 2018. javaTpoint. [Online]
Available at: https://www.javatpoint.com/c-programming-language-tutorial
[Accessed 01 03 2021].

34 References
Geeks, G. f., 2018. How to write a Pseudo Code?. [Online]
Available at: https://www.geeksforgeeks.org/how-to-write-a-pseudo-code/
[Accessed 06 01 2021].
Software, L., 2019. What is a Flowchart. [Online]
Available at: https://www.lucidchart.com/pages/what-is-a-flowchart-tutorial
[Accessed 06 01 2021].
TEAM, C., 2020. What is a programming language?. [Online]
Available at: https://news.codecademy.com/programming-languages/
[Accessed 02 01 2021].

109 | P a g e
University, T. P. 2. P., 2018. Sequence, Selection, Repetition and Subprogram. [Online]
Available at: https://courses.p2pu.org/en/courses/2544/content/5290/
[Accessed 05 01 2021].
wei, J., 2021. Techopedia - The IT Education Site. [Online]
Available at: https://www.techopedia.com/definition/3739/algorithm
[Accessed 25 01 2021].

110 | P a g e

You might also like