Download as pdf or txt
Download as pdf or txt
You are on page 1of 51

SOFT7004

Object Oriented Principles


Java

Triona.McSweeney@cit.ie
Week 1 Introduction

2
About Me

• Assistant Lecturer in the Department of


Computer Science
• Experience in wide variety of industries:
R&D Engineering, software & hardware,
electrical, energy, pharma, project
management

3
Course Details
• Title: Object Oriented Principles
• Module Code: SOFT7004
• Module Description: Object Oriented Programming is a
programming paradigm where data and operations can be
defined together as objects and allows reuse of these objects
within the same or other programs. In this module students
will learn object creation and design; how to store objects in
an array or data structures; exceptions; basic file input,
output and testing..
• Credits: 5

4
Course Details
1. Discuss the fundamental principles of Object Oriented Programming
(OOP).
2. Identify the benefits of object-oriented design and implementation.
3. Design and develop a program from a high level specification.
4. Use exception handling to deal with unexpected events that occur
during the execution of a program.
5. Using the appropriate library in the Object Oriented Programming
language, persist and read data to and from a file.

Note that these outcomes will be enabled by


new content focussed on Java development.

5
Course Details

Assignment Description LOs Due Weight

MCQ Questions will be material presented in class. 1,2 Wk 8 20%

The purpose of this assessment is to assess


the lab work of the students.For example,
Practical/Skills the student may be required to develop a Every
1, 2, 3,4 30%
Evaluation class or collection of classes that achieve a 2nd week
certain task, showing good object oriented
principles throughout.
An example project may be to develop a class
or collection of classes to achieve some
requested functionality. The student may be
Project expected to design an application composed 1,3,4,5 Wk 13 50%
of classes demonstrating some key
principles and technologies of OOP, handle
Exceptions, read and write to a file.

6
Methodology
Lectures

1.5-2 hour lecture (Wednesday 6.15-8.15pm)

Combination of lecture slides, code demo + problem solving

Labs

Discussion forum on Canvas with questions about labs. Feel free to help
each other out! Interaction is a great way to learn!

7
Assessment - Marks & Standards
Coursework which is submitted after the submission date will attract a
penalty as follows:

❏ Up to 1 calendar week delay 10% of the marks available for the


assessment
❏ Up to 2 calendar weeks delay 20% of the marks available for the
assessment
❏ Over 2 calendar weeks delay 100% of the marks available for
the assessment

8
Assessment - Marks & Standards
If valid extenuating circumstances have been established in accordance
with Institute guidelines and to the satisfaction of the MEB, then the
MEB may, at its discretion:

❏ remove or mitigate any late submission penalty

and/or

❏ record a Deferral to enable the candidate to repeat or be


reassessed in the module as a first attempt.

9
Course Prerequisites
• Prerequisites:
– No prior programming experience
assumed
• Who should be taking this course:
– students who want to learn
programming using one of the most
popular languages in the world

10
Course Information

• Canvas
• Google Community
• Post information
• Ask questions
• Help each other
• No cheating!

11
Civility
• In an effort to make this class enjoyable for
everybody…
– Please be on time to class!

– If you have a question, just ask.

12
Integrated Development Environment
(IDE)
• For the course, you may use any IDE you are
comfortable using. I will use the following in the
classroom:
– Eclipse
• All these products can be downloaded from the
web for free.

13
Basics of Java
• Java is a popular programming language, widely used
in industry.
• We will learn all the specifics of how to program in
Java.
• This includes all the peculiar rules that are specific to
Java.
• We will cover the fundamentals: Variables,
Arithmetic, If / Else, For Loops, While Loops, Arrays,
Methods, etc.

14
Why Java?

15
Principles of Software Development
• Building high quality software is very difficult.
• The course presents the syntax and concepts of
programming, and also presents strategies for
building real software that addresses real problems.
• I will also try to bring my real-world industry
experience to class.

