0% found this document useful (0 votes)
3 views

Lecture 1-2. ICS

The document is an introduction to Java programming, covering fundamental concepts such as computer parts, object-oriented programming, data types, and loops. It explains the use of classes, variables, and methods, as well as the importance of IDEs and compilers in coding. Additionally, it discusses arrays, ArrayLists, and the distinction between void methods and functions in Java.

Uploaded by

kiptoontrevor2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 1-2. ICS

The document is an introduction to Java programming, covering fundamental concepts such as computer parts, object-oriented programming, data types, and loops. It explains the use of classes, variables, and methods, as well as the importance of IDEs and compilers in coding. Additionally, it discusses arrays, ArrayLists, and the distinction between void methods and functions in Java.

Uploaded by

kiptoontrevor2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

ICS 2101: Introduction to Java

Programming
By Dr. Eunice Njeri

28/04/2025 – 5/05 /2025


Introduction
▪ A few key computer parts for programming are the
input, screen, CPU and the RAM. In the following
schematic, communication between these is shown

▪ A computer by itself already has a limited set of


instructions for the processor.

▪ However, whilst programming, the human-readable


code is automatically translated into new sets of
instructions, in bytecode for the computer.

▪ The computer uses memory to store long sequences


of instructions.
Introduction to Java / Class bodies

▪ Java is an object-oriented programming


language.

▪ It uses classes to run programs.

▪ An example of such a class


Introduction to Java / Class bodies
▪ Such a class always needs to start with a capital letter.
▪ The “public” will be explained in detail later in the class.
▪ The brackets are used as containers. Everything that is inside the brackets of class Greeter will be
executed when this class is called.
▪ Everything that is inside the brackets of void greet will be executed when this void is called.

▪ 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

▪ Coding languages typically store different types


of variables (= stored bit of information, often
1. String - this is a text data type, like “60a” or “ eunice.
given a name as ID in order to recall it later) into
data types.
this is a number within R, for example, 2.5 or
2. Double - 500/7.
▪ The four main data types are
this is a number within N: only whole
3. Integer - numbers, like 2 or 67.

this is a variable that can be either “true” or


4. Boolean - “false”.
Data Types: Variables
▪ Java is a strict programming language
because all variables must be
declared.
▪ A variable gives an error if it is not
assigned a type, like a string or an
integer.
▪ An example of declaration in a working
code
Choices - Example

▪ Using boolean variables, choices can be made.

▪ Integrated function if(condition) checks if the


condition is true, and only executes the code within
the function if it is.

▪ If it is not true, an else if() or else() function can be


created, to make further choices.

▪ There are two examples to try in the coding


challenge for the week.
Boolean operators
▪ The condition can be constructed using
Boolean operators.

▪ These compare two variables and evaluate if


it is true or not.

▪ The following boolean operators exist

▪ To compare if strings are equal, string.


Equals(otherstring) is used.
Imports
▪ There are several add-ons with extra functions that are not loaded into the program by default
▪ In order to use them, they must be imported.
▪ One of these add-ons is java.util.Scanner - This tool allows a program in Java to use input from
the console.
▪ From the example (next slide), the scanner is first imported (loaded), and a new object scanner of
type Scanner is created, which reads input from the console.
▪ Later, this function is used as scanner.next().
▪ This reads the next input from the console and stores it into the variable “name”. The other
functions are nextInt, nextDouble, nextLine, hasNext (boolean to check if there is more input) and
hasNextInt()
Scanner (Try below example)
Import - Random
▪ This allows the program to get a random number based on a series of large calculations.
▪ This number could be either an integer or double.
▪ An example of the use is shown below:
Loop
▪ In order to make a computer run a large amount of code without programming
too many lines, is a loop.

▪ This is a repetitive instruction.

▪ In this section, for and while loop are introduced.


Loop - While (condition)

▪ A while loop repeats itself while the condition


is still met.

▪ An example of a while loop is shown


While Loop Example - Explanations
▪ First, all utilities of java are imported (loaded in). This includes the Scanner.
▪ Then, a class is created. This class holds the creation of a Scanner object and a void/method which runs a
specific subset of code.
▪ In this void, a String choice is declared.
▪ This “choice” is called a Guard.
▪ In a few sentences, it will be obvious why this is the case.
▪ First, the method gives output to the user.
▪ Then a scanner asks for and interprets the input of the user.
▪ The idea of this program is that every time the user inputs “h”, the program will execute the loop of saying
“hello” once more.
Loop Example - Explanations
▪ Then the while loop is introduced. While the choice, the guard, is
equal to “h”, so the input of the user is h, the system prints out “hello”
and scans the input again, to see if the user types “h” again or other
input in order to quit.
▪ The idea of a guard is that while the guard holds the condition (of
being equal to the letter h), the while loop will repeatedly keep being
executed. It only stops when the condition of the guard is not true
anymore. This explains the name “guard”.
Loop – For Loop

▪ 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)

▪ An example of a for loop in a code is:

▪ for(double number = 0 ; number < 10; number++)

▪ The for loop in this example would run exactly 10 times.


Elements – For Loop
The elements of the for loop are as follows:

Name Seen in code as Function

the number which will be used as a guard is


Initializer double number = 0 declared.

guard number < 10 this is the condition for the for-loop to keep running.

every time the for loop runs, 1 is added to the


progress number ++ number.
In this way, the condition is controlled.
Loop - For-Each Loop
▪ This loop checks all of the elements in a list or array (introduced in the next slides).

▪ 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(' ');

foreach (var sub in subs)


{
char[] value = sub.ToCharArray();
Array.Reverse(value);
string result = String.Join("", value);
System.out. Printlnresult);
}
Arrays
▪ An Array is a numbered sequence of
variables.
▪ It is a structured collection that stores a set
of elements, each with its own index.

▪ This index starts at 0 and moves up


according to the array length.

▪ The length of an array is fixed from the


creation.

▪ This is an example of the creation of an


Array:
Array - Example
ArrayList
▪ An ArrayList is a more flexible collection than an array.

▪ It grows when elements are added and shrinks when elements are deleted.

▪ This flexibility makes it clumsier and less efficient.

▪ This is how you declare, create and change an ArrayList:


Declare, Create and Change an ArrayList:
Declare, Create and Change an ArrayList:
Multi-Dimensional Arrays (matrix)

▪ A multi-dimension array occurs when you add arrays inside arrays.

▪ The first array will act as the index of rows, and the rows themselves will be filled with the arrays
inside this first array.

▪ A multi-dimensional array is initialized like String[][], or Int[][].

▪ 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.

▪ When this method is called, the code within it is executed.

▪ 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 a variable which is used in the method for further calculation.

▪ 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.

You might also like