Algoritms 1-2 - JAVA - Introduction PART1
Algoritms 1-2 - JAVA - Introduction PART1
Introduction to programming
Algorithms
• Variable names cannot contain spaces, and cannot start with any
numbers or special characters (&,$,@,%, etc.).
Variable names
NB:
• Bear in mind that java is a case sensitive.
• Every java line of code ends with a semi-colon ‘;’
Example 1
READ Number1
READ Number2
Total = Number1 * Number2
WRITE Total
STOP
Example 1
READ AssignmentMark1
READ AssignmentMark2
Sum = AssignmentMark1 + AssignmentMark2
If (Sum > 20) Then
WRITE ‘High’
Else
WRITE ‘Low’
EndIf
STOP
Example 3
READ AssignmentMark
Counter = 0
While (AssignmentMark >= 0) Do
Counter = Counter + 1
READ AssignmentMark
EndWhile
WRITE Counter
STOP
Example 6
• Write an algorithm that will determine and display the average mark
of the assignments handed in (there are an unknown number of
assignments). Assume that a negative number will end the input of all
assignments.
Example 6
• Write an algorithm that will determine and display the average mark
of the assignments handed in (there are an unknown number of
assignments). Assume that a negative number will end the input of all
assignments.
READ AssignmentMark
Total = 0
Counter = 0
While (AssignmentMark >= 0) Do
Total = Total + AssignmentMark
Counter = Counter + 1
READ AssignmentMark
EndWhile
Average = Total / Counter
WRITE Average
STOP
Exercise 7
• Write an algorithm that will determine the highest and lowest assignment mark
between an unknown number of assignments. Assume that a 0 will end the input of
assignments. The algorithm should also display the highest and lowest marks obtained
for the assignment at the end.
READ AssignmentMark
Highest = AssignmentMark
Lowest = AssignmentMark
While (AssignmentMark != 0) Do
If (AssignmentMark > Highest) Then
Highest = AssignmentMark
Else
If (AssignmentMark < Lowest) Then
Lowest = AssignmentMark
EndIf
EndIf
READ AssignmentMark
EndWhile
WRITE ‘The highest is ‘, Highest ,’ and the lowest is ‘, Lowest
STOP
Exercise 7
• Write an algorithm that will determine the highest and lowest assignment mark
between an unknown number of assignments. Assume that a 0 will end the input of
assignments. The algorithm should also display the highest and lowest marks obtained
for the assignment at the end.
Conclusion
• Any questions?