Professional Documents
Culture Documents
OOP Session 1 Slides
OOP Session 1 Slides
Module Overview
Programming using Java
MISM: Object Oriented Programming in JAVA
Email: kmatyokurehwa@gmail.com
Module aim
The module aims at empowering students with computer-programming skills using
structured and object oriented languages. The module will also cover the basic OOP
concepts, Exception handling in Java, Programming using Swings, Threads and
Frameworks in Java
OBJECTIVES
Continuous assessment is worth 40% of the total final mark
Objectives
In this Chapter you will learn to:
Understand Java Programming Language
Declare variables and Literals
Explore the NetBeans environment
Java Programming Language
Applets
Java is also used to create miniature, dynamic programs that run alongside or are
embedded within Web pages.
These programs are called applets and can be used to display maps, weather, games or
other interactive widgets or tools on a Web page.
Java Programming Language
Programming
Java is object-oriented and class-based.
Developers adopt and use Java because code can be run securely on nearly any other
platform, regardless of the operating system or architecture of the device, as long as the
device has a Java Runtime Environment (JRE) installed
Java Programming Language
Desktop Applications
These are the applications that we need to install on each machine like for example an
Anti-virus program.
Web Applications
These are applications that can run on the server side to create web pages and the most
commonly used is the Servlet.
Java Programming Language
Enterprise Applications
These are distributed applications like the banking applications and they can be accessed everywhere in
a given Country. In Java we use the EJBs (Enterprise Java Beans) to create enterprise applications.
Mobile
These are applications that are created for mobile devices. We are currently using the Java ME to create
these mobile applications.
Embedded System
These are systems in which the Computer that controls the device will be embedded inside that device
hence the name embedded system for example your wrist watch is an example of an embedded system
and we use the Oracle Java SE Embedded technology.
The Evolution of Java
1994 A Web browser was developed by the Team called HotJava that ran
applet programs.
1995 The Oak was renamed to Java
1996 Java was established as an Object Oriented Programming
Language
Basic Java Features
Multi-threaded
A thread is like a separate program, executing concurrently.
We can write Java programs that deal with many tasks at once by defining multiple threads
Distributed
We can create distributed applications in java.
RMI (Remote Method Invocation) and EJBs are used for creating distributed applications.
Portable
The ability to carry the Java byte code to any platform
Basic Java Features
Object-oriented
Object-oriented means we organize our software as a combination of different types of objects
that incorporates both data and behavior.
The object oriented language supports abstraction, encapsulation, polymorphism and inheritance
we are going to discuss more about these terms in the coming Chapters.
Platform Independent
A platform is the hardware or software environment in which a program runs.
The Java program is compiled by a Compiler and converted into byte code. The byte code is a
platform independent code because it can be run on multiple platforms (Write Once and Run
Anywhere (WORA).
Declaring Variables
When you come up with your program you will find that you need to create objects, these are
real world entities that we can see like for example a dog, person, cat and others.
These objects have got state and behavior like for example a person the state [what we know
about a person] could be the name of the person, hair color, height and others.
The behavior [what the person can do] could be a person can run, walk, talk, laugh and so on.
The state of an object is maintained in a variable and behavior will be shown through a method.
In programming the state of an object is shown through a variable.
A variable is just a unique identifier that stores a value in the program.
We can also show the behavior of the person through a method for example we can come up
with a method to talk, run and laugh and so on.
The variable should start with a data type that tells the compiler how the variable name should
be stored so let us look at the data types.
Data Types
A data type is simplest terms specifies how the data will be stored.
The data type is given to a variable name that determines the specific data to be contained
in the variable.
There are two types of data types: the primitive and non-primitive
Primitive data type:
These are the data types that are predefined by the language and we have eight of them that are
supported in Java.
Byte
This is normally used when you are dealing with large arrays to save space because a byte is
almost four times smaller as compared to an integer data type.
The default value for the byte is 0
Short
We can use this data type to save memory also when you are dealing with large arrays where
memory saving is critical as the short data type only uses 16 bits as compared to the integer that
consumes 32 bits
The default value for the short is 0.
Primitive data type:
Int
This is a 32 bit integer. The int data type is mostly used to control loops and also used with
arrays.
The default value for the int is 0.
Long
This is a 64 bit data type and is commonly used when you feel the int data type will not
hold enough space.
The rule is you only use the long data type is the range of values is wide enough to the
extent that the int data type cannot support it
The default value for the long is 0L.
Primitive data type:
Float
This is a single- precision 32 bit floating point.
This data type is normally used when you want to save memory when you are dealing with large arrays of floating points as
compared to using the double data type.
However this data type should not be used when you are dealing with computations that require precise values like the
currency.
The default value for the long is 0.0f
Double
This is a double-precision 64 bit floating point. Generally for most decimal values, this is the default data type.
However this data type should not be used when you are dealing with computations that require precise values like the
currency.
The default value for the long is 0.0d.
You should use floats instead of double when memory usage is critical. If you need more precise computations, use doubles
Primitive data type:
Boolean
This data type has got two possible values: true or false. This data type is normally used to
track those conditions that evaluate to either true or false.
The default value for the Boolean is false.
Char
This is a 16 bit Unicode character. The char data type is used to store any character.
The default value for the char is '\u0000'.
Non-primitive data type:
The non-primitive data types are not defined by the language itself but by the programmer
who will writing the code.
They are at times called “reference variables” or “object references” because they
simply reference a memory location that stores the data.
One interesting thing is that the information that is going to be accessed by these reference
variables will be eventually stored as a primitive data type.
So in other words we are saying the variables of non-primitive data type they simply
reference to the actual value stored in the primitive variable.
Defining variables and Literals
A variable provides a storage space that can be used to store a value for a program.
Each variable will be of a specific type like int for integer, char for character,
Boolean, string, double, float and these are all called data types.
Types of variables in Java
Local variable:
These are the variables that are declared inside a method or a constructor, the scope
of the local variable or the visibility of the variable is only confined to the method
only.
Local variables do not have default values hence once you declare a local variable
you need to assign a value to the variable failure to do that if you try to call the
variable later in the method without a value assigned this will generate an error.
Defining variables and Literals
Instance variable:
These are the variables declared in a class but not inside a method or a constructor. The values that
the instance holds may be referenced by many methods. For instance variables we can use access
modifiers like public or private.
If you declare an instance variable and do not assign it a value it will be given a default value.
Class variable:
These variables are declared with the keyword static in the class but outside the method
These variables are rarely used and the moment they are declared with the static keyword it means
the value assigned to the variable will not change.
The class variables also have the same default values as the instance variables.
If we declare the class variable as public static final then the variable name itself should be in
uppercase
Variable naming conventions
A literal is a sequence of characters that could be letters, digits and other symbols representing a value to be
stored. The literals can be Boolean, integer, String or Char data types.
Integer literals
The following primitive data types such as byte, short, long and int can all be created from the int literal. An
example of an integer literal can be like this: int age = 50;
Floating point literals
These are numerical values but with some decimal points for example: double sum = 90.50;
Character literals
These are shown in single quotation marks for example char answer = ‘Y’;
String literals
These are enclosed inside double quotation marks for example String name = “Joana”;
Boolean literals
These either have a True or False value for example boolean married = true;
The NetBeans Environment
We are going to have a look at the NetBeans environment and see how we can create a
project in NetBeans IDE
Double click on the Netbeans icon and you will see a screen like the following:
The NetBeans Environment
Un-check the Show On Startup checkbox so that it will not appear next
time. Click on File menu tab and select New Project
The NetBeans Environment
In the Categories section select Java and in the Projects section select basic
Java Application and click Next button.
The NetBeans Environment
This interface enables you to give a name for the project you are creating.
You will find that the Project Name will be given the same name as the
name of your Main Class . Click the Finish button
The NetBeans Environment
In java we have comments and the line shown as a comment will not be
executed when the program runs and the comments are there to assist the
programmer on what a section of code does
Let us declare the following variable names of type integer.
int age1, age2, age3; //we are coming up with three variable
//declarations age1 to age 3 which are of type int
In Java each statement should end with a semi-colon; if you do not end
with a semi-colon you will see an error message. An error message is
generated by the Java compiler so that you correct your code again
}
Variable declaration
Let us write a program to assign these variables the values: use the correct data
types:
age = 60;
score = 50.70;
name = “your name”;
gender = ‘M’;
Married = “False”;
Task 1:
You have been asked to create a class in Java called Human and then in the
method you need to declare the variable names that describe a human being
together with their data types:
for example a human being is identified by a name and this will be of type
string.
Come up with the other five variable names that describe a human being
Exercises:
Solution: Task 1
In your main one of the possible variable names you can use could be the
following:
int shoeSize;
int height;
String lastName;
String address;
String hairColor;
Summary of Chapter 1
Main
Student p = new Student();
P.method();