Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Instructor: Franklin Amoo
What is a Computer Program?
For a computer to be able to do anything (multiply,
play a song, run a word processor), it must be given
the instructions to do so.
A computer program is a set of instructions written
by humans to tell computers what to do.
The instructions are written in programming
languages such as C, C++, Java, etc.
Programming Languages
Computer programming has developed into easier
tasks because of the development of high-level
programming languages
High-level programming languages allow the use of
vocabulary of reasonable terms easy for humans to
understand
Some easy terms include “print”, “read”, “write”, “add”
High level languages also allow the use of
meaningful names for use in computer memory
Programming Language Syntax
Each high-level language has its own syntax or
rules of the language
All programming languages have specific limited
vocabulary and a specific set of rules for using the
vocabulary
Programmers use the syntax of the specific
language to write a series of statements that make
up the program instructions
Programming instructions are translated into
machine codes by computer programs known as
compilers or interpreters
Compilers Verses Interpreters
A compiler translates an entire program before
carrying out the statements or executing the program
An interpreter translates one program statement at a
time, executing statements as soon as it is translated
An invalid program statement is known as a syntax
error (misuse of the programming language)
Compilers and interpreters issue error messages each
time they encounter syntax errors
Syntax errors can be removed (corrected) in a process
known as debugging (freeing the program of all
errors)
Compiler and interpreter
Programs written in compiled languages execute more
quickly than programs written in interpreted
languages
Java uses the best of both compiler technology and
interpreter technology
First, Java uses the compiler to translate the
programming statements into the Java bytecode
Second, java uses the interpreter to read the compiled
code (bytecode) at run time in a statement by
statement manner
Programming Logic
In addition to understanding the syntax of a language, a
programmer must understand programming logic
The logic of programming involves the proper
arrangement of program statements in a correct order
Achieving the desired results depends very much on the
logic of statements and procedures
Errors in the logic of a program is known as logical
errors
Locating and removing syntax errors will not make a
program error free unless logical errors are also fixed
Some programmers refer to logic errors as semantic
errors
Procedural Programming
Procedural programming involves writing
programs to be executed in a sequential order
In procedural programming computer memory
locations are named as variables so as to hold
values at variable locations in computer memory
The individual tasks performed are grouped in
logical units known as procedures
Depending on the language used procedures may
be referred to as modules, methods, functions and
subroutines
Object-Oriented Programming
Object-oriented programming (OOP) is an extension
of procedural programming
Object-oriented programming involves the
creation of programming units called classes,
creating objects from those classes
creating applications that are stand alone executable
programs that use those objects
Classes created are reusable and can be used for
developing new programs
The Object-Oriented Programming Concept
Understanding object-oriented programming differs
from the traditional procedural programming requires
the understanding of four basic concepts
Objects
Classes
Inheritance
Polymorphism
The Object Concept
Objects in the real world and in OOP consists of
attributes and methods
The attributes of an object are also referred to as the
state of the object
Attributes are the characteristics that define the object
Attributes differentiate objects of the same class from
one another
Attributes of an automobile include its make, model,
year, price,
The class Concept
A class is the term used to describe a group or
collection of objects that have common properties
An instance of a class is a particular existing object of
the class
Consider the white Toyota car you saw today or the
brown Alsatian dog you know as Bruno
That white Toyota car is an instance of the class that
consists of all Cars
That brown dog, Bruno is an instance of the class that is
made up of all Alsatian dogs
A particular instance of a class takes its attributes from
the general category or the class
The method Concept
Beside attributes, objects can use methods to
accomplish specific tasks
A method is a self-contained block of program code
which is similar to a function or a procedure
Like procedural programs, object-oriented
programs have variables (attributes) and
procedures (methods)
The details of methods are encapsulated into the
objects that operate on or use the methods
Encapsulation refers to the hiding of data and
methods within an object
Object-Oriented Thinking
Thinking of items as instances of a class is a key step in
understanding object-oriented programming
Thinking in terms of object-oriented development
involves
Envisioning program components as belonging to
classes
Envisioning program components as objects in the real
world
Objects can be manipulated by class methods
Objects can interrelate to each other to produce desired
results
Inheritance
Inheritance refers to the ability to create classes that
share the attributes and methods of existing classes
Classes that are inherited are called super classes and
have general characteristics and properties
Classes that inherit features from other classes are
called sub classes and have more specific characteristics
and properties
A super class called Vehicles has general features that is
characteristic of all members of class Vehicles (small
vehicles, buses, vans, long vehicles, trucks)
A sub class called Cars has more specific features that is
characteristic of all members of class Cars (saloon,
caravan etc)
Polymorphism
Polymorphism literally means many forms
Polymorphism describes the feature of languages that
allows the use of one word in different context
Consider the English word “open”
The word “open” can be used in many different
contexts to produce different meanings
We can open a door
We can open a bank account
We can open a book
We can open a school
We can apply this concept in object-oriented
programming to name certain units
Compiling Programs
Computers do not understand the high-level
languages (VisualBasic, C/C++, Java, etc) that
programs are written in.
Programs written in high-level lanaguages must
first be compiled (converted) into machine
code that the computer can run.
A compiler is a program that translates a
programming language into machine code.
Running Programs
All programs follow a simple format:
Input
Processing
Output
Inputs can be from users, files, or other computer
programs
Outputs can take on many forms: numbers, text,
graphics, sound, or commands to other programs
Multiple Compilers
Because different operating systems (Windows,
Macs, Unix) require different machine code, you
must compile most programming languages
separately for each platform.
Java code
compiler
compiler
compiler
Win
Unix
MAC
Java Interpreter
Java is slightly different.
Java compiler produces bytecode not
machine code.
Bytecode can be run on any computer with
the Java interpreter installed.
Java Program
Java Bytecode
MAC
Interpreter
compiler
Filename.java
Win
Filename.class
Unix
Advantages of Java…
Advantages:
Java is platform independent. Once it's compiled, you can
run the bytecode on any machine that has the Java
interpreter. You do not have to recompile for each
platform.
Java is safe. Certain common programming bugs and
dangerous operations are prevented by the language and
compiler.
Java standardises many useful operations like managing
network connections and providing graphical user
interfaces.
Java offers less work for programmers. One version of
java program is suitable for all platforms (different
operating systems can run the same code)
When using other languages software vendors have to
produce multiple versions of the same product for
different platforms (Windows version, Linux version etc)
… and disadvantages of Java
Disadvantages:
Running bytecode through the interpreter is not as
fast as running machine code, which is specific to
that platform.
Because it is platform independent, it is difficult to
use platform specific features (e.g., Windows
taskbar, quick launch) in Java.
Java interpreter must be installed on the computer
in order to run Java programs.
The Java Virtual Machine (JVM)
Java instructions do not execute directly on
computers
Java runs on a hypothetical computer known as the
Java virtual machine (JVM)
Java statements are typically written in text editors
such as notepad and are saved with .java extension
The written statements make up the program and
is usually known as the source code
The java compiler converts the source code into an
intermediate program called the java bytecode
The Java Security features
The java interpreter checks the bytecode and
communicates with the operating system
The java interpreter then executes the program
statements within the java virtual machine
Execution is done in a statement by statement fashion
Java is run independent of the operating system so it
does not interact with computer hardware
The (JVM) provides security against intruders who try to
access computer hardware through the operating system
Java therefore is more secure than other languages and
can be run on a wide variety of computers
Java Program Types
We can use Java to write two major types of programs
Java applets are programs written to run in web pages
Java applications are stand-alone programs
Java applications are subdivided into
Console applications with support for character output
to a computer screen in a DOS window
Windowed applications, which create graphic user
interface (GUI) with elements such as menus, toolbars,
and dialog boxes
Console applications are easiest to create and ideal for
learning to program
Our First Java Program
Open Notepad++ and type the following piece of Java code
exactly is you see it:
public class Welcome
{
public static void main(String[] arg)
{
System.out.println(“Welcome to the Java Class!”);
}
}
Save this file as Welcome.java (watch capitalization) in the
following directory:
c:\JavaPrograms
Compiling and Running Our First Program
Open the command prompt in Windows (cmd)
To run the program that you just wrote, type at the command
prompt:
cd c:\JavaPrograms
Your command prompt should now look like this:
c:\JavaPrograms>
To compile the program that you wrote, you need to run the
Java Development Tool Kit Compiler as follows:
At the command prompt type:
c:\JavaPrograms> javac Welcome.java
You have now created your first compiled Java program named
Welcome.class
To run your first program, type the following at the command
prompt:
c:\JavaPrograms>java Welcome
Another Java Program
Open Notepad++ and type the following piece of Java code
exactly:
public class AnotherProgram
{
public static void main( String arg[] )
{
System.out.print(“This is another program”);
}
}
Save this file as AnotherProgram.java in the following
directory:
c:\JavaPrograms
Compiling and Running Another Program
Open the command prompt in Windows
To run the program that we just wrote, let us type the
following command:
cd c:\JavaPrograms
The command prompt should now look like this:
c:\JavaPrograms>
To compile the program that you wrote, you need to run
the Java Development Tool Kit Compiler as follows:
At the command prompt type:
c:\JavaPrograms> javac AnotherProgram.java
You have now created your first compiled Java program
named AnotherProgram.class
To run your first program, type the following at the
command prompt:
c:\JavaPrograms>java AnotherProgram
Object-Oriented Programming
As noted earlier Java is an object-oriented
programming language
For the rest of the course, we will start learning
the basic principles of object-oriented
programming.
We will start using these principles immediately,
and gradually progress into more advanced
concepts in the next few weeks.
Object
Definition: An object is an instance of a class;
made up of attributes and methods; attributes of
an object refer to its state; objects have State and
Behaviour.
Software Objects are often used to model realworld objects.
Example: electric fan has states (motor, blade,
power switch) and behaviors (speed change,
direction change, power on/off).
Object Examples
Example 1: Electric fans
States: brand name, color, size/height
Behaviours: rotate, on, off
Example 2: Cars
States: color, model, speed, direction
Behaviours: accelerate, turn, change gears
Class
Definition: A class is a term for describing a group or
collection of objects that have some common
properties; blueprint that defines the states and the
behaviours common to all objects of a certain kind.
In the real world, we often have many objects of the
same kind. For example, a ceiling fan, standing fan,
table fan . . .
Even though all fans have blades and rotate at
different speeds, each fan’s behaviour is
independent of other fans.
For example: while a ceiling fan is on, a table fan
may be off
Summary of OOP
When writing an object-oriented program, we
define classes, which describe categories of
objects, and the states and behaviours that they
have in common.
We then create objects which belong to classes,
and share the common features of their class.
Objects interact with each other by passing
messages.
You will be creating your own classes and objects
soon!
Exercise 1
Open Notepad++ and type the following piece of Java
code exactly:
public class FirstExcercise
{
public static void main( String arg[] )
{
System.out.println(“This is my first java
exercise”);
}
}
Save this file as FirstExercise.java in the following
directory:
c:\JavaPrograms
Exercise 2
Open Notepad++ and type the following piece of Java
code exactly:
public class SecondExcercise
{
public static void main( String[] args )
{
System.out.print(“This is \nmy second
\njava exercise\n”);
}
}
Save this file as SecondExercise.java in the following
directory:
c:\JavaPrograms
Exercise 3
Open Notepad and type the following piece of Java code exactly:
import javax.swing.JOptionPane;
public class ThirdExcercise
{
public static void main( String[] arg )
{
JOptionPane.showMessageDialog( null, “This is my
\nthird java exercise\n”);
System.exit(0);
}
}
Save this file as ThirdExercise.java in the following directory:
c:\JavaPrograms
Exercise 4
What is the difference between
A compiler and an interpreter?
Syntax error and a logical error?
Console application and Windowed application?
Explain the following terms
Computer program
Syntax
Debugging