Lecture 1-2. ICS
Lecture 1-2. ICS
Programming
By Dr. Eunice Njeri
▪ At the end of every statement, “;” indicates that the program should start on a new line. Not
writing this means an error will show up.
▪ At the end of the class, the void is called in a “starter”, which runs the code. Calling a class would
also run the code inside this class. In this starter, the class is generated and the method is called.
This runs the method and produces the output “Hi there” for it.
IDE & Compiler
▪ You can use any of the following IDEs:
▪ NetBeans
▪ Eclipse
▪ IntelliJ IDEA
▪ VS Code (with Java extensions)
▪ Language compilers are needed to scan the code for errors and translate the language into
bytecode. The Virtual Machine part of an IDE runs the code.
Data Types
▪ This loop is more controlled, and perfect for scanning through a list or a definite number of items.
▪ The for loop works the same as a while loop, except for the declaration of this loop.
▪ Declaration: For(initializer;guard;process)
guard number < 10 this is the condition for the for-loop to keep running.
▪ It is set up as follows:
For Each - String Challenge
string s = "The quick brown fox jumps over the lazy dog";
string[] subs = s.Split(' ');
▪ It grows when elements are added and shrinks when elements are deleted.
▪ The first array will act as the index of rows, and the rows themselves will be filled with the arrays
inside this first array.
▪ To get an element out of these, the first bracket acts as the row index, and the second as the
column index.
Example - Multi-Dimensional Array
Void Method
▪ A method (void) is container with a name in which a sequence of lines of code is stored.
▪ A method is called by putting its name on a line of code, not forgetting the brackets.
Example - Void
▪ Void smile is called in the void demoticon. That
means that when the void demoticon is run, then it
will run the smile() method after the first line.
▪ .
Explanations
▪ Void angry is special: there is a parameter (int n) in the brackets.
▪ This is useful if you need to perform the same sequence of code for different numbers.
▪ If angry(3) is executed, int n=3, and it will use n to execute the code.
▪ In this specific code, that means that the for loop will be executed 3 times
Function
The difference between a void method and a function is whether the method “returns” a value. The
method shown above will run a code and output this, but upon running this method, it is not a
variable by itself. When a method, like a sum, returns something, the sum itself is a variable too.