Actual Chap2
Actual Chap2
University of Buea
Faculty of Engineering and Technology
Department of Computer Engineering Course outline
Java Basic
Basic of Java programming
Java comments
• The java comments are statements that are not executed by
the compiler and interpreter.
• The comments can be used to provide information or
explanation about the variable, method, class or any
statement. It can also be used to hide program code for
specific time.
• Three types of comments:
Single line comment
Multi Line comment
Documentation comment
5 6
11 12
23 24
Java Basic
Declaration and creation of objects- reminder 2. Constructor
• A constructor in java is a special type of method that is used to
initialize the object.
• Java constructor is invoked at the time of object creation using
the keyword “New”. It constructs the values i.e. provides data
for the object, that is why it is known as constructor.
• There are basically two rules defined for the constructor.
1. Constructor name must be the same as its class name
2. Constructor must have no explicit return type- not even
void
27 28
29 30
33 34
2. Public members are accessible from 4. A Default member: If you don't use any modifier, the member
outside the class. (class, method, etc.) is treated as default by default.
PS: Using the modifiers public and private on local • The default modifier is accessible only within package.
variables would cause a compile error.
45 46
Interface in Java
Abstract class vs Interface
Example
//Interface declaration: by first user
Interface Drawable{ void draw();
} //Using interface: by third user
51 52
e.g. The nextLine() method is used to read Strings entered by the user.
• The Scanner class is used to get
user input, and it is found in the
java.util package.
• To use the Scanner class, create
an object of the class and use any
of the available methods found in
the Scanner class documentation.
53 54
Java package
Control statements
Tutorial
e.g. The nextLine() method is used to read Strings entered by the user.
• Selection or decision-
making statement
• Iteration or repetition or
looping statement
• Jump statement
55 56
57 58
If-statements If-statements
simple if-statement The if-else statement
• The most basic statement among all control flow statements in
Java.
• The “if-else” statement is an extension Syntax:
• It evaluates a Boolean expression and enables the program to to the if-statement, which uses
enter a block of code if the expression evaluates to true. another block of code, i.e., else block.
Syntax: • The else block is executed if the
condition of the if-block is evaluated
as false.
59 60
If-statements If-statements
The if-else-if statement The nested-if statement
61 62
Decision-making statements
Loop statements
2. switch-statement
Syntax:
• In Java, Switch statements are similar to • In programming, sometimes we need to
if-else-if statements. execute the block of code repeatedly
• The switch statement contains multiple while some condition evaluates to true.
blocks of code called “cases” and a • However, loop statements are used to
single case is executed based on the execute the set of instructions in a
variable which is being switched. repeated order.
• The switch statement is easier to use • The execution of the set of instructions
instead of if-else-if statements. It also depends upon a particular.
enhances the readability of the
program.
63 64
want to execute the block of code. // Summing all elements double total = 0; }
total += myList[i]; } }
65 } 66
1. Write a Java program to generate the multiplication table for • Java provides an enhanced for loop to
a given number. traverse the data structures like array or
collection.
2. Write a Java program to find the maximum element in an e.g.:
array. • In the for-each loop, we don't need to
update the loop variable.
3. Write a Java program to sort an array using the bubble sort
algorithm.
PS: Bubble sort is a basic algorithm for arranging a string of numbers or other elements in
the correct order.
67 68
Jump statements
Jump statements
1. Break statement
e.g.:
• As the name suggests, the break statement
• They are used to transfer the control of is used to break the current flow of the
the program to the specific statements. program and transfer the control to the
next statement outside a loop or switch
• In other words, jump statements statement.
transfer the execution control to the
• However, it breaks only the inner loop in
other part of the program. the case of the nested loop.
• There are two types of jump statements • The break statement cannot be used
in Java, i.e., break and continue. independently in the Java program, i.e., it
can only be written inside the loop or
switch statement..
71 72
73 74