You are on page 1of 43

Programming Fundamentals

What is a Computer?
A computer is an electronic device capable of performing complex computations in a short
time.
A computer is a fast electronic calculating machine that accepts input information, processes
it according to a list of internally stored instructions called a program, and produces the
resultant output information.
A program is a set of instructions for a computer, telling it what to do or how to behave.
Programming is the craft of implementing one or more interrelated abstract algorithms using
a particular programming language to produce a concrete computer program.
Components of a Computer
. !ardware
". #oftware
!ardware Concepts
!ardware consists of devices, li$e the computer itself, the monitor, $eyboard, printer, mouse
and spea$ers. %he hardware processes the commands it receives from the software, and
performs tas$s or calculations.
!ow does the computer wor$?
!ow can a computer show pictures?
!ow can it play sounds?
&ata representation is the conversion of images, letters and sounds to digital electrical
signals. !ere, a digital electrical signal refers to a combination or se'uence of (on) and (o*)
signals.
+ost computers are digital, as opposed to analog, devices.
A digital device wor$s with discrete signals such as , and .
#oftware Concepts
What is software?
#oftware is the name given to the programs that you install on the computer to perform
certain types of activities.
#oftware -asics
Computer program .or (program)/ 0 an organi1ed list of instructions that, when executed,
causes the computer to behave in a predetermined manner. Without programs, computers
are useless.
#upport module 0 an auxiliary set of instructions used in con2unction with the main software
program .example3 dynamic lin$ libraries/.
&ata module 0 contains data .not supplied by the user/ necessary for the execution of certain
tas$s.
Data vs. Software
#ometimes the term (software) is used too loosely and could cause confusion. -efore, the
term (software) is always associated to all non4hardware components of a computer.
!owever, modern de5nitions ma$e it clear that all documents, spreadsheets and even
downloaded materials from the net are now classi5ed as data.
Code Generation
6n computer science, the code generation is a compilation stage that outputs machine code
in the target language.

