Week 015-016 - Module Procedural Programming
Week 015-016 - Module Procedural Programming
1
Procedural Programming
Procedural Programming
This module introduces the fundamental concepts and principles of
procedural programming. It focuses on strategic problem solving and
algorithmic programming, as well as the principles of data structures and
functions.
This module aims to:
1. introduce the basic concepts of procedural programming;
2. understand the different variable types in Java;
3. explore the usage of Java methods; and
4. comprehend the concepts of classes and modifiers.
Program Specification
To put the Divide and Conquer Strategy into practice, let’s consider the
following problem:
We need to design a program that compares 2 pizzas and determine which is
a better buy.
Course Module
Input: The diameters and total prices of the pizzas to compare.
Output: Cost per square inch for each pizza and which is a better buy
including caption with your images.
Analysis:
By using the top-down design, we can form the program subtasks
below:
1. Get the input for both pizzas.
2. Compute the price per sq. in. for the first pizza.
3. Compute the price per sq. in. for second pizza.
4. Determine which is the better buy between the two.
5. Display the results.
Pseudocode:
1. Compute for radius. radius = one half diameter
2. Get the area. area = pi * radius * radius
3. Return (price / area)
In this example program, Pizza A is better since it has cheaper unit price
(price per square inch).
Course Module
Why Practice Top-Down Design?
One of the most basic ideas in solving problems involves subdividing a
problem into sub-problems and solving each sub problem separately.
Quite often, the attempt to answer a sub-problem introduces new sub-
problems at a lower level.
The subdivision of sub-problems should continue until a sub-problem can be
easily solved. This is equivalent to the refining of an algorithm.
In large projects, sub-problems may be assigned to different programmers or
teams, who will be given accurate guidelines about how that sub-problem fits
into the overall solution.
Classes
A class is a blueprint from which individual objects are created. The class can
have any number of methods to access the value of various kinds of methods.
It can be seen by all classes from all packages in a project.
Example:
The public class has to have the same name as the JAVA file.
A single Java file can contain more than one nonpublic class but can have
only one public class.
Modifiers
Modifiers are keywords that you add to those definitions to change their
meanings.
There are two types of modifiers in Java: (1) Access Modifiers and (2) Non-
Access Modifiers.
Access Modifiers - Java provides a number of access modifiers to set access
levels for classes, variables, methods and constructors. The four access levels
are:
1. private - visible to the class only
2. public - visible to the world
Programming NC IV
7
Procedural Programming
Java Variables
Programs work by manipulating data placed in the computer memory. The
data can be numbers, text, objects, etc. The data is given a name, so that it can
be re-used whenever it is needed. The name and its corresponding value, is
known as a Variable.
A variable is a named storage that programs can manipulate. It can also be
defined as the name of the reserved area allocated in the memory. With the
combination of the words "vary" and "able", it simply means that the value of
a variable can be changed.
Variable Types
There are three kinds of variables in Java:
1. Local variables - These variables are declared within the body of a
method and can be only used inside the method. Unlike instance and
class variables, local variables are fussy about where you position the
declaration for it.
You must place the declaration before the first statement that actually
uses the variable.
Example:
Course Module
The variable message is our local variable because it was declared
inside the main method.
Methods cannot use variables that are locally declared from other
methods.
2. Instance variables – These are variables that are declared in a class, but
outside a method. They hold values that might be referenced by more
than one method or block.
Instance variables can be accessed directly by calling the variable. They
should be called using the fully qualified name as
ObjectReference.VariableName.
Example:
import java.io.*;
public class InstanceVariableTest{
public String message;
public static void main(String[] args){
message = "Hello, World!";
System.out.println(message);
}
}
import java.io.*;
public class StaticVariableTest{
public static String message;
public static final String PERSON = “Me”;
public static void main(String[] args){
message = "Hello, World!";
System.out.println(PERSON + message);
}
}
Programming NC IV
9
Procedural Programming
The variables message and PERSON are our static variables because
they were declared outside the main method along with a static
keyword. The variable PERSON has a final keyword that makes it
hold a constant value.
Java Methods
A Java method is a collection of commands that are grouped together to
perform an operation that can be used over again.
Sometimes, the terms “function” and “method” are used interchangeably. The
correct terminology for Java is “method”.
A simple Java method includes the following:
1. Modifier – It defines the access type or the visibility of the method. It is
optional to use.
Examples: public, private, protected
2. Return Type – This refers to the data type of the value to be returned by
the method.
Examples: void, int, double, boolean
3. Method Name – This is whatever you want to call your method depending
on its purpose. Just remember about the naming convention for Java
methods.
Examples: getName, getTotalAmount, setAge, displayName
4. Parameter List - It is the type, order, and number of parameters of a
method. Using parameters lists is optional, since a method may contain
zero parameters.
Examples: getName(String firstName, String lastName)
5. Method Body – This is where the actions of the method takes place. It
contains all valid Java instructions that the method should do.
Course Module
Example:
Where:
public − modifier
int − return type
getNumber − name of the method
x, y − formal parameters
int x, int y − list of parameters
References
Java Point. Variable in Java. India. Retrieved from:
http://www.javatpoint.com/variable-datatype
Tutorials Point. Available at: https://www.tutorialspoint.com
ZDU Student Manual. (1998). Java Programming: Part 1. Retrieved from:
https://7chan.org/pr/src/JAVA_-_Student_Manual_-_Tutorial_-
_Exercises.pdf