You are on page 1of 30

Programming

and Databases
Introduction
• In this course, Basic Java concepts and principles
will be discussed, alongside with the explanation
of sample executable programs.
• The student will also be trained in the various
concepts and models necessary to store,
manipulate and handle data using MySQL.
• This will involve creating forms and reports from
queries based on data present in an existing
database.
Learning Goals
By the end of this courseware, the student is expected to:
1. Appreciate Java as a programming language.
2. Define commonly used terms in object-oriented
programming such as classes and objects.
3. Apply the different language elements and constructs
of Java to solve simple programming problems.
4. Create a database using MySQL through
phpMyAdmin.
Lesson 6
Loops and Exceptions
• What Are Loops?
• For Loop
• While Loop
• Do – While Loop
• Nested Loops
• What are Exceptions
• Error – Handling
• Try and Catch Statement
What are Loops?
Loops permit a set of instructions to be repeated
until a condition is reached. In Java, there are three
loop structures.

• For Loop
• While Loop
• Do – While Loop
For Loop
Syntax

for (<initialization>; <test condition>;<increment/operation>)


{
<statement/s>
}

Example
While Loop
Syntax while (boolean condition)
{
<statement/s>
}

Example
Do-While Loop
Syntax do
{
<statement/s>
}while (Boolean condition);

Example
Nested Loops
What are Exceptions?
In Java, there are also exceptions: scenarios or
events that might cause your program to produce
errors.

Error-Handling
Exception Classes are used to handle errors in java
programming.
Example 1
Line 11 try
• try is a Java keyword that tells the compiler that
an error might happen within the scope of the try
statement.

Line 17 catch (Exception e)


• Whatever is within the scope of the catch
statement will be executed if an error is produced
within the try block
Try and Catch Statements
Its purpose is to anticipate errors, and if errors are
encountered, provide a way for the program to
terminate gracefully.
catch (IOException
e)

Example 2
Lesson 7
Creating Classes
• Classes
• Methods
• Inheritance
• Method Overloading
• Overriding
Classes
In Java, the blueprint of real-life object is a class. An
object is used to represent a class.

name

attributes

methods
Methods
These are groups of codes that do a particular task
when executed.

Syntax

Access_Modifier return_type method_name{argument/s)


{
<statement/s>
}
Constructor
A constructor is a method called automatically
when you create a new object. It has the same
name as the class and has no return type.
Example 1
Example 2
Inheritance
It is the ability to derive new classes from existing
ones.
public class <subclass> extends parent_class
{
Syntax
<statement/s>
}

Example
Method Overloading
Overloading means that you have methods with the
same method name but with different argument
lists or parameters.
Method Overriding
Changing inherited methods within the subclass is
called overriding.
Example

You might also like