16
What Is a Computer?
• Computer
– Performs computations and makes logical decisions
– Millions / billions times faster than human beings
• Computer programs
– Sets of instructions by which a computer processes data
• Hardware
– Physical devices of computer system
• Software
– Programs that run on computers
Machine Languages, Assembly Languages,
and High-level Languages
• Three types of programming languages
– Machine languages
• Strings of numbers giving machine specific instructions
• Example:
1010101010
1010101110
1010101010
– Assembly languages
• English-like abbreviations representing elementary
computer operations (translated via assemblers)
• Example:
LOAD BASEPAY
ADD OVERPAY
STORE GROSSPAY

18
Machine Languages, Assembly Languages, and
High-level Languages
–High-level languages
• Instructions closer to everyday English
• Use mathematical notations (translated via
compilers)
• Example:
grossPay = basePay + overTimePay
• Interpreter – Executes high level language
programs without compilation.

19
Computer Architecture

Memory

CPU

Bus

20
High Level Languages
• Procedural – focus on instructions that
operate on data - boxes of data in memory.
• X = 5; [___________] X
• Y = 3; [___________] Y
• Z=X*Y [___________] Z

• This can become hard to design and maintain.

21
High Level Languages
• This can become hard to design and maintain.
• Name = “Paul”
• GrossPay= 1500;
• Tax=500;
• Name2 = “Joe”
• Tax2 =6000;
• GrossPay2 = 12000; Code becomes
• NetPay=GrossPay-Tax; complex very
• NetPay2=GrossPay2-Tax2; quickly!
• Print (Name);
• Print (NetPay);
• Print (Name2);
• Print (NetPay2);

22
23
24
Think in Terms of Objects
• The accepted philosophy is to think in terms
of objects

25
Think in Terms of Objects
• The accepted philosophy is to think in terms
of objects

26
The Key Software Trend: Object
Technology
• Objects
– Reusable software components that model items in the
real world

27
The Key Software Trend: Object
Technology
• Objects
– Meaningful software units
• Date objects, time objects, paycheck objects, invoice objects,
audio objects, video objects, file objects, record objects, etc.
• Any noun can be represented as an object
– Very reusable

https://material.angularjs.org/latest/demo/datepicker 28
The Key Software Trend: Object
Technology
• Objects
– More understandable, better organized, and easier to
maintain than procedural programming
– Favor modularity

29
Basics of a Typical Java Environment

• Java programs normally undergo five phases


– Edit
• Programmer writes program (and stores program on disk)
– Compile
• Compiler creates bytecodes from program
– Load
• Class loader stores bytecodes in memory
– Verify
• Verifier ensures bytecodes do not violate security requirements
– Execute
• Interpreter translates bytecodes into machine language
30
Disk
Class Loader
Compiler
Editor
Bytecode
Interpreter
Phase 1 Verifier
2
3
4
5
Program is created
in an editor and
stored on disk in a
file ending with .java.

Compiler creates
bytecodes and
stores them on disk
in a file ending with
.class.
Primary
Memory
Class loader reads
.class files
containing
bytecodes from disk
and puts those
bytecodes in
memory.
..
. ..
.

Primary
Typical Java
Memory
Bytecode verifier
confirms that all
bytecodes are valid
environment
and do not violate
Java’s security
restrictions.

..
. ..
.

Primary
Memory Interpreter reads
bytecodes and
translates them into
a language that the
computer can
understand, possibly
storing data values
as the program
executes.
..
. ..
.

31
Another Basic Step for Java Programming
• Debugging
– Check program execution and output to ensure
program compiles and runs as expected
– If it doesn’t, make corrections in the edit phase
and repeat the remaining steps

32
Getting Started

33
Java Building Blocks
• Statements – composed of literals, variables, constants,
expressions, declarations etc
• Code Blocks
– Sequence of statements
– Delimited by curly braces: indicate scope of a declaration
• Methods
– Signature + implementation block
• Classes
– Set of methods and fields (each field is a variable or a
constant)
– Objects: each object is an instance of a class
• Packages
– Set of related classes

34
Time for some Java Code:
HelloWorld.java

public class HelloWorld {


public static void main(String[] args) {
System.out.println(“I rule!");
}
}

35
36
HelloWorld Unpicked
• public is a modifier: it says a definition is
externally visible
• class is a keyword: tells the compiler a class is
about to be defined
• HelloWorld is the name of the class being defined
• static is a modifier: states that a definition
belongs to a class, and not to specific objects of
that class
• void is a keyword: it indicates that the method
being defined does not return any value

37
HelloWorld Unpicked II
• main is a method name, but it’s a special
name: it indicates the program can be invoked
when starting up a JVM
• (String[] args) declares the
parameters for the method:
– In this case a single parameter
– The parameter is called “args”
– The type of the parameter is an array of String
– The square brackets “[]” indicate array
– String is a built in Java type (class)
38
HelloWorld Unpicked III
• The method body consists of a single statement:
– System.out.println(“Hello World”);
• System is the name of a class
• It is defined in package java.lang
• Most packages have to be explicitly imported
– But java.lang is implicitly imported
• out is a static variable in class System
• Its type is PrintStream
• println is a method of PrintStream
• “HelloWorld” is a string literal
• The statement invokes the println method to print a value
out to the console

39
Demo Setup Tools and Run
• For Windows
• Step 0: Install JDK. To use Eclipse for Java
programming, you need to first install Java
Development Kit (JDK). ...
• Step 1: Download. Download Eclipse from
http://www.eclipse.org/downloads. ...

40
Java and Types
• Java is a strongly typed language
• Every time a variable is introduced
• Its type is declared
• Example – Java: int x = 2;

41
Java Class Structure
• Java program code is organised into classes
• We’ve just seen one such class: HelloWorld
• Classes can be executed if they have a
properly defined main method
• Many classes cannot be directly executed
• Instead they define constants, variables, and
methods for use by other classes
• Mostly, the order of definitions within a class
is not significant
• I usually declare variables before methods
42
Typical Class Structure
Italics indicate where to place your own definitions
public class MyClass {
// some variable declarations or definitions
// some method definitions
}

Note: a declaration declares the name of a


variable or method e.g. int x;
A definition gives it a value e.g. int x = 10;

43
Method Structure: Signature
• Two parts: signature and body (definition)
• Signature specifies:
– Visibility (e.g. public)
– Scope: e.g. static (belongs to the class)
– Return type: e.g. int
– Parameters e.g. (int x, int y)
– Each parameter consists of a variable declaration
– The name and type of each variable is specified
– The variable is bound to a value when the method
is invoked (called)
44
Method Body
• A code block (sequence of statements between curly braces)
following the method signature
• The code block is executed when the method is called
• Can access the values of the parameters
• Introduce local variables (these will only exist while the
method is being executed)
• Evaluate expressions
• Call other methods
• Note: any method with a non-void return type MUST return
a value
• This is specified with the return statement, and is the last
statement executed within the method body

45
AddTwo

public class AddTwo {


// declare two class variables
static int x = 2;
static int y = 3;
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
// x and y are the arguments
// passed to the method
System.out.println(add(x,y));
}
}

46
Dealing with multiple values: arrays
• Java has many ways of dealing with collections
of values; we’ll study many of them later in
the course
• A simple one is the array – we already saw this
in HelloWorld
• Arrays are tables with indexed access
• We also need a way to access elements of
arrays

47
Arrays and simple loops
int[] a = {10, 20, 30} ; // an array of int
int b = a[0]; // set b to first element of a
int c = a[1] + a[2]; // what will c be now?

Java has a nice (enhanced) for loop to iterate over


an array:
for (int x : a) {
// do something for each “x” in “a”
System.out.println(x);
}

This is also called a for-each loop

48
Useful Resources
Eclipse Download
http://www3.ntu.edu.sg/home/ehchua/programming/howto/ec
lipsejava_howto.html
Books
Head First Java
Videos
Derek Banas Java Video Tutorial
Online:
https://www.learnjavaonline.org/
https://www.codecademy.com/learn/learn-java
Eclipse

49
Exercise
• Download and Set up JDK and Eclipse
• Modify the Hello world to multiply, subtract
and divide integers
• Start reading Head First Java book
• Look at videos
• CodeAcademy:
https://www.codecademy.com/learn/learn-java

50
Summary of Introduction
• Java platform is vast and extremely powerful
• Can be great fun to learn
• Implement well designed solutions to interesting
problems
• So far we’ve covered some important material:
– HelloWorld
– Basic syntax for declaring classes and methods
• In the coming weeks we’ll study and write more
interesting programs!

51

You might also like