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

MOBILE APPLICATION DEVELOPMENT - Program Structures + Classes and Objects

Format

Uploaded by

fairleyjames747
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

MOBILE APPLICATION DEVELOPMENT - Program Structures + Classes and Objects

Format

Uploaded by

fairleyjames747
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

3/12/2024

MOBILE APPLICATION DEVELOPMENT


PROGRAMMING STRUCTURES + CLASS DECLARATION

PROGRAMMING STRUCTURES
Well structured Programs may be written using 3 structures namely:
 SEQUENCE – One instruction at a time from top to bottom.
 SELECTION – Used when the computer needs to make decision as
to which path to follow. The normal flow/sequence of execution of
instructions is altered Conditionally or Unconditionally.
 ITERATION/LOOP – Simply means repetition of one or more
instructions a given number of times (Counter controlled) or until a
condition no longer exists (Sentinel controlled).

1
3/12/2024

PROGRAMMING STRUCTURES
SEQUENCE STRUCTURE – One instruction at a time from top
to bottom.

PROGRAMMING STRUCTURES
SELECTION STRUCTURE – Conditional: A criteria is evaluated
to know what path (instructions to execute) is desired. This
involves writing Boolean expressions:

2
3/12/2024

PROGRAMMING STRUCTURES
 SELECTION STRUCTURE – Unconditional: No criteria is evaluated
before normal flow of program execution is altered. E.g. Function
call, use of the return, break, continue statements etc.

PROGRAMMING STRUCTURES
ITERATION/LOOP STRUCTURE – Counter Controlled:
Repetitions can be counted (determined) before the loop is
executed. Involves the use of a counter variable.

3
3/12/2024

PROGRAMMING STRUCTURES
ITERATION/LOOP STRUCTURE – Sentinel Controlled: Exact
number of repetitions cannot be determined before the loop is executed.
Iteration continues until a condition is no longer satisfied. E.g. Input of data
from the user and validation of same.

PROGRAMMING STRUCTURES
The break & continue Statements– break is basically used to
unconditionally terminate a loop or a switch statement while
continue is used to unconditionally skip to the next iteration.

4
3/12/2024

CLASSES & OBJECTS


Class: A description or blue print of an object with respect to
its properties (data/instance variables) and behaviors
(functionality/methods). Classes models objects.
Object: Is an instance of a class. They are created
(instantiated) from a class. They possess only the attributes and
behaviors specified by the designer of the class.
The engineering drawing/design of a Car can be likened to a
class while the car built (object) based on the design is
considered as the instance of the Car.

CLASSES & OBJECTS


A Car object may have properties such as: color, number of
doors, odometer reading, engine capacity, amount of fuel in
its tank etc.
The tasks/ functions that the Car object exhibits may include:
starting the car, acceleration, reverse, change gear, wash the
car, etc. these tasks (methods) may depend upon one or more
values of the properties of the Car object.
The properties (instance variables) and behaviors (methods) of
a class (object) may be modeled and communicated using a
UML class diagram.

5
3/12/2024

CLASSES & OBJECTS


Class Diagram: comprises of 3 compartments.
The first compartment contains the name of the class
centralized. The object being modeled is usually used as the
class name.
The second compartment contains the names of the properties
(fields). The data type and scope is also indicated.
The third compartment contains the functionalities (methods) of
the class.

CLASSES & OBJECTS


UML Class Diagram of a Bird class
First Compartment

Second Compartment

Third Compartment

6
3/12/2024

CLASSES & OBJECTS


UML Class Diagram of a Bird: We shall be considering only a
subset of the class diagram as presented below:

CLASSES & OBJECTS


Class Declaration: This involves writing codes based on the
design/model of the class as specified in the class diagram in
the chosen programming language (Java).
A class declaration in Java comprises of the class definition
header and the body of the class (enclosed in curly brackets)
 scope modifier class Class_Name // Class definition header
{
Body of the class – the instance variables and methods are
defined therein.
}

7
3/12/2024

CLASSES & OBJECTS


Bird Class: An empty class declaration of the Bird class is
presented below.

CLASSES & OBJECTS


Declaring Instance Variables: Represent the data part of an
object. Each object (instance manages its own copy of the
values).
Syntax: scope dataType variableName;
Examples:
private int gestation; // scope: private dataType: int name: gestation
private double weight;
protected String name;

8
3/12/2024

CLASSES & OBJECTS


 Declaring Methods: Methods represent the functionality/task
performed by an object.
 Syntax:
scope modifier returnDataType methodName(parameter_list)
{
statement(s) to be executed
}
 Example:
public void fly() // method definition header
{
System.out.println(“This Bird can fly!!!!”);
} // end method fly

CLASSES & OBJECTS


Creating (Instantiating) and Using Objects: When objects
are created from their respective classes (instantiation) then
messages can be passed to them (method invocation) so that
they can perform the desired tasks.
To create an instance of a class, you may:
1st Declare a variable of the class. E.g. Bird myBird;
2nd create an object of the class using the keyword: new then
assign it to the variable after calling the required constructor.
E.g.: myBird = new Bird();
Or alternatively combine the two process as one thus:
Bird myBird = new Bird();

9
3/12/2024

CLASSES & OBJECTS


Using Objects: Once objects are crated, using their names
and a dot operator (.) they may be used to perform specific
tasks as desired. Example:
To cause the bird object created earlier to fly, you may write:
myBird.fly();
If we need the Bird to sing: myBird.sing();
To change its gestation period to 25days:
myBird.setGestation(25);
NOTE: Several objects may be created from a class and
customized by assigning appropriate values to their properties
as appropriate.

10

You might also like