Sri Lanka Institute of Information Technology
Sri Lanka Institute of Information Technology
Faculty of Computing
Dr.Junius Anjana
Introduction
Input :
Task/Process :
Output :
Input :
Task/Process :
Output :
2 Find the summation of two integers input from the keyboard. Output
the result to the screen.
3 Find the area of a circle when the radius is entered from the keyboard.
2 Find the summation of two integers input from the keyboard. Output
the result to the screen.
Consist of 1 s and 0 s
Machine dependent
Computer can directly understand its own machine language
Example :
load salary
add bonus
store total
C, C++, Python, Visual Basic and Java are some of the high level
programming languages.
Example:
total = salary + bonus;
Translator
Assemblers (convert assembly language programs to machine
language)
Compilers (convert high-level language programs to machine
language)
Interpreters (convert high-level language programs to machine
language)
Interpreting
Interpreting is the process whereby the source code file is translated
line by line into machine code.
If syntax errors exist, the program partially executes since the
execution halts as soon as a syntax error is encountered.
In the 2000s, Java became widely used for big business applications,
web apps, and even Android phone apps.
programming language.
2 Translation to bytecode: A java compiler translates your source
it into native machine language code that your specific computer can
follow.
5 Running the Program: The computer follows the native machine
Analyze the given problem, design and write your algorithm first.
(Pseudocode)
A text file with the extension of .java must be created. This file
stores the human readable java program. (Source code)
The .java file / source code name must be the same as the
class name.
Ex. MyFirstProgram.java
Dr.Junius Anjana
The type of data to be stored in the variable is called as the data type.
The type of data to be stored in the variable is called as the data type.
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operator
Increment and decrement operators
Casting
Dr.Junius Anjana
2 External libraries
These are like extra libraries that provide specialized functionality for
specific tasks.
Graphics for games
Connecting to databases
Working with the internet
User-written libraries
Enter the radius of a circle from the keyboard and display the
circumference and the area.
1 Write the relevant pseudocode.
2 Convert it to Java program.
Dr.Junius Anjana
IF statement
This is the basic selection statement to check a condition when making
a decision
If the condition is true, the code block following the IF statement gets
executed
If the condition is false, the code block is skipped
Exercise
Input two integers from the keyboard. Display whether the two integers
are equal.
Write a pseudocode to the above scenario.
Draw the flow chart to the above scenario.
Write a Java program to the above scenario.
Input two integers from the keyboard. Display whether the two integers
are equal or not.
Write a pseudocode to the above scenario
Draw the flow chart to the above scenario
Write a Java program to the above scenario
Dr.Junius Anjana
Exercise
Find the summation and average of five numbers entered through the
keyboard
Write a pseudocode to the above scenario
Convert the above pseudocode to a Java program
Exercise
Prompt the user to enter a series of integers from the keyboard. If the user
enters -1, the program should display ”End of Program” and terminate.
Write a pseudocode to the above scenario
Convert the above pseudocode to a Java program
Dr.Junius Anjana
In a do-while loop, the code block inside the curly braces { } will
execute at least once, even if the condition is initially false.
The condition is checked after executing the code block.
If the condition is true, the loop repeats, and the code block executes
again.
If the condition is false, the loop terminates.
It’s suitable for situations where you want to ensure the code block
executes at least once, even if the initial condition might not be
met.
for loop.
3, 8, 13, 18, 23
2 What does the following code segment print?
Dr.Junius Anjana
Arrays
Array is a data structure which store data items of the same data type
Array store all the data items in a continuous memory location
Arrays can be initialized to any size
Once initialized, the size of an array is fixed and cannot be changed
If need to change the size of an array, you must create a new array
with the desired size and copy the elements from old array to the new
one
Dr.Junius Anjana
Java Methods
a Given a string ”Hello, World!”, how can you determine the number of
characters in the string using an predefined method?
b Given a string ” Hello, World! ”, how can you remove the leading and
trailing whitespace using the trim() method?
c Given a string ”Hello, World!”, how can you access the character at
index 7 (where ‘W’ is located) using the charAt() method?
d Given a string ”HELLO, WORLD!”, how can you convert it to
lowercase using the toLowerCase() method?
Dr.Junius Anjana
Implement a method
Example : Implement a method called calculateArea() to calculate and
return the are of a rectangle.
public: Defines the access modifier and makes the method accessible
from anywhere within the program.
static: Defines the modifier and makes the method belong to the
class itself, rather than to an instance of the class.
Return Type: This specifies the data type of the value that method
returns. If the method doesn’t return a value, use void as the return
type.
Dr.Junius Anjana
Testing