Procedural Programming Notes
Procedural Programming Notes
(keywords are just words with a specific meaning and features in the language)
The program must be saved with the extension .java with the prefix to the dot being
the name of the class used in the program (classname.java)
static - is just a type of a method and is mostly used in object oriented programming
Statements
Statements end with a semicolon
Variable Declarations
Creates a new variable
Variable - a storage space for a specific type of value; identifiers (names) so they
can be referred to again
Notes 1
The compiler will give you an error if you assign the value of a different type to the
variable rather than the the you declared it to be
The variable created inside a method - a local variable - can only be used within the
method or a restricted set of instructions in a program.
Assignment
core work of the programs
works by putting the value at the lhs of the = sign preceded by the declaration of the
variable or just the variable name after declaration
Function call
the execution jumps to another piece of code ie the function code and returns to the
initial position after the function is complete
Print statements
System.out.println() is simply a method but is provided by the creators of java which
prints out the value specified by the user inside parentheses to the terminal
The rhs of the = sign creates a new Scanner object and assigns it to the name
‘scanner’
Use a special method called scanner.nextLine() that basically instructs the machine
the let the characters typed by the user in the clear line created by the program in
Notes 2
the terminal because of the .nextLine() statement get stored in the variable assigned
to the method call
the values stored in the variables are of the same type as the declaration of the
variable
operations and methods performed on the variable are only applied to the right
type
Fields
different compartments inside the record definition
Definition of the record doesn’t create a record itself but instead a type available for
the whole program, a blueprint for new records to be created from
To actually create a new empty record - ‘new’ keyword followed by the recordname()
assigned to a variable of type recordname, creating actual storage space
Notes 3
To store values to particular fields inside the record - notation is record.field = value;
Accessor Methods
good practice to access record fields using methods you define for the purpose and
not using the direct dot notation; methods thus created are called accessor methods
(Methods are just packaged up bits of code, that are given a name to allow us to
execute that bit of code just by giving the name (calling the method). They can be
given information they need to do the job (arguments) and can return results)
can also be created for the purpose of setting a value, one method for each field
again
restricts the details of the record to a few methods, if we change the internal
structure of the record, only a small part of the code needs to change
Abstraction
hiding the detail of how the program is implemented
helps because we can make a simple change to small part of the code in order to
make big changes in the code functionality regarding records
we don’t need to change any other part of the code except accessor methods.
Everywhere else we call same methods; This is possible because accessor
methods hid what the record actually stored - main method doesn’t need to be
altered
good practice when writing programs with records to use abstraction to hide details
of implementation.
Notes 4
Forms the basis of ADTs (a complex type whose internal structure is hidden)
Defensive Programming
writing code that can cope with all eventualities
Input validation - must assume that users will make mistakes while entering inputs,
writing code that makes sure correct or appropriate input has been entered by the
user
Rather than recovering code after letting things go wrong, you should avoid letting
things go wrong in the first place
Exception Handler
In many cases, input validation can be taken care of using straightforward if
statements and is considered better than exception handler
compiler “throws/raises an exception” and gives an error when the code instructs to
do something impossible
Local variable - created and can be accessed within a restricted set of lines of code
like a method
Good programming practice suggests using local variables ie within a small scope
should not use global variables because they make it hard to understand and hard
to change; testing becomes hard when using global variables
Notes 5
compiler will tell when using local variables outside the limited space; helps the
developer not making mistakes for using particular variables where not to; then the
developer can fix it
resulting compile time errors for using local variables in unintended space can help
knowing where the error lies
Using global variables and getting an error, any line could have caused it, such error
found will be a runtime error and not enough testing, the programmer will never
know where the error is
Using local variables may reduce the possibility of name clashes with other objects
in the program
Final keyword
some variables are declared to be constant by putting the ‘final’ keyword in front of
their name
Decomposition
Uses methods to structure the program in order to focus on a specific part of the
code
accumulator variable - a variable whose value we manipulate each time the loop is
executed
sentinel stop value - a specific value by the developer, when the loop variable
equals this value it breaks as constructed
counter variable - a variable that keeps count for how many iterations has gone
through, basically an accumulator variable but for the specific purpose of keeping
Notes 6
count of iterations
counter controlled loops - breaks when counter variable reaches a certain value
sequence of characters, program can use while extracting one character at a time
from the stream
UNIT 10 - SORTING
Sorting
rearranging pieces of data into a set order, one after the other in either ascending or
descending order
sort algorithm - set of instructions that put that data in this sense
important because
eg telephone directory
Arrays
holds data one after the other
Notes 7
UNIT 8 - STACK HEAP
Storing
a program manipulates values of different types- numbers, letters, text or images
and sound
Stack
simplest values stores in stack (integers, booleans, chars, floating point values)
Stack frame
when a method is called
like a barrier in the stack making ‘irrelevant to method’ variables invisible; local
variables are only available
scope of a variable - part of the program the variables can accessed within
Notes 8