You are on page 1of 25

Object Oriented

Programming
CPE313,MIS313,ISYE313,CMPE313,IT31,ITEC313,ISE313

by
Dr. Cain Kazimoglu

Info about resources


I claim no rights in the material used for making these
presentation slides. The slides are prepared in a way to avoid
using copyright-protected material. As the lecturer of the
course, I compiled a variety of resources in order to produce
recent and correct information fitting to the content of the
course.
I have no content claim on any and all of the material used
in this course including text, images, programs and other
materials used. The lecture primarily uses a variety of books –
which are all mentioned on the Moodle page of the course.
While the majority of the exercises are prepared by me, the
copyright of all materials used belong to their original
owners.
- Asst. Prof. Dr. Cain KAZIMOGLU

1
Contact Details

Cagin (Cain) Kazimoglu


Office : ST206
Telephone : 0392-671 11 11 - ext: 2416
E-Mail : ckazimoglu@ciu.edu.tr

Course Details
The course will be taught in two different phases :

Firstly, the course covers the fundamental constructs of


programming and the concept of programming through
Graphical User Interface (GUI). At this stage, you get fond
of java and its syntax.

Secondly, the course covers the paradigm of object


oriented programming : classes, objects, auxiliary classes,
inheritance, abstract classes, interfaces and
polymorphism.

2
How to survive this course

 Attend the lectures regularly and ask questions in the class


 Complete the lab work.
 Make up your own log book.
 Complete your coursework
 Pass the exams
 Do not memorise anything, try to practice often
 Practice makes perfect.

This is not how you learn


programming
 Using the right way to study is the key for your success

 Remember that programming is a skill, treat learning


programming like driving a car, going to gym, or
swimming rather than watching a NetFlix show.

3
This is how you should learn
programming
Pair programming, discussing with your colleagues,
generating abstract solutions through team-work is
the key in being sucessfull in learning programming

Use the language

 You will only become a competent programmer by


programming.
 Keep writing programs and keep the programs you
write.
 Comment, Comment and Comment.
 Read other people’s programs.
 Adapt and extend your own and those of others.
 Don’t get discouraged and you will succeed.

4
Why Java? Why not any
other language?

Java is the most popular Object-


Oriented Language to date

Without a doubt, Java is one of the most widely-used OOP


languages on the market.

Part of the reason for the widespread use of Java is because


it’s one of the official languages for Android development.
With Android the most popular mobile operating system on
the planet, Java isn’t going to be knocked off that top spot
for some time.

10

5
What is Java? (1)
 Java is an OOP (Object Oriented Programming)
language developed by James Gosling of Sun
Microsystems in 1990.

 It is widely used for the development of Platform


independent (and other distributed) software.

 It can be used to develop apps for mobile phones, web


enable database applications. The language itself has
inspired many different script languages such as
ActionScript of Flash applications.

11

What is Java? (2)


 Its features include:
 neutrality (i.e. it doesn’t depend on a specific
type of machine)
 Security (reverse engineering)
 a small number of class files or JAR files is easy to reverse engineer,
however, larger java applications e.g. J2EE that use many hundred JAR
files and have hugely complex build scripts can be difficult to reverse
engineer just because of the complexity of the code.
 Robustness (i.e. it always works! Platform
indepedence)
 and Multi-threading (i.e. it can do two things at
once).
 It offers extensive application programmer interfaces (APIs)
including support for graphical user interfaces (GUIs), utilities,
database access, networking etc.

12

6
What is the current version
of Java?
 The current stable and long-term version of Java
is JAVA SE 17.

 This version of Java supports a new syntax that


reduces the verbosity associated with writing
Java, Dynamic Class-File Constants, Arbitrarily
Low-Overhead Garbage Collector and many
other features.

13

How Java work


Assembly

Low Level Language

Java
compiler
High Level

Hardware & Software

Machine Language
101010100100111

bit

14

7
Interpreter & Compiler

High
Level
Languages
(source code)
Compiler

Machine language
Compiler converts source code in three steps : (object program)

