OBOPROG
Object Oriented Programming
FAITH Semester 1
Academic Year 2020-2021
A Brief History of Programming
Languages
• Machine Code
• Assemblers
• Third-generation
• Pascal and C
• Object-orientation
• Scripting langiages
Hierarchy of Programming Langauges
Programming Examples
Problems with Programming
• Programmer efficiency
• Testing and reliability
• Documentation
• Maintenance
Programmer Efficiency
Small, medium and large programs
Small < 1 million lines
Medium 1-10 million lines
Large > 10 million lines
Windows 10 is 100 million lines
Regular programs change as business rules
change e.g. Boeing 737 Max
Testing and Reliability
• Many apps are safety-critical e.g. Aircraft
• A small programming failure can be fatal for a
business. Will Boeing survive?
• Long linked supply chains need compatible
software
• Many departments of a business are “human-
free”
Documentation
Programs from 1960s are still in use.
Programmers must adapt them when changes
are needed.
Documentation may not exist and may need to
be writte,
Programs should be self-documented
Maintenance
Maintenance when:
• Business rules change e.g. Legislation change;
• Errors appear;
• The program is brought up to a new standard.
Software at all levels
Strategic management : financial planning, inter- business co-operation,
Time scale ~5 years Use Decision Support Systems(DSS)
Middle management: resourcing, logistics, supply chains,
Use Management Information Systems (MIS) e.g. Stock control.
Time scale ~ 1 year
Operational management: manufacturing, customer contact, day-to-day
admin Use Office Automation Systems(OAS) Time scale = days
Operations: robotics, automation, firmware systems.
Integrating Software Levels
Problems with Software 1. Design
Standard top-down design is essential.
Organisations may have multiple incompatible
systems written in a variety of languages.
The software design methodology is a strategic
decision.
Problems with Software
2. Writing programs
Programming standards.
Programmer control standards – many
programmers are contractors.
Black hats and white hats.
Black hats - contract programmer hackers
White hats - repairers of hacked systems -
sometimes the same people as the black
hats
Problems with Software 3. Testing
Independent testing and test data.
Test results may be used in subsequent
litigation if anything goes wrong.
Problems with Software
4. Integration with other systems
Business integration may be multi-national with
long supply chains.
Common language - usually English.
Common Internet standards e.g. Huawei.
Trend towards larger business groupings e.g.
ASEAN, EU, NAFTA.
Tariff barriers e.g. TikTok
Problems with Software 5. Updates
Updates must be synchronised across all
connected systems.
Problems with Software
6. Agreed Standards
Move to common standards e.g.
Security and encryption;
4-byte URL;
Standard data dictionaries;
Programming languages and packages.
Good Program Language Design
• No ‘goto’ - too powerful and
far-reaching
• Structured programming constructs -
sequence, selection, iteration
• Self-documentation
• Self-contained small program units –
subprograms, methods, sub-routines etc.
• Structure of the units unavailable to the
programmer
Why Object-Orientation?
• Replace procedural languages – safety
especially in critical applications
• Retains the principles of structured analysis
and design
• Used with WIMPS environment – WIMPS is
object-oriented
What is object-orientation?
Objects and messages
The Language of OOP
• Abstraction
• Encapsulation
• Polymorphism
• Instantiation
Abstraction
• Abstraction – simplification of
program design
• Incorporates ‘top-down’ structured
design methodology
• Programs are self-documenting (long
and meaningful datanames)
Abstraction - a simple file update program
structure chart
Top-down design and abstraction
class FileMaintenance{
public FileMaintenance(){ // the class ‘constructor’
InitializeFile i = new InitializeFile(); // classes are ‘instantiated’
UpdateFile u = new UpdateFile();
ReportResults r = new ReportResults();
}
public static void main(String args){
FileMaintenance fM = new FileMaintenance();
}
}
class InitializeFile{} // classes may be defined in the same file
class UpdateFile{}
class ReportResults{}
Top-down design and abstraction
This program will compile as it is and may be
developed by stepwise refinement.
The main class is in the file
FileMaimtenance.java.
It is executable because it has a ‘main’ method.
Inheritance
Inherited Methods
If there is
void openAccount(String name, Int accountNo);
in ‘BankAccount’, it can be used by all its child methods.
Inherited methods
To create an inherited class use ‘extends’ as in
class MyAccount extends BankAccount{}
MyAccount inherits all the methods of BankAccount
Inherited methods can be overridden e.g.
@Override
void openAccount (String name, int accountNo,
float interestRateDeal);
Encapsulation
• Protecting the programmer from
himself.
• Classes have ‘hidden’ code with
methods and variables which are not
visible to the programmer.
Polymorphism
Means ‘many shapes’.
A method may have several different input
forms e.g.
void defineColorOutline(Color colorConstant){..}
void defineColorOutline(int[] rGBGroup){..}
void defineColorOutline(float[] proportions){..}
Datanames and ‘Camel notation’
Datanames must be meaningful. Programs often
last longer than their originators and will be
maintained by gemerations of programmers.
E.g. A variable denoting daily cash balance might
be named
dailyCashBalance
There is an upper limit of 255 on the length of a
dataname but an optimum size is usually up to
32 characters.
Datanames and ‘Camel notation’
The data variable is shown in ‘camel notation’
i.e. dailyCashBalance
Where each word in the name begins with a
capital, except the first.
Java
• The language of object orientation.
• Dates from 1990
• Owned by Oracle (formerly Sun)
• Used for all levels of programming
• Embedded systems & devices (3 billion!)
• The language of the Android app
Instantiation - Classes and Objects
• A class instantiates objects
• Object – a program in its own right with
Either
a main() method
Or
code (methods and variables)
to be used by another program
Components of a Java Program
• Classes
– Programmable classes
– Imported classes and methods
• Constants and variables
• Methods
– Constructors
– Working methods
• Internal classes
• Interfaces
The basic syntax is that of C++
Contents of a class – variables and methods
[public] class myClass {
........................ // variables of myClass
[method type] myMethod1{
// variables of method1
}
[method type] myMethod2{
// variables of method2
}
// other methods
}
Java does not permit methods within methods
Method types
Method type is either a known variable or ‘void’
void nonValueReturning(..){
............................
}
double valueReturning(..) {
.............................
return [someDefinedDoubleValue];
}
Structure of an Executable class
import package1.somePackage; // packages are collections of tested, reusable classes
public class ClassDefinition {
// variables which have scope over the whole class
[public] ClassDefinition(){ // the constructor
......................
} // closure of the constructor
................. // other methods of ClassDefinition
public static void main(String[] args){
// local variables of method main()
ClassDefinition cD = new ClassDefinition(); // instantiation of ClassDefiniition
............................ // other main() method code
} // closure of main method
} // closure of class ClassDefinition
Executable Programs
If a class is going to be used as an executable
program, it must contain a method
public static void main(String[] args){}
which is the start point of the program
Classes need not be executable. They can contain
methods which can be imported and used by
other classes.
Running a Java App
Enter Command Prompt (black screen) and change
directory to your desktop directory where your
development applications are kept e.g.
C:\Windows\SYSTEM32\>CD\
C:\>
C:> CD USERS\MYNAME\Desktop\MyJavaFolder\
C:\USERS\MYNAME\Desktop\MyJavaFolder\>
HelloWorld.java
/* My very first working program!!!*/
public class HelloWorld {
public static void main(String[] args){
// the main method
System.out.println(“Hello World!!!”);
}
}
The Compile/Execute Sequence
Compiling and Running a Java Program
Create a file HelloWorld.java using a text editor. (Java is
case sensitive.)
Use the command line window. Windows-based
programs are run from the black window.
To compile to byte code called HelloWorld.class
>javac HelloWorld.java
If no errors, run the program by
>java HelloWorld
The Java Virtual Machine
javac.exe and java.exe use the
Java Virtual Machine(JVM)
Java is ‘compile once, run anywhere’
Byte code
Java programs are compiled to ‘byte code’ which
are tightly compressed programs to be run on
any machine with the JVM installed.
Java does not produce .exe programs in native
code. This is because there is only one version
of the byte code to run anywhere.
Interpreting Byte Code
Byte code is ‘interpreted’. The conversion from
byte code to native machine code is perfomed
by the version of the JVM installed.
The JVM is operating system specific.
Advantages of Java Byte Code
The same version of the Java program can be
run on any platform. It is not machine/OS
specific.
Cross platform means that it is suitable for
Internet platforms – laptops, desktops,
tablets, Smartphones and remote devices
Disadvantages of Java Byte Code
The interpretation is slow even if the byte code
is very small.
Creating a .exe file in native machine code is a
third-party operation.
Java security is poor – a byte code .class file can
be reverse engineered quite easily.
Online Java programs need to be ‘sandboxed’.
The Oracle Java Package
• The Java Development Kit (JDK) at
C:\Program Files\Java\jdk1.8.0_221\bin
contains the compiler and byte code interpreter
The system PATH must include this string.
Loading Java
Download the 64-bit Java Development Kit (jdk)
from the Oracle website. It will be automatically
installed in
C:\Programs\Java\jdk****\bin
(**** is the latest version number)
Go to this subdirectory and obtain and save the link
from the textfield lising the subdirectory
structure
Setting Java Environment Strings
Add this saved path to the environment variables.
To do this, go to
• Control Panel->System->
System -> Advanced System Settings
• Click on “Environment Variables”
• A panel with two text areas appears.
• Edit the upper text area by adding the saved path
to the list of path strings, separated by “;”.
• Save and close all parts of the Control Panel
Java Versions
All Java applications use the same Java syntax
and formats
Java can be loaded to any common operating
system – Windows, Linux, Solaris, Android,
MacOS, in 32-bit and 64-bit versions.