Programming Fundamentals
Code can appear in a variety of forms. %he code that a programmer writes is called source
code. After it has been compiled, it is called object code. Code that is ready to run is called
executable code or machine code.
6nitially, a programmer writes a program in a particular programming language. %his form of
the program is called the source program, or more generically, source code.
Application Software vs. Sstem Software
#oftware is basically categori1ed into two3 application software and sstem software.
Application software are computer programs that are used to accomplish speci5c or
speciali1ed tas$s for computer users such as creating and editing documents .word
processing/, ma$ing graphic presentations, or listening to mp7 music.
Sstem software are programs that control the basic operations of a computer system such
as saving 5les in a storage device li$e a hard dis$ or 8oppy dis$, printing 5les, accepting
input from a $eyboard or mouse, execute programs, etc.
#oftware &evelopment 9ife Cycle
SD!C is a methodology that is typically used to develop, maintain and replace information
systems for improving the 'uality of the software design and development process.
Phases of #&9C
Planning Phase
%his is the initial stage in the #&9C that has to be performed. %his phase includes the
information about the re'uirements for the proposed software. Also, this phase is $nown as
the feasibility study phase.
Analysis Phase
%his phase re'uires the analyst to thoroughly study the current procedures or software used
to execute tas$s in an organi1ation.
%he main goal in this phase is to identify the re'uirements for new software or simply change
several aspects in the current wor$ing software.
%he activities performed by the analyst during this phase are3
#tudy the current software
&etermine software re'uirements
Write re'uirements reports
&esign Phase
&uring this phase, the developer of the software translates the result of the previous phase
into actual design or speci5cations of the software. &evelopment of the software involves
covering the input and output screens to reports, databases, and computer process.
%he activities performed in this phase are3
6dentify potential solutions
:valuate solutions and select the best
#elect hardware and software
&evelop application speci5cations
;btain approval to implement the new software
6mplement Phase
After the design phase, we must put the proposed software into the test. &uring this phase,
implementing the software will include several steps3
. Coding 0 creation of the actual program
"
Programming Fundamentals
". %esting 0 both programmer and analyst submits the software to various ('uality
testing) to discover if there are any bugs within software
7. 6nstallation 0 after coding and testing is done, the actual software must be installed
and slowly or completely replaces the old software
+aintenance Phase
With every phase being completed, perhaps the maintenance phase would be the most
prevalent phase of all. %here are some bugs in the software which can<t be properly identi5ed
without putting the software into actual use. %he maintenance phase is used to ma$e
necessary patches to remove found errors.
%his is where the software is systematically repaired and improved based on errors or
possible new re'uirements found.
Waterfall #&9C
%he waterfall #&9C suggests that prior to proceeding to the next phase= the current phase
should be 5nished 5rst. -ut applying this strategy in the real world would be unfeasible since
it would be impossible to modulari1e system development.
+odi5ed waterfall and iterative #&9C
%his version of the #&9C is more 8exible compared to the traditional #&9C model. #ince the
nature of developing software is unpredictable, using this model will allow the developer to
ad2ust to certain situations .example3 unforeseen re'uirements or additional features that
are highly needed in the software.
Programming Cycle
-elow are the steps in coming up with a program3
. Problem de5nition 0 speci5es what we want for the computer to do.
". Problem analysis 0 loo$ing at the problem itself= point of view of the computer= start
with the input>output.
7. Algorithm development 0 this is an outline that brea$s problem down into segments,
strategy on how to do the tas$.
?. Coding and debugging 0 remove errors.
Algorithm
An algorithm is a 5nite set of well4de5ned instructions for accomplishing a tas$ which, given
an initial state, will result in a corresponding recogni1able end4state.
For example, a recipe for ba$ing a ca$e is an algorithm.
Characteristics of an algorithm3
#pecify each step or instructor exactly 0 %here must be no ambiguity or the
instructions must be clear.
%here must be a 5nite number of steps 0 %he algorithm must terminate and should
have a stopping point.
%here must be an output 0 %he algorithm must produce the correct result.
7
Programming Fundamentals
Flowchart
A 8ow chart is a graphical representation of a se'uence of operations.
%he following are the elements of a 8owchart3
"low lines # indicated by directed lines to show the direction of data>control 8ow. %he
arrowhead is sometimes not shown when the direction of 8ow is clear. Flow lines are used to
connect bloc$s by exiting from one and entering another.
$erminal bloc% # ;vals or rounded rectangles are used to indicate the start and end of a
module or a program. An oval is labeled with the name of the module at the start. %he end is
indicated by the word end or stop for the top or Control +odule. A start has no 8ow lines
entering it and only one exiting it. ;n the other hand, an end or exit has one 8ow line
entering it but none exiting it.
&nitiali'ation bloc% # %he hexagon is used for declaring > initiali1ing variables. %a$e note3 @ou
can only use variables that had been initiali1ed. An initiali1ation box has one entrance and
one exit.
(rocess bloc% # %he rectangle indicates a processing bloc$, such things as calculations,
opening and closing 5les, and so forth. A processing bloc$ has one entrance and one exit.
&nput)*utput bloc% # %he parallelogram indicates general input and output operations. An 6>;
bloc$s has one entrance and only one exit.
Decision bloc% # %he diamond indicates a decision. 6t has one entrance and exactly two exits
from the bloc$.
Connector # Circles are used for in4page connectors labeled with uppercase letters while
pentagons are used for o*4page connectors labeled with uppercase letters and a number
.typically the page number of the matching pair/. %he circle is used as a connection point
between two sections of a 8owchart that are not ad2acent or closely located to each other.
?
Programming Fundamentals
%hings to $now 5rst3
Aariable is a container of value, it may contain value at a given time.
A variable name must describe its content.
:xample
num B ,
name B (lynssynu2)
letter B C9<
Dse $eywords Eet, 6nput or Fead for input bloc$
Dse $eywords &isplay or Print for the output bloc$
#ample Flowchart
#e'uence
. Create a 8owchart that will display your age G years from now.
G
#tart
ageB H
ageBageIG
Display
age
:nd
Programming Fundamentals
". Create a 8owchart that displays the user<s name, address, age, birthday, gender and
phone number which will follow the sample output below.
Jame3 Kun Fementilla
-irthday3 April L, LH?
Age3 "G
Eender3 +ale
Address3 %agaytay City
Contact Jo.3 ,LMM"G?G"N
:xercises3
. 6nput " numbers and get the sum, di*erence, product and 'uotient.
". Find the area of a rectangle.
7. Find the area of a circle.
?. Find the circumference of a circle.
Conditional #tatements
Conditions are statements that result in a -oolean value. A -oolean Aalue is either a %FD: or
FA9#:.
%wo types of operators result in -oolean Aalues3
+. !ogical *perators # these are mainly used to control program 8ow. %hey are usually
part of an 6F, W!69:, or some other control statements. %he concept of logical
operators is to allow a program to ma$e a decision based on multiple conditions.
,. -elational operators # used to compare two operands and determine the validity of a
relationship.
%here are three basic logical operators3
M
#tart
name, bday, age,
addr, gen, phone
Input
name, bday, age,
addr,gen, phone
Display
(Jame3) name
(-irthday3) bday
(Age3) age
(Eender3) gen
(Address) addr
(Contact Jo.3) phone
:nd
Programming Fundamentals
%he J;% truth table shows the nature of the J;% operator. A truth table is a type of
mathematical table used in logic to determine whether an expression is true or valid. #uch a
table typically contains several rows and columns, with the top row representing the logical
variables and combinations, in increasing complexity leading up to the 5nal function.
%he J;% operator operates on a single value and the result is the reverse of the operand
-oolean value.
Condition A !A
F T
T F
%he AJ& operator is used if you want a strict condition. For AJ& to have true result, both
conditions must be satis5ed. For AJ& to have true result, both conditions must be satis5ed.
%he ;F operator is used when either one of the condition is true.
Condition a Condition b a AND b a OR b
F F F F
F T F T
T F F T
T T T T
Conditional #tatements
Felational operators compare two values and return a -oolean value depending on whether
the test condition is true or false.
6n other programming languages, these are the symbols used for relational operators.
Assume you have variable x and y.
x O y 0 test if x is less than y
x P y 0 test if x is greater than y
x OB y 0 test if x is less than ;F e'ual to y
x PB y 0 test if x is greater than ;F e'ual to y
x B B y 0 test if x is e'ual to y
x QB y 0 test if x is not e'ual to y
x OP 4 test if x is not e'ual to y
Jote3 &o not be confuse with B B .e'ual/ and B .assignment/. %he 5rst one determines if a
variable has the same value as another variable, but the second ma$es a variable become
some value.
Conditional #tatements3 :xercises "
Assume that you have variable R and @. What would be the results of the conditions if we
were to compare them with di*erent values and di*erent relational operators?
X Y X<Y X>Y X<=Y X>=Y X= =Y X!=Y
5 17 T
18 4 F
2 12 F
30 31 T
Conditional #tatements
.ore on Decision /ox
We said that the diamond box is used for decision ma$ing within your 8owchart.
N
x O
",
Programming Fundamentals
6t should be noted that what you place inside the diamond box are conditions. Conditions
that either results to %FD: or FA9#:.
Jote3 @ou don<t place conditions that are in essay form, you must be able to represent it in a
mathematical form.
Single Alternative Selection Structure
A decision box may not necessarily have to do something for both the %FD: path and the
FA9#: path.
F %
Dual Alternative Selection Structure
%he dual alternative structure allows you to perform two di*erent tas$s depending on the
result of the condition<s evaluation. ;ne path is performed if the evaluation resulted in true
and the other if it is false.
0xample1 if x O y, print x, otherwise print y
F %
Conditional #tatements3 :xercises 7
. &raw a 8owchart that will as$ the user to input the values of four variables, A, -, C,
and & and print (%FD:) if A is greater than - and C is less than &. ;therwise do
nothing.
H
6s
variable
x less
than
",?
Programming Fundamentals
%
F
". &raw a 8owchart that will as$ the user to enter a character .either C+< or CF</ to
indicate the user<s gender .+ale or Female/. &isplay (+ale) if the user enters the
value of C+< and (Female) for the value of CF<. .Assume all the inputs are correct./
F %
L
#tart
Input
gen
gen
gen
BB
C+<
Outpu
t
(+A9:)
:nd
Outpu
t
(F:+A9
:)
#tart
Input
A, -, C, &
A, -, C, &
AP- and
CO&
Output
(%FD:)
:nd
Programming Fundamentals
:xercises3
. Accept person<s name and age then determine if he>she is old enough to vote.
". Create a 8ow chart that will increase the value of the number by G if it is greater than
, and display the result, otherwise do nothing.
7. &raw a 8ow chart for college admission oSce. Allow the user to input the student<s
EWA from high school and Admission test score. Print message (Accept) if the student
has a EWA of H, or above and admission test of at least M,. 6f the student doesn<t
meet either of the 'uali5cation criteria, print (Fe2ect).
?. Create a 8ow chart that will increase the value of the number by , if it is less than ",
and display the result. ;therwise increase the value of the number by ", and display
the result.
.ultiple Decision /oxes
+ultiple decision boxes can oftentimes be combined into a single box. %here are times,
however, when you have to be very speci5c with the conditions that you have to put in. %his
is especially true in cases when di*erent actions need to be performed depending on various
conditions.
0xample1
Create a 8owchart that as$s the user to enter a number from 0 7. 6f is entered, print
(!ello), if " is entered, print (!i), and if 7 is entered, then print (-ye). ;therwise, print
(6nvalid).
,
Disp
Hello
Get x
x==1
?
Disp Hi
Disp Bye
Disp
Invalid
Stop
x==2
?
x==3
?
x = 0
Start
Programming Fundamentals
2sing Decision /ox with -anges
-usiness programs often need to ma$e selections based on a variable falling within a range
of values. %o perform a range chec$, use a se'uence of decision bloc$s starting with either
the lowest or highest value in each range of values to ma$e your selections.
0xample1
Create a 8owchart that would print the name of the supervisor under a certain department
number.
&epartment Jumber #upervisor
0 7 +r. R
? 0 N +r. @
H 0 L +r. T
+
st
Solution
,
nd
Solution