1. Lexical Analysis
Machine controls if corresponding commands have equivalents in machine
language
2. Syntax Analysis
Checks the grammer rules of the programming language chosen
3. Code generator
Converts programming code into machine language

15

An example to compiling and


interpreting: JAVA

Writing program

.java 001010100...

interpreting interpreter

compiler .class

16

8
What can you do with
Java?
Amongst many other things, you can:
 Produce stand-alone applications which run directly on a
computer (or a mobile).

Windows machine Mac Mobile


(Android)
Unix / Linux system

17

How Java provides


portability
 Java virtual machine (JVM) is a virtual machine that can execute
Java byte code. It is the code execution component of the Java
platform.

 JVMs are available for many hardware and software platforms.

 The use of the same bytecode for all JVMs on all platforms allows
Java to be described as a write once, run anywhere
programming language, versus write once, compile anywhere,
which describes cross-platform compiled languages.

 Thus, the JVM is a crucial component of the Java platform.

18

9
What else can you do with
Java?
 Android phones have Java installed as standard.

 Java which is different to Javascript, is not supported on


iPhone. You can however look in the App Store for 3rd party
Browsers that offer support for it.

19

What else can you do with


Java?
 Produce servlets on a world-wide web server. Unlike
applets, these run on the host machine and typically are
used to provide dynamic content using web databases.

20

10
Creating Java applications
Ordinary text created
MyProgram.java
1.Editor and saved by you
(source code)

Byte code
MyProgram.class created and saved
2.Compiler by compiler

Assembled and
3.Loader Library connected with all
classes used

Now in
machine memory
Byte code
translated and
executed on your
MyProgram
4.Interpreter runs
machine

21

Integrated Development
Editors (IDEs)
 To develop your application you will need to write your
instructions for the computer.

 You will learn a computer language, in our case JAVA,


involving both syntax (initially the most difficult to get right)
and semantics.

 Syntax is to do with the way a language is written – similar to its


grammar and punctuation.

 Semantics is to do with the meaning of the language.

22

11
A variety of places to write
code

23

Notepad
 A standard windows accessory. This is probably a terrible
choice for the long term, but please be aware that you do not
need an IDE to write Java codes – you only need a SDK to
compile

Pros
 Simple to use.
 Can write and edit large amounts of text.
Cons
 It gives no help with format or errors.
 It has no additional facilities for building GUI.
 You cannot compile and run a program directly from this editor.

24

12
NetBeans
Pros
 Netbeans is the official ide by oracle.
 Integrated development modules for JAVA.
 Easy to use Swing GUI design tool (drag & drop).
 It can give hints on debugging code.
 Autocompletes code and provide hints
 Version control system and many advanced tools.
Cons
 Takes up more memory than Eclipse and other IDEs
 You might need some training to use advanced toolset

25

Eclipse
Pros
 Fairly straightforward installation
 Can write and edit large amounts of text.
 It can give hints on debugging code.
 It can be linked to compile and run from the editor.

Cons
 Does not have drag and drop GUI features by default
(you can add this feature through a plug-in though)
 Learning-curve, can be a bit complicated at the start.

26

13
IntelliJ

Pros
 Developed by the same people as Android Studio
 Once advanced in Java, this IDE can do a lot of
sophisticated work.
 Displays data types as you type along.

Cons
 IntelliJ is not free; it has a price (there is, however, a
community edition)
 Small ecosystem and a singly company product.

27

How to create Java


application
This involves the following steps:
 Write the Java source code - saved (by you) as
Filename.java. You can use an IDE for this purpose,
although you could use any text editor, even Notepad ...

 … but never Microsoft Word.

 Compile and run the Java source code –


 You can do this by selecting Run | Run File.

28

14
Java for dummies ???
 Some of you will be experienced programmers already and
this will be familiar – however, for many this will be a new
experience.

 At the start you may feel you are copying a lot of words down
which you don’t yet understand.

 We are trying to take one step at a time so we will leave some


of the detailed explanation until later.

 For the moment just copy the Java we give you while you
learn the process of editing, saving, compiling and executing
programs.

29

First Exercise : “Hello World”


Start Eclipse/Netbeans/IntelliJ with and open a new
project project using File | Java Project

