Java Oop
Java Oop
Introduction to OOP
Lecture 1
Introduction to OOP
OOP Language
OOP Stands for "Object-Oriented Programming." refers to a programming methodology based on objects,
instead of just functions and procedures. These objects are organized into classes, which allow individual objects
to be group together.
In this course Java Language will be used to introduce Object Oriented Programming.
Java Editions
1- J2SE (Java 2 Standard Edition):
Java Platform Standard Edition is known as Core Java and is the most basic and standard version of Java. It is
the purest form of Java and is the foundation of all the other editions. It contains a variety of general-purpose
APIs, including Java.Lang, Java.util, etc.
The following line says that this program is a class called HelloLibya
public class HelloLibya
{
public static void main(String[] args)
{
// welcome the user
System.out.println(“Hello Libya.”);
}
}
SAQ 1
What do you understand by the terms, comment, special symbol, identifier and keyword? Annotate the above
classes showing examples of each. Which elements of the above classes are essential in order to get the class to
compile?
Integer Expressions
Integer expressions are formed using integer values (whole numbers) and integer operators (+ - / * %).You may
directly output the results of arithmetic expressions using println and print as shown in the class fragment below:
SAQ 2
Identify the operators available for use with integers; put the operators in order of precedence.
Exercise 1
The only operator that some of you are likely to be unfamiliar with is the modulo operator. For positive numbers
it gives the remainder of an integer division, e.g. 7 % 2 yields 1. For negative integers the modulo operator
requires a little bit more consideration.
Exercise 2
Consider Exercise 1.2 in Bailey and Bailey. This requires you to write a program to discover the various results
of using the % operator. e.g. – 16 % 3, 16 % -3, -16 % -3. Work out what results you would expect, and then
modify your first Java program to test the results.
Exercise 3
Amend the above class so that it also calculates and outputs a mean for the scores.
SAQ 3
What is the meaning of the following terms : final values; declaration; assignment.
Consider the class on page 16-17 of Bailey and Bailey, which provides a sequence of assignment statements.
You should notice a key feature of the statements in this class is that the same identifier appears on the left hand
and right hand side of the = operator.
sum = sum + 1;
Exercise 4
Consider what would be output at the end of the following fragment of code:
Sum = 26;
Sum = sum + sum * 2;
System.out.println(sum);
Finally, consider what happens when you attempt to assign out of range values to an integer variable. Bailey and
Bailey show on page 13 what would happen when you attempted to give an integer an explicit value larger than
2147483647. The compiler would not however, detect the following violation:
big = 2147483647;
big = big +1;