Disp
r! "
Stop Dept
#=$?
Disp
r! %&
Disp
r! '
Start
Dept(0
Get
Dept
Dept
#=3?
Programming Fundamentals
:xercise3
&raw a 8owchart that will print the student<s letter grade given the following speci5cations3
L, 0 ,, B A
H, 0 HL B -
N, 0 NL B C
M, 0 ML B &
-elow 0 M, B F
Assume that the grades are integers.
9ooping Constructors
"
Start
Dept(0
Get
Dept
Dept
)=*?
Dept
)=+?
Disp
r! %&
Disp
r! '&
Disp
r! "&
Stop
Programming Fundamentals
A loop is a set of program instructions that executes repeatedly until a condition is satis5ed.
%he chart shows the basics of looping. 6t tells the nature of loop and how it wor$s.
:xample3 G O H will always be true= however, using x O H the result will be dependent on the
value of x. 6n programming, the value of the variable can be changed in 7 basic ways3
. through mathematical computations .ex3 #et x to x I 7 or 6ncrement x by 7/
". user input .enter x/
7. by initiali1ation or passing of values .ex3 x B ?, y B x/
-epetition Structure
Also referred to as a loop or iteration, this type of structure repeats one or more instructions
until a certain condition is met.
%he action performed by a repetition structure must eventually cause the loop to terminate.
;therwise, an in5nite loop is created.
@es
Jo
7
,un
-ondition
al
Stop
.aria/l
e
0rue
1als
e
2 3 ( 4
5
Initiali6atio
n
7oop
8ser
Input
% #
'?
9ro:ess
;
% #
'?
Programming Fundamentals
@es
Jo
%his type of structure is $nown as a pre # test repetition structure. %he condition is tested
-:F;F: any actions are performed

A post3test repetition structure always performs its actions at least once.
%he condition is tested AF%:F the actions are performed.
@es
Jo
9ooping Constructs3 :xample
Creating a 8owchart that will display the following result3 .Assume that the variable is
initiali1ed to G./
*utput1
G
?
7
"

&one
?
Display
%
Display
%
% = % 2
1
% #
'?
Start
x = <
Display
%
% = % 2
1
% #
'?
Programming Fundamentals
@es
Jo
9ooping Constructs3 :xample "
Create a 8owchart that will print a series of numbers based on what was entered by the user.
4umber 0ntered1 ?
Sample *utput1

"
7
?
&one
9ooping Constructs3 :xample 7
Create a 8owchart that will print the sum of all even numbers from 0 ,.
G
% )
0?
Display
%
Disp
Done
% = % ( 1
Stop
Start
%=0 =
'
Get '
% #
'?
Disp
Done
Stop
Disp %
% = % 2
1
Start
S8=
-0, =
0
-0, #
10?
-0, = -0, 2
2
S8 = S8 2
-0,
Disp S8
Stop
Programming Fundamentals

@es
no
Jo
:xercises3
. Create a 8owchart that will print the sum of all odd numbers from 0 ,.
". Create a 8owchart that will print your name G times.
9ooping Construct 0 +ultiple 9oops
6t is possible to have a loop. 6n case of multiple loops, it is critical to determine what
condition will terminate the whole 8owchart. +ultiple loops are useful if there is a need to
perform repetitive tas$s.
;ne use of multiple loops is when there is a need to perform user input validations. For
example, suppose you will let the user enter a number except , or a negative number.
0xample1
Create a 8owchart that as$s the user to enter a gender. &etermine if the user presses either
C+< or CF<, if C+< display (+A9:), or if CF< display (F:+A9:). ;therwise, display (6nvalid
Eender) and let the user reenter a gender. Fepeat the whole process G times.
M
0>is
:ondi
tion
?ill
:aus
e t>e
loop
to
stop!
Disp
ale
Start
Gen=
-tr =
1
-tr #=
<
Stop
Get Gen
Disp
Invalid
Gen ==
@A
Or
Gen == @1A
Gen==A
A
Disp
1eBale
-tr = -tr21
Programming Fundamentals
:xercise3
Create the post4test design of the previous example.
Pseudocodes
4 6s a $ind of structured :nglish for describing algorithms. 6t allows the designer to
focus on the logic of the algorithm without being distracted by details of language
syntax.
4 is a short hand way of describing a computer program. Father than use the speci5c
syntax of a computer language, more general wording is used.
Common Action Ueywords
&nput1 Fead, ;btain, Eet
N
Programming Fundamentals
*utput1 Print, &isplay, #how
Compute1 Computer, Calculate, &etermine
&nitiali'e1 #et, 6nit
Add one1 6ncrement
Subtract1 &ecrement

%he structured part of pseudocde is a notation for representing six speci5c structured
programming constructs3
A. #:VD:JC: a linear progression where one tas$ is performed se'uentially after
another.
Sample problem +1 Create a pseudocde that will display your age G years from now.
+
st
(seudocode ,
nd
(seudocode
6nit ageBH read age
Compute age as age plus G Compute age as age plus G
&isplay age &isplay age
Sample problem ,1 Create a 8owchart that displays the user<s name, address, age,
birthday, gender and phone number which will follow the sample output below.
Jame3 Kun Fementilla
-irthday3 April L, LH?
Age3 "G
Eender3 +ale
Address3 %agaytay City
Contact Jo.3 ,LMM"G?G"N
(seudocode1
6nput name, addr, bday, age, gen, phone
&isplay (Jame3) name
(-irthday3) bday
(Age3) age
(Eender3) gen
(Address3) addr
(Contact Jo.3) phone
-. 6F %!:J :9#: is a decision .selection/ in which a choice is made between two
alternative course of action. -inary choice on a given -oolean condition is indicated
by the use of four $eywords3 6F, %!:J, :9#: and :J&6 F.
%he general form is3
6F .condition/ %!:J
#e'uence
:9#:
#e'uence "
:J& 6F
H
Programming Fundamentals
%he :9#: $eyword and (se'uence ") are optional. 6f the condition is true, se'uence
is performed, otherwise se'uence " is performed.
#ample Problem 3 Create a pseudocode that will as$ the user to input the values of
four variables, A, -, C, and & and print (%FD:) if A is greater than - and C is less than
&. ;therwise do nothing.
Pseudocode3
6nput a, b c, d
6f a is greater than - and C is less than & %hen
&isplay (%FD:)
:nd 6f
#ample Problem "3 Create a pseudocode that will as$ the user to enter a character
.either C+< or CF</ to indicate the user<s gender .+ale or Female/. &isplay (+ale) if the
user enters the value of C+< and (Female) for the value of CF<. .Assume all the inputs
are correct./
Pseudocode3
6nput gen
6f gen is e'ual to C+< %hen
&isplay (+ale)
:lse
&isplay ( Female)
:nd 6f
C. CA#: is a multi4way branch .decision/ based on the value of an expression. CA#: is a
generali1ation of 6F4%!:J4:9#:.
A CA#: construct indicates a multi4way branch based on conditions that are mutually
exclusive.
Four $eywords, CA#:, ;F, ;%!:F#, and :J&CA#:, and conditions are used to indicate
various alternatives.
%he general form is3
CA#: expression ;F
condition 3 se'uence
condition "3 se'uence "
W
condition n3 se'uence n
;%!:F#3
default se'uence
:J&CA#:
%he ;%!:F# clause is its default se'uence is optional. Conditions are normally numbers or
characters indicating the value of (expression), but they can be :nglish statements or some
other notation that speci5es the condition under which the given se'uence is to be
performed. A certain se'uence maybe associated with more than one condition.
Sample (roblem+1 Create a pseudocode that as$s the user to enter a number from 0 7. 6f
is entered, print (!ello), if " is entered, print (!i), and if 7 is entered, then print (-ye).
;therwise, print (6nvalid).
L
Programming Fundamentals
(seudocode1
:nter num
Case num ;f
3 Print (!ello)
"3 Print (!i)
73 Print (-ye)
;thers
Print ( 6nvalid number)
:nd Case
Sample problem ,1 Create a pseudocode that accept letter grade and display the
corresponding remar$s below.
A B :xcellent C B #atisfactory
- B Eood & B Poor
(seudocode1
6nput grade
Case grade ;f
A3 &isplay ( :xcellent)
-3 &isplay ( Eood (
C3 &isplay ( #atisfactory (
&3 &isplay ( Poor (
:nd Case
&. W!69: is a loop .repetition/ with a simple conditional test at its beginning.
%he W!69: construct is used to specify a loop with a test at the top. %he beginning and
the ending of the loop are indicated by two $eywords W!69: and :J&W!69:. %he
general form is 3
W!69: .condition/
#e'uence
#e'uence"
W..
#e'uence n
:J&W!69:
%he loop is entered only if the condition is true. %he (se'uence) is performed for each
iteration. At the conclusion of each iteration, the condition is evaluated and the loop
continues as long as the condition is true.
Sample (roblem+1 Create a pseudocode that as$s the user to enter , numbers then display
the sum.
(seudocode1
Set sum to 5
Set ctr to 5
While ctr 67 +5
&nput num
",
Programming Fundamentals
Compute sum as sum plus num
&ncrement ctr
0nd While
Displa sum
Sample problem ,1 Create a pseudocode that as$s the user to enter , numbers then
display the average.
(seudocode1
Set sum to 5
Set ctr to 5
While num 67 +5
&nput num
Compute sum as sum plus num
&ncrement ctr
0nd While
Compute average as sum divided b ctr
Displa average
:. F:P:A%4DJ%69 is a loop with a simple conditional test at the bottom.
%his loop is similar to the W!69: loop except that the test is performed at the bottom
of the loop instead of at the top. %wo $eyword, F:P:A% and DJ%69 are used. %he
general form is 3
F:P:A%
#e'uence
#e'uence "
W..
#e'uence n
DJ%69 .condition/
%he (se'uence) in this type of loop is always performed at least once, because the
test is performed after the se'uence is executed. At the conclusion of the loop repeats
if the condition is false. %he loop terminates when the condition become true
Sample (roblem+1 Create a pseudocode that as$s the user to enter , numbers then display
the sum.
(seudocode1
Set sum to 5
Set ctr to +
-epeat
&nput num
Compute sum as sum plus num
&ncrement ctr
2ntil 8num 9 +5:
Displa sum
"
Programming Fundamentals
Sample problem ,1 Create a pseudocode that as$s the user to enter , numbers then
display the average.
(seudocode1
Set sum to 5
Set ctr to +
-epeat
&nput num
Compute sum as sum plus num
&ncrement ctr
2ntil 8num 9+5:
Compute average as sum divided b ctr
Displa average
F. F;F is a speciali1ed construct for iterating a speci5c number of times, often called a
(counting) loop.
%wo $eywords, F;F and :J&F;F are used.
%he general form is3
F;F .iteration bounds/
#e'uence
#e'uence "
W..
#e'uence n
:J&F;F
Sample (roblem+1 Create a pseudocode that as$s the user to enter , numbers then display
the sum.
(seudocode1
Set sum to 5
"or 8ctr starts counting from + to +5:
&nput num
Compute sum as sum plus num
&ncrement ctr
0nd "or
Displa sum
Sample problem ,1 Create a pseudocode that as$s the user to enter , numbers then
display the average.
(seudocode1
Set sum to 5
"or 8crt starts counting from + to +5:
&nput num
Compute sum as sum plus num
&ncrement ctr
0nd "or
""
Programming Fundamentals
Compute average as sum divided b ctr
Displa average
E. J:#%:& C;J#%FDC%#
%he construct can be embedded within each other, and this is made clear by use of
indenting. Jested constructs should be clearly indented from their surrounding constructs.
:xample
SET total to zero
REPEAT
READ Temperature
IF Temperature > Freezing THEN INCREMENT total
END IF
UNTIL Temperature < zero
Print total
6n the above example, the 6F construct is nested within the F:P:A% construct, and
therefore is indented.
Sample (roblem+1 Create a pseudocode that as$s the user to enter , numbers, if the
number is greater than 7, compute the sum otherwise compute the product then display the
sum and product.
(seudocode1
Set ctr to +
Set sum to 5
Set product to 5
-epeat
&nput num
&f 8num9 ;5:
Compute sum is e<ual to sum plus num
0lse
Compute product is e<ual to product times num
2ntil8crt9+5:
Displa =$he sum is1 = sum
Displa =$he product is1 = product
What is Kava?
"7
Programming Fundamentals
A hih!"#$#" %&o&a''in "an(a# d#$#"o%#d b) *(n +i,&o-)-t#'-. /a$a 0a- o&iina"") ,a""#d OAK, and 0a-
d#-in#d 1o& handh#"d d#$i,#- and -#t!to% bo2#-.
Kava is an ob2ect4oriented language similar to CII, but simpli5ed to eliminate language
features that cause common programming errors.
Kava is a general purpose programming language with a number of features that ma$e the
language well suited for use on the World Wide Web.
Applets and Applications
%here are two $inds of programs you can write with the Kava language and these are3
Kava Applets. Kava applets are small programs that the travel with !%+9 code and
execute on the Web user<s computer.
Kava Applications. Kava applications are complete stand4alone programs that do not
re'uire another application to execute.
Kava Program Cycle
Kava source code 5les .5les with a .java extension/ are compiled into a format called
btecode .5les with a .class extension/, which can then be executed by a Kava interpreter.
Compiled Kava code can run on most computers because Kava interpreters and runtime
environments, $nown as >ava ?irtual .achines 8?.s:@ exist for most operating systems,
including DJ6R, the +acintosh ;#, and Windows. -ytecode can also be converted directly
into machine language instructions by a 2ust4in4time compiler .K6%/.
Kava Program
A minimum Kava program has the following format3
public class Oclass nameP
X
public static void main .#tringYZargs/ X
Oprogram statementP
[
[
"?
Programming Fundamentals
6n this course, we are interested in the basic programming so the students need not
understand the meaning of each declaration. Kust note that3
. %he items in angle brac$ets are placeholders. %hese must be replaced with whatever
is appropriate for the program .without the angle brac$ets/.
a. %he Oclass nameP is an identi5er for the program which should be the same as
the 5le name. Fules for identi5ers will be discussed later.
b. %he Oprogram statementP is a series of Kava statements to be discussed later.
". %here is the minimum of " pairs of curly braces 0 one after Oclass nameP surrounding
everything else, and another surrounding the Oprogram statementsP.
7. args are available which maybe renamed according to Kava rules on variables name. At
this point, 2ust use it as is.
Kava Conditioning Euidelines
-elow are the guidelines to be followed when coding in Kava3
. Kava program 5les must
!ave same name as public class
:nd with extension.2ava
". Dse with comments for documentation and readability
7. Whites spaces are ignored
?. 6ndent for readability
Kava #tatements
A statement speci5es one or more actions to be performed during the execution of a
program. 6n Kava, a statement is one or more lines of code terminated by a semicolon.
For example3
#ystem.out.print. (!ello, WorldQ) /=
Kava -loc$s
6n Kava, statements can be combined to form a statement bloc$. A bloc$ is formed by
enclosing statements by curly braces.
Kava Comments
Comments are used to add notes and documentation to source codes. :verything
mar$ed as comment is ignored by the compiler.
%here are three types of comments in Kava 0 bloc$, end4of4line, and Kava&oc
comments.
. A bloc$ comments is used to provide descriptions of 5les, methods, data structures
and algorithms. 6t starts with a forward slash and asteris$ .>\/ and ends with an
asteris$ and forward slash .\>/.
". A line comment starts with two forward slashes .>>/ and ends at the end of the line
the slashes appear on.
7. A Kava&oc comment is used speci5cally to document classes, attributes, and
methods. 6t starts with a forward slash and two asteris$ .>\\/. 6t ends with one
asteris$ and a forward slash .\>/. Jote that the Kava&oc comment is 2ust a special
case of the bloc$ comment and can be used even if you do not intend to use
Kava&oc.
"G
Programming Fundamentals
Kava 6denti5ers
6denti5ers are the words that are used by the programmer to properly identify or name
a class, procedure, method, or variables.
Kava 6denti5er Fules and Euidelines
%he following are some rules and guidelines on how to name an identi5er3
Fules3
6denti5ers can use alphabetic characters, both lower and upper case .a41 and A4
T/, numbers .,4L/, underscores .]/, and dollar signs .^/.
6denti5ers cannot start with a number.
Ueywords cannot be used as 6denti5ers .for reason $eywords are sometimes
called reserved words/.
Euidelines3
Jame your 6denti5er close its functionality.
+ethod and variable names start in lowercase while classes start in uppercase.
For multi4word identi5ers, either use underscores to separate the words,
capitali1e the start of each words.
Avoid starting the identi5ers using the underscore.
Kava Ueywords
A $eyword is a 5xed set of symbols built into the syntax of the language.
#ince 2ava is case4sensitive, changing the case of the $eyword will allow its use an identi5er.
#ample $eywords3
class public void main
int for while new
&ata %ypes
&ata types represent di*erent set of data values and determine which standard operators
can be applied.
%here are two $inds of data types3
+. #imple data types are the built4in or primitive Kava data types.
(rimitive data tpes1
-oolean. boolean/ #hort .short/ -yte .byte/
6nteger .int/ 9ong .long/ Character .char/
&ouble .double/ Floating Point .8oat/
,. Composite or class data types are created by the programmer using simple types,
arrays, classes and interfaces.
Sample of (redeAned Composite Data tpe
toDpperCase. / to9owerCase. / #tring
length. / e'uals. /
"M
Programming Fundamentals
Primitive &ata %ypes
Primitive data types are the building bloc$s and are in essential part of a program.
. -oolean &ata type 4 Kava<s -oolean data type is one4bit wide and ta$es on the values
true or false. %he initial value of a -oolean variable is false.
". Character &ata type4 Kava uses a M4bit data type called char to represent text
characters.
7. 6nteger &ata type4 %he integer types are for numbers without fractional parts.
Jegative values are allowed. Kava provides the four integer types int type is the most
practical, for greater number of digits you need to resort to a long. %he bte and
short are mainly intended for speciali1ed applications, such as low4level 5le handling.
?. Floating Point 4 Another type of number that can be stored is a 8oating4point number,
which has the type Boat or double. Floating4point numbers are numbers with a
decimal point. %he name double refers to the fact that these numbers have twice the
precision of the 8oat type.
Kava Aariables
Aariables are places in memory that store values that are generated in a program.
&eclaring and 6nitiali1ing Aariables
6n Kava, variables are declared via statements of the form
Odata typeP OnameP Bnitial value4=
:xample3
int yourAge B L=
6n the given example, the variable yourAge is declared and at the same given an initial value
of L.
-elow are the guidelines in initiali1ing variables3
. initiali1e your variables as you declare them if needed.
". Dse descriptive names for your variables.
7. &eclare one variable per line of code.
&isplaying Aariable &ata
6n Kava, we can output the value of a variable using the following commands3
S!tem"out"print# $
S!tem"out"println# $
%he given example illustrates how these two commands are used in a program.
public class Aar;utput X
public static void main .#tringYZ args/X
int value B ,=
char 4 x=
x B<A C=
#ystem.out.println.value/=
#yatem.out.println.( the value of x B) I x/=
"N
Programming Fundamentals
[
[
%he program will output the following text on the screen3
,
%he value of x B A
%ypes of Aariables
%here are two types of variables in Kava and these are3
. Primitive &ata Aariables these variables store data in the actual memory location of
where the variable is.
:xample443 int i B ",
int 2 B G=
". Feference variables are variables that store the address in the memory location.
A variable that does not have the value with it , but it refers to another variable
for the values .
:xample443 int i,2=
iBG=
2Bi=
6f you are using the ob2ect 2 in a class and accessing a 5le from another class
then you have to ta$e the help of the &;%../ operator.... !ere _2_ is the reference
variable and i is the value variable.
Constants
Constants are refer to those whose value does not change. %he $eyword 5nal is used to
modify a variable declaration to declare a constant . For :xample,
public class Variables {
public static void main (String[] args) {
final double PI 3.!"
#
#
%ype Casting
We have data types such as int,8oat ,char,double. int\int gives int only. similarly 8oat\8oat
gives 8oat only.
-ut in some cases for example .in divison/ ?>7 gives 8oat .-ut ? and 7 are integers. 6n such
cases we have to change data type of ints temporarily as 8oats. %his temporary conversions
of one data type to another is called type casting.
Casting means changing. %ype casting gives the meaning of changing the data type.
int donut$ount% mil&$ount"
double donutsPer'lass"
S(stem.out.print()*nter number of donuts+ ))"
donut$ount,Integer.parseInt(&.read-ine())"
S(stem.out.print()*nter number of glasses of mil&+ ))"
"H
Programming Fundamentals
mil&$ount,Integer.parseInt(&.read-ine())"
if(mil&$ount. ){
S(stem.out.println()/o 0il&1))"
S(stem.out.println()'o bu( some mil&))"
#
else{
donutsPer'lass,donut$ount2(double)mil&$ount"
S(stem.out.println(donut$ount3 ) donuts.))"
S(stem.out.println(mil&$ount3) glasses of mil&))"
S(stem.out.println()4ou 5ave )3 donutsPer'lass3) donuts for
eac5 glass of mil&.))"
#
Kava :xpressions
An expression produces a result and returns a value.
%he following are the purposes of expressions3
to compute values
to assign values to variables
to control the 8ow of execution
-elow are common examples of mathematical expressions3
% & '(
& %(
z & % ) (

Kava ;perators
;perators are special symbols that perform some action on its operands and return the
result.
Arithmetic
6ncrement and &ecrement
Assignment
Felational
9ogical
Assignment ;perators
%he simplest assignment operator is the standard e'uals .B/ sign operator. For example, x B
yI7 evaluates the expression yI7 and assigns the result to x.
"L
Programming Fundamentals
%he other Kava assignment operators are $nown as arithmetic assignment operators.
3 = Add and a--in o%#&ato&
! = *(bt&a,t and a--in o%#&ato&
4 = +("ti%") and d#-in o%#&ato&
5 = Di$id# and a--in o%#&ato&
6 = R#'aind#& and a--in o%#&ato&
6ncrement and &ecrement ;perators
%he I I operator increments its single operand, which must be a variable, by one. %he
behavior of this operator depends on its position relative to the operand.
%he 4 4 operator decrements its single numeric operand, which must be a variable by one.
%he behavior of this operator depends on its position relative to the operand.
Felational ;perators
Felational operators are used to create comparison expressions that result in a -oolean
value .either true or false/. Kava provides the following six relational operators.
B B is e'ual to
O is less than
P is greater than
O B is less than or e'ual to
P B is greater than or e'ual to
Q B is not e'ual to
9ogical ;perators
:xpressions that result in -oolean values, such as comparison operations, can be combined
to form one or more complex expressions. %his is handled through logical operators, which
are used for logical combinations AJ&, ;F, R;F, and logical J;%.
For AJ& combinations the ` or `` logical operators are used.
For ;F combinations, the a or aa logical operators are used. %hese combined expressions
return a true value if either -oolean expression is true.
%he R;F combination has one logical operator.b. %his result in a true value only if both
-oolean expressions it combines have opposite values .
%he J;% combination uses the Q logical operator followed by a single expression.
7,
Programming Fundamentals
;perator Precedence
When more than one operator used in an expression, Kava has an established precedence
hierarchy to determine the order in which operators are evaluated.
Kava #tatements
#tatements are the tas$s you wish your program to do. #tatements are your commands and
may be comprised of $eywords, 6denti5ers, and >or mathematical expressions. 6t generally
contains expressions .expressions have a value/.
#tatements in Kava can either be simple statement or compound statements. #imple
statements are the basic building bloc$s of a program. Compound statements are used to
organi1e simple statements into complex structures, which are called control structures since
they control the order in which the statements are executed.
Kava Control #tructures
Control structures allow a programmer to change the ordering of how the statements in
programs are executed. %here are two types of control structures and these are3
&ecision control structures 0 allows a programmer to select speci5c sections of code to
be executed.
Fepetition control structures 0 allows a programmer to execute speci5c
sections of the code a number of times.
&ecision Control #tructures
$hese structures include tpes such as &f@ &f30lse@ and &f30lse3if.
&f Structure
%he 6f statement speci5es that a statement .or bloc$ of code/ will be executed if and
only if a certain -oolean statement is true. 6t has the format 3
i* #e%pre!!ion!$
grammar !tatement or +lo,-(
.R
i* #e%pre!!ion$ !tatement(
6f there is a need to execute several statements .compound statement/.then left right X [
braces can group these statements .
I* #e%pre!!ion$
/
!tatement 0(
!tatement 1(
2
7
Programming Fundamentals
&f3 0lse Structure
%he if4else statement is used when you want to execute statement>s if a condition is true and
another statement if the condition is false. 6t has the format3
i* #e%pre!!ion!$
!tatement0(
el!e
!tatement 13
or
i* #e%pre!!ion$ !tatement 0(
el!e !tatement1(
:xercises3
9et the students to perform the following exercises after discussing the 6f4:lse #tructure.
. %est if the value of the variable count is greater than ,. 6f it is, print Count is greater
than , , otherwise, print Count is less than , .
". %est if the value of the variable number is an odd number . 6f it is, print %his number is
not an even number ,otherwise print %his number is an odd number .
7. %est if the value of the variable age is greater than H, and less than or e'ual to ?,. 6f
it is, print @ou<re not getting oldQ otherwise, print @ou<re not getting younger .

&f3 0lse3&f Structure
%he if4else 0if statement is used when you want to execute statement>s if a condition is true
and test another condition if the previous condition is false. 6t has the format3
i* #e%pre!!ion!$
!tatement0(
el!e i* #e%pre!!ion$
!tatement 1(
el!e i* #e%pre!!ion$
!tatement 4(
"
"
"
el!e !tatement1(
7"
Programming Fundamentals
#witch #tructure
Another way you can add decision4ma$ing code to your programs is with the switch
statement. %he switch statement enables a computer program to switch between di*erent
outcomes based on a given expressions.
%he general format of #witch statement is3
!5it,6 #!5it,67e%pre!!ion$
/
,a!e 08
!tatement0(
,a!e18
!tatement 1(
,a!e n8
!tatementn(
9e*ault8
2
!5it,6 #!5it,67e%pre!!ion$
/
,a!e 08
!tatement0(
+rea-(
,a!e18
!tatement 1(
+rea-(
,a!e n8
!tatementn(
+rea-(
9e*ault8
2

:verytime you encounter a switch statement,
. Kava 5rst evaluates the switch]expressions, and 2umps to the case whose
selector maches
". %he program executes the statements in order from that point on until a brea$
statement is encountered, s$ipping then to the 5rst statement after the end of
the switch structure.
7. 6f none of the cases are satis5ed, the default bloc$ is executed. !owever, note
that the default part is optional.
%a$e note of the following in a switch statement3
77
Programming Fundamentals
o Dnli$e with the 6f statement, the multiple statements are executed in the switch
statement without needing the curly braces.
o When the case in a switch statement has been matched, all the statements associated
with that case are executed. Jot only that, the statements associated with the
succeeding cases are also executed.
o %o prevent the program from executing statements in the subse'uent cases, use a
brea$ statement as the last statement.
Fepetition Control #tructures
Fepetition control structures are Kava statements that allow a programmer to execute the
speci5c bloc$s of code a number of times.
%hese structure include types such as W!69:, &;4W!69: and F;F.
While #tructure
%he while loop is a statement or bloc$ of statements that is repeated as long as some
condition is satis5ed. &epending on the -oolean expression, the loop may not be executed at
all.
%he general format of a while statement is 3
56ile #+oolean7e%pre!!ion$
/
!tatement0(
!tatement1(
2

&o4While #tructure
A do4while loop is similar in structure to a while4loop, except in its upside4down appearance,
as the condition appears after the body. %he statements inside a do4while loop are executed
repeatedly as long as the condition is satis5ed.
%he general format of a do4while statement is3
9o
/
Statement0(
Statement1(
:
7?
Programming Fundamentals
2 56ile #+oolean7e%pre!!ion$(
For #tructure
A for loop instructs a program to perform a bloc$ of code a speci5ed number of times. %here
are many applications for a for loop, including tas$s such as reading through a list of data
items or initiali1ing an array.
%he general format of a for statement is3
*or #initialization( ,on9ition( in,rement$;
/
Statement 0(
Statement 1(
:
2
%he for construct has three parts3 the initiali1ation section, condition section, and the
increment section.
%he initiali1ation section is used to initiali1e the loop variable.
%he condition section compares the loop variable to some limit value.
%he increment section updates the loop variable.
Arrays
Programming languages provide the capability to use one variable to store a list of data of
the same type, and manipulate these more eSciently. %his type of variable is called an array.
A data structure is a way of storing data in a computer so that it can be used eSciently.
An array is a se'uence of memory locations for storing data organi1ed in such a way that any
one of the individual locations can be accessed by providing its index number. 6n Kava, the
index number starts at 1ero. %he individual items within an array are called elements. %he
elements are referenced by their index using the subscript operator Y Z.
For example
6ndex3 , "
number 3 " 7
&eclaring Kava Arrays
An array variable is declared the same way that any Kava variable is declared. %he
declaration of an array in Kava simply allocates memory to contain a reference to the array.
#'uare brac$ets Y Z are used to modify the variable declaration.
%he s'uare brac$ets may also be attached to the variable to declare that the variable is an
array variable, as in
For example
int x Y Z=
int ages Y Z =
7G
Programming Fundamentals
;r :'uivalent ly
int Y Z x=
int Y Z ages=
@ou can<t store di*erent data types in a single array. @ou can have an array of integers, or an
array of strings, or an array of arrays, but you can<t have an array that contains, for example,
both string and integers.
6nitiali1ing Array Aariables
;nce an array variable has been declared, memory must then be allocated or assigned to it
before data can be stored in the array. %his is done by initiali1ing the array variable using the
operator. %he new operator is followed by the data type with the number of elements to
allocate speci5ed. %he number of elements to be allocated is placed within the Y Z.
For example
name B new #tring Y,,Z=
age B new int Y ,Z
%his, in e*ect, (creates) the array for data storage. Jote that the (array) is di*erent from the
(array variable (. %he array stores the data while the (array variable (identi5es the array.
9i$e other variables, an array variable can be initiali1ed when it is declared.
:lement %ype Y Z arrayJame B new :lement %ype Y#i1e ;f ArrayZ=
;F
:lement %ype arrayJameY Z B new :lement %ype Y#i1e ;f ArrayZ=
For example
int Y Z x B new int Y , Z
int Y Z ages B new int Y ,,Z=
@ou can also create an array by directly initiali1ing it with data. 6n this case, the data should
be enclosed in curly braces and separated by commas. %he length of the array is simplicity
de5ned by the number of elements included within the X [.
For example
int Y Z arr B X, ", 7, ?, G[=
;r
int arr Y Z B X, ", 7, ?, G[=
%his statement declares and creates an array of integers with 5ve elements, and initiali1es
this with the values , ", 7, ? and G.
;nce the array is created, you cannot ma$e more space in the array
%he following are examples of creating arrays.
. Creates an array of boolean variables with identi5er results. %his array contains ?
elements that initiali1ed to values Xtrue, false, true, false[.
-oolean Y Z results B Xtrue, false , true , false[=
". Creates an array of ? double variables initiali1ed to the values X,,, L,, H,,NG.G[.
doouble YZ grades B X,,, L,, H,, NG.G[=
7M
Programming Fundamentals
7. Creates an array of strings with identi5er days and initiali1ed. %his array contains N
elements.
#tring YZ days B X(+on), (%ue), (Wed), (%hu), (Fri), (#at), (#un)[=
Accessing an Array :lement
@ou can access the elements of a Kava array by treating it as a one argument function, where
the argument is the index or subscript.
6ndex number or subscript
Assigned to each member of the array, to allow the program to access an individual member
of the array
An integer beginning at 1ero and progresses se'uentially to the end of the end of the array
Jote3 index is from , .si1e;fArray4/
For example, given the array we declared, we have
>>assigns , to the 5rst element in the array
ages Y,Z B ,=
>>print the last element in the array
#ystem.out.print .agesYLLZ/=
;nce an array is declared, the stored value of each member of the array will be initiali1ed to
1ero for number data.
Coding Euidelines
6t is better to create the array right after you declare it.
int Y Z arr B new int Y ,,Z
%he elements of an n4element array have indexes from , to .
Jote that there is no array element arrYnZQ %his will result in an array4index4out4of4bounds
exception.
@ou cannot resi1e an array. !owever, you can create a new array of a di*erent si1e, copy the
desired data into the new array, and assign the new array to the array variable.
Array 9ength
When you create an array, you specify the number of elements. #ubse'uently, the array
$eeps this information. @ou can access the number of elements using the constant length
attached to the arrayJame by a dot, as in arrayJame.length. for example, if you have an
array called employeeJames, the following code sets an int called n:mployees to be e'ual to
the length of the array3
n:mployees B employeesJames.length=
7N
Programming Fundamentals
%he array<s length is permanently 5xed at creation time, so you can<t modify it. %he following
code would cause a compilation error3
employeeJame.length B G,,,=

*a'%"# 7&o&a' 18
import 6ava.io.7"
public class 8asic9rra({
public static void main (String[]args) t5ro:s I;*<ception{
=ataInputStream &,ne: =ataInputStream (S(stem.in)"
int inde<"
String names[],{)>un)% )>eff)% )>o5n)% )>essie)% )>et)#"
for(inde<,?"inde<.names.lengt5"inde<33)
S(stem.out.println(names[inde<])"
S(stem.out.println()"
int sum,?"
int num[],{??%@??%3??%!??%A??#"
for(inde<,?"inde<.num.lengt5"inde<33){
S(stem.out.println(num[inde<])"
sum, sum3num[inde<]"
#
S(stem.out.println() B5e total of t5e entered numbers is ) 3sum)"
S(stem.out.println()"
String da(s[],{)0on)%)Bue)%)Ced)%)B5u)%)Dri)#"
for(inde<,?"inde<.da(s.lengt5"inde<33)
S(stem.out.println(da(s[inde<])"
S(stem.out.println()"
c5ar vo:els[],{EaE%EeE%EiE%EoE%EuE#"
for(inde<,vo:els.lengt5F"inde<G,?"inde<FF)
7H
Programming Fundamentals
S(stem.out.print(vo:els[inde<]3)% ))"
#
#

O(t%(t8
/(n
/#11
/ohn
/#--i#
/#t
100
200
300
400
500
Th# tota" o1 th# #nt#&#d n('b#&- i- 1500
+on
T(#
9#d
Th(
F&i
*at
*(n
(: o: i: #: a:
*a'%"# 7&o&a' 28
import 6ava.io.7"
public class 8asic9rra(@{
public static void main (String[]args) t5ro:s I;*<ception{
=ataInputStream &,ne: =ataInputStream (S(stem.in)"
S(stem.out.println()*nter Bemperatures))"
int inde<"
double temperature[] , ne: double[H]"
27 t5e follo:ing codes s5o: 5o: to input elements in an arra(
7L
Programming Fundamentals
7 and t5en displa( t5e element in a specific inde<72
for(inde<,?"inde<.temperature.lengt5"inde<33){
temperature[inde<],=ouble.parse=ouble(&.read-ine())"
#
S(stem.out.println() B5e element in inde< @ is )3temperature[@])"
#
#
O(t%(t8
;nt#& T#'%#&at(&#-
45
<5
85
75
81
100
Th# #"#'#nt in ind#2 2 i- 85.0
*a'%"# 7&o&a' 38
import <a=a"io")(
pu+li, ,la!! >a!i,Arra1/
pu+li, !tati, =oi9 main #String?;arg!$ t6ro5! I.E%,eption/
DataInputStream -&ne5 DataInputStream #S!tem"in$(
S!tem"out"println#@Enter Temperature!@$(
int in9e%(
AA t6e *ollo5ing line! o* ,o9e! !6o5 6o5 to input element! in an
AA arra
9ou+le temperature?; & ne5 9ou+le?4;(
*or#in9e%&B(in9e%<4(in9e%CC$/
temperature?in9e%;&Dou+le"par!eDou+le#-"rea9Line#$$(
2
AA t6e *ollo5ing line! o* ,o9e! !6o5 6o5 to retrie=e element! *rom
AA an arra
*or#in9e%&B(in9e%<4(in9e%CC$/
S!tem"out"println#@ t6e element in in9e% @Cin9e%C @ i!
@Ctemperature?in9e%;$(
?,
Programming Fundamentals
2
2
2
O(t%(t8
;nt#& T#'%#&at(&#-
G,
",
L
%he element in index , is G,.,
%he element in index is ",.,
%he element in index " is L.,
*a'%"# 7&o&a' 48
import <a=a"io")(
pu+li, ,la!! >a!i,Arra1/
pu+li, !tati, =oi9 main #String?;arg!$ t6ro5! I.E%,eption/
DataInputStream -&ne5 DataInputStream #S!tem"in$(
S!tem"out"println#@Enter Temperature!@$(
int in9e%(
AA t6e *ollo5ing ,o9e! !6o5 6o5 to input element! in an arra
AA an9 t6en 9i!pla 6o5 ma arra! 5ere entere9
9ou+le temperature?; & ne5 9ou+le?D;(
*or#in9e%&B(in9e%<temperature"lengt6(in9e%CC$/
temperature?in9e%;&Dou+le"par!eDou+le#-"rea9Line#$$(
2
S!tem"out"println#@ Total o* @Ctemperature"lengt6C@ element!"@$(
2
2
O(t%(t8
;nt#& T#'%#&at(&#-
7
M
?G
LG
NH
?
Programming Fundamentals
L"
%he total number elements is M.
?"
Programming Fundamentals
Activity3
Create a program having an array of double with an identi5er point. 6nput ? numbers
then display the sum of
st
and "
nd
elements then the sum of the 7
rd
and ?
th
elements.
#ample ;utput3
:nter numbers
".
"
7.
7
%he sum of elements in index , and is ?.
%he sum of elements in index " and 7 is M.
?7

You might also like