30

15
Create a new Java Project
The latest version of JDK 17.

However the exercises we will do in the class would work in


version 1.7 or earlier versions of Java.

31

Create a new Java Class

32

16
Coding in Java

33

First Java Program :


Hello World

34

17
package myFirstProject;

// this is my first java program


public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world");
}
}

The third line is a Java comment and will


not be used by the machine but is there for
you to read.

35

System.out.format
public class Root {
public static void main(String[] args) {
int i = 2;
double r = Math.sqrt(i);
System.out.format("The square root of
%d is %f.%n", i, r);
}
}

36

18
Case Sensitivity

 Note that:
 Java is case sensitive. For example HelloWorld is
not the same as helloWorld.
 Spaces are significant. For example Hello World
is not the same as HelloWorld.
 The file name must be the same as the class name
(class HelloWorld, file HelloWorld.java here).

37

A Java program consists of a


number of classes, which contain
methods, which contain statements

package myFirstProject;

// this is my first java program


class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world");
}
}

38

19
Main method in Java apps

Note that all applications must have a main method


(although applets and servlets don’t need one). It is the
starting point for the program.

On the other hand, you will discover that not all java
classes have a main method due to its object oriented
structure. Please be aware that there is usually only one
main class in a java application.

39

Example – HelloWorld

package myFirstProgram;

// this is my first java program


class HelloWorld {
public static void main(String[] args){
System.out.println("Hello world");
}
}

This statement prints Hello world to the


required output.

40

20
Running hello world
 Running a java program

To run Hello1, use the menu


item Run | Run File or use the
relevant shortcut.

The output should appear in


the bottom pane of your IDE.

41

New Exercise :
HelloName.Java
Now we will run a new Java class called
HelloName.java which should look like this:
Note : import static
javax.swing.JOptionPane.*;
Means import all system classes
under swing library

42

21
Variables

Variables hold values (in this case a String like


“Cagin” or “Cain”) within a program, and
the values of the variables can change as the
program runs.

In this case, name gets its value from an input


dialog.

43

Running HelloName.java
 Use Run File and type a name into the
input dialog box.

44

22
The Variable name

 Java is expecting a String to be put in for


name.

 This is a reusable variable so we can overwrite


the value many times.

 At any one time it only has one String value.

45

public class HelloName {

public static void main(String[] args) {


String name;
name = JOptionPane.showInputDialog("Input?");
System.out.println("Hello " + name);
}
}

JOptionPane.ShowInputDialog is an assignment
statement which takes a value from the prompt such as
“Cain” whatever the user typed – and puts it into the
variable name on the left hand side.

46

23
Self-test Exercises

1. What output would you expect if you input the


word Cain?
2. What output would you expect if you input the
words Johnny Depp?
3. What happens if you hit OK at the prompt
without inputting anything? (Try it).
4. If line of the code is changed to
"Hello " + name + ". How are you?"
what output would you expect if the input is
Michael?

47

More about modern IDEs


 IDEs recognises java code automatically and
colours it. If you mistype a word the
Eclipse/NetBeans will underline it.
 This colouring is not just to make your programs look
pretty. It helps you to spot mistakes as you type, for
instance:
 If you type Public instead of public it won’t be
coloured blue.
 If you forget to type a closing string quote " the
rest of that line will be blue.
 There is masses more to discover about modern IDEs
which we shall find out throughout the course.

48

24
What next?
 Tutorial in labs (each week):
 See the tutorial sheet for each week’s exercises.
 Use IDEs (Eclipse ,Netbeans or IntelliJ) to write, save,
compile and run.
 Bring a flash disk to back-up your programs.
 Keep your folders and files organised.

 After the tutorial / at home (each week):


 Finish any exercises you didn’t complete in the labs.
 Read the corresponding chapter from online
resources or the suggested (reference) books
 Search for more online exercises.

49

Revision
 What is Java? What can Java do for you?
 What is an Interface Development Environment (IDE)?
 Eclipse ,Netbeans or IntelliJ as development environments
 First Java Program : Hello World!
 Hello Name exercise.

50

25

You might also like