Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
129 views

Procedural Programming Notes

Uploaded by

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

Procedural Programming Notes

Uploaded by

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

Notes

UNIT 1 - SIMPLE PROGRAMS (1)


Classes
The program starts with the keyword class

(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)

The main method


All java programs must have a main method

static - is just a type of a method and is mostly used in object oriented programming

Statements
Statements end with a semicolon

Semicolons illustrate a full stop in java programs

Not using them, compiler will give a SyntaxError

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

Variables store one value at a time

When you call a variable, you get a copy of the variable

Fixed types of variables in java

Declaration must include the variable name and type

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

progress the data flow in the program

put a value to a variable

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

Function v methods - functions are methods that return a value

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

Scanners and Input


predefined methods and types by java util library

Need to create a variable of type Scanner to take input

statement goes “Scanner scanner = new Scanner(System.in);”

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

UNIT 2 - TYPES AND VALUES (1)


Compilers’ rule to check for in declared data types

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

methods return the right type

UNIT 3 - MAKING DECISIONS (1)


Records
When to use - lots of values of the same type, each value describe the same feature
of lots of different things OR values of different types that describe different features
of the same thing.

Fields
different compartments inside the record definition

need to declare the type of the data like we do for variables

variables created inside the record - instance variables

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

e.g. Student s1 = new Student();

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)

One method for each field in the record to access it.

Conventionally, the name of the name must be getfieldname

can also be created for the purpose of setting a value, one method for each field
again

Conventionally, the name of the name must be setfieldname

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

String conversion method


useful to create this method in order to print records of a particular kind - will be
used to print the details of the record, we can use the accessor methods inside this
method to get the details and include the values in the string output

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

alternatively, another kind of decision command - an exception handler

compiler “throws/raises an exception” and gives an error when the code instructs to
do something impossible

slower to execute, makes code harder to reads than necessary

UNIT 4 - COUNTER CONTROLLED LOOPS


(1)
Local and Global Variables
at any point, the program can access the variable

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

the value of the variable cannot change unexpectedly

Decomposition
Uses methods to structure the program in order to focus on a specific part of the
code

makes difficult programs much easier to write and understand

each method is set to perform a specific purpose

UNIT 6 - WHILE LOOPS (1)


the body keeps executing while the condition is true

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

accumulator controlled loops - breaks when the accumulator reaches a certain


value

UNIT 7 - METHODS AND ADT (1)


Character stream
data type

like a string but restricted operations

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

making things easier to find

eg telephone directory

Arrays
holds data one after the other

we can refer to any position in the array using indexes starting 0

Notes 7
UNIT 8 - STACK HEAP
Storing
a program manipulates values of different types- numbers, letters, text or images
and sound

values represented as but patterns of different lengths

these stores in variables

variables - particular storage space in the memory

By java, memory is stored in two areas: stack and heap

Stack
simplest values stores in stack (integers, booleans, chars, floating point values)

declaration of a variable - allocates space on stack to hold values in that variable;


next free point on stack is allocated

stack grows in uniform, structured way

Stack frame
when a method is called

area on stack so group of variables are accessible

like a barrier in the stack making ‘irrelevant to method’ variables invisible; local
variables are only available

removed after returning function

scope of a variable - part of the program the variables can accessed within

Notes 8

You might also like