0% found this document useful (0 votes)
59 views

Chapter 1-Introduction To Object-Orientation

This chapter introduces object-oriented programming concepts such as objects, classes, encapsulation, inheritance, and polymorphism. It discusses the differences between structured and object-oriented design approaches. The key aspects of object-oriented design like identifying objects and their characteristics, operations, and interactions are covered. The chapter also provides an example problem of automating a hotel reservation system to illustrate object-oriented design concepts. It introduces the Java programming language and some basic programming concepts like variables, data types, operators, and input/output in Java.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Chapter 1-Introduction To Object-Orientation

This chapter introduces object-oriented programming concepts such as objects, classes, encapsulation, inheritance, and polymorphism. It discusses the differences between structured and object-oriented design approaches. The key aspects of object-oriented design like identifying objects and their characteristics, operations, and interactions are covered. The chapter also provides an example problem of automating a hotel reservation system to illustrate object-oriented design concepts. It introduces the Java programming language and some basic programming concepts like variables, data types, operators, and input/output in Java.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 53

AACS2204 Object-Oriented Programming Techniques

Chapter 1
Introduction to Object-Orientation
Learning Outcomes

At the end of this chapter, you should be able to :


⮚ Explain what is structured design
⮚ Describe the object-oriented design approach
⮚ Write a simple program that uses an object for
console input
1. Structured Design
⮚ Dividing a problem into smaller subproblems is called
structured design.
⮚ The structured design approach is also known as :
▪ top-down design
▪ stepwise refinement
▪ modular programming
How Structured Design Works
Structured programming : The process of implementing a
structured design.
Structured Design
Step 1
Step 2
Huge Problem
Solution
Solution

Small Small Small Solution


Problem Problem Problem
Step 3
Solution
Solution Solution

Complete Solution
2. Object-Oriented Design
Object:
- a thing that has physical existence or significance
- any real-world entity which can have some characteristics
or which can perform some tasks

6
2. Object-Oriented Design
In object-oriented design (OOD), the steps in the problem-
solving process include-
OOD Example
⮚ Suppose you want to write a program that automates the
hotel reservation for customer to reserve hotel rooms.
⮚ What are the two main objects in this problem ?

room
? customer
?
Example of Data
⮚ customer name
⮚ number of rooms
⮚ check in date
⮚ room number
⮚ room status
⮚ etc
Examples of operations
⮚ Check the availability of the rooms
⮚ Change the status of the room once occupied
⮚ Check the number of room available
Object Characteristics
⮚ Each object consists of data and operations on the data.
⮚ In OOD, the final program is a collection of interacting
objects.
⮚ A programming language that implements OOD is called
an object-oriented programming (OOP) language.
OOD’s 3 basic principles
⮚ Encapsulation – all about wrapping variables and methods
in one single unit with the sole purpose of data hiding from
external classes.

⮚ Inheritance – the ability to create new (data) types from


existing (data) types.

⮚ Polymorphism – the ability to use the same expression to


denote different operations.
OOD basic principles
Classes
■ A class is an entity that determines how an object will behave
and what the object will contain.
■ Classes are used to create and manage new objects
■ In OOP languages (e.g. Java, C++), encapsulation is
accomplished via the use of the data types called classes.
■ In OOD, you decide which classes you need and what are their
relevant data fields (variables) and methods (functions for the
operations).
Classes
Implementation
⮚ In the implementation phase of the software development
process, you write and compile programming code to
implement the classes and methods that were discovered in
the design phase.
⮚ This course uses the OOD technique (in conjunction with
structured programming) to solve problems.
Object-oriented Programs
⮚ The final OO program consists of objects and methods.
⮚ Methods are designed to accomplish a specific task.
⮚ Some methods are part of the main program; others are
used to implement various operations on methods.
⮚ Objects interact with each other via method calls.
One of the OOP
Language Java

Other OOP C++ , C#,


Language
Vb.net, etc
Intro to Java, an OOP language
At the end of this session, you should be able to :
⮚ Use Java data types.
⮚ Write i/o statements.
⮚ Use the standard Java naming convention in programs.
Self-Review by Students
■ Appendix A1 Introduction to Java provides a brief intro to
the language
■ Revise your C or C++ programming topics using
■ Appendix A2 Elementary Programming
■ Appendix A3 Selections
■ Appendix A4 Loops
■ We will only go through parts of elementary programming
which are different in Java
The Java Syntax
The Java syntax is very similar to the C Language.
C Java
Similar:
#include<stdio.h>
int main(void) {}
int x; int x;
printf(“X = %d\n ”, x);
int x; int x;
scanf(“%d”, &x);

Different:
char c[] = “Love”; String s = “Love”;

--------------------------
c | L| o | v | e | \0 | s
Java operators
Java provides a rich set of operators to manipulate
variables. We can divide all the Java operators into the
following groups:
■ Arithmetic Operators (+, -, *, /, %, ++, --)
■ Relational Operators (==, !=, >, <, >=,
■ Bitwise Operators <=)
(&, |, ^, ~, <<, >>, >>>)
■ Logical Operators (&&, ||, !)
■ Assignment Operators (=, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=)
■ Misc Operators (Conditional Operator ( ? : ), instanceOf Operator)

22
Precedence of Java Operators:

23
Rules for Java Identifiers
■ Can only use: ■ Cannot be
✔ Letters 🗴 a reserved word (see
✔ Digits Appendix A of textbook list of
Java reserved words).
✔ Underscores (_)
🗴 true, false, null
✔ Dollar sign ($).
■ Must start with ■ Note: Java is case-sensitive
✔ a letter
✔ an underscore (_), or
✔ a dollar sign ($)
■ Can be of any length.

24
■ Valid identifier Invalid identifier

25
Java Numerical Data Types

26
Using Methods
■ In order to use a method, the user needs to know only
■ how to use the method and
■ what the method does
■ The user should not be concerned with the details of the
method, i.e. how the method is written.

27
Example: The Scanner class
■ The Scanner class is a predefined class in the Java library
for handling console input.

28
Console Input
1. Create a Scanner object
Scanner scanner = new Scanner(System.in);
2. Use the methods next(), nextByte(), nextShort(),
nextInt(), nextLong(), nextFloat(), nextDouble() or
nextBoolean() to obtain to a string, byte, short, int, long, float,
double, or boolean value.
Example:
System.out.print("Enter a double value: ");
Scanner scanner = new Scanner(System.in);
double d = scanner.nextDouble();
29
Sample Program for Console Input
■ TestScanner.java
Tip:
■ Benefit of using console input: we can have input and output re-
direction.
■ Input re-direction
■ Store input values in a text file and pass the file from the
command line using the “<“ symbol as follows:
C:\java TestScanner < input.txt
■ Output re-direction is also possible
C:\java TestScanner < input.txt > out.txt
30
Console output: System.out methods
Method Description
void print(char c) Print a character
void print(int i) Print an integer
void print(double d) Print a double value
void print(String s) Print a string.
void println() Terminate the current line by writing the line
separator string (usually a single newline character
‘\n’.

void println(char c) Print a character and then terminate the line.


void println(int i) Print an integer and then terminate the line.
void println(double d) Print a double value
void println(String s) Print a string and then terminate the line.
31
Type Casting

Comparing numeric types’ range

Range increases

byte, short, int, long, float, double

32
Examples of Type Casting
1. double d = 3;
2. int i = (int)3.0;
3. int i = (int)3.9;
4. int x = (int) (5 / 2.0);

For each example above:


■ Is it implicit or explicit casting?
■ Is it type widening or type narrowing?
■ What value is assigned to the variable?

33
Numeric Type Conversion
Consider the following statements:

byte i = 100;
long k = i * 3 + 4;
double d = i * 3.1 + k / 2;

■ An integer literal is by default of the int type.


■ An integer literal can be assigned to an integer type (byte,
short, int) without explicit casting as long as it can fit into
the variable.

34
What is a constant?

Why use constants?

35
Syntax for Defining Constants

final <datatype> <CONSTANTNAME> = <VALUE>;

Example:
final int MAX = 300;

36
Character Data Type (1)
char letter = 'A'; (ASCII)
Four hexadecimal digits.
char numChar = '4'; (ASCII)
char letter = '\u0041'; (Unicode)
char numChar = '\u0034'; (Unicode)

■ Java characters use Unicode.


■ Unicode
■ 16-bit encoding scheme
■ Supports the different world languages
37
Character Data Type (2)
Escape Sequences for Special Characters
Description Escape Sequence Unicode
Backspace \b \u0008
Tab \t \u0009
Linefeed \n \u000A
Carriage return \r \u000D
Backslash \\ \u005C
Single Quote \' \u0027
Double Quote \" \u0022

38
ASCII vs Unicode
■ ASCII Character Set is a subset of the Unicode
from \u0000 to \u007f.

39
ASCII Character Set

1-40
Casting char ↔ numeric types
■ Casting between char and numeric types is
similar as for the C language.

int i = 'a'; // Same as int i = (int)'a';


char c = 97; // Same as char c = (char)97;

41
The boolean Type
■ Boolean variables may only hold two values:
■ true
■ false
■ Examples:
boolean b = (1 > 2);
■ A boolean variable’s value may only be assigned to a boolean
variable.
boolean x = true;
int y = (int) x; // illegal statement
42
The String Type
■ String is a predefined class in the Java library.
■ Used to represent a string of characters, e.g.
String message = "Welcome to Java";
■ Not a primitive type, but a reference type.
Note:
• Any Java class can be used as a reference type for a variable.
• Reference data types will be thoroughly discussed in Chapter 4,
“Classes and Objects.”
• For the time being, you just need to know how to declare a String
variable, how to assign a string to the variable, and how to
concatenate strings.

43
String Concatenation
Examples
1. Three strings are concatenated
String message = "Welcome " + "to " + "Java";
2. String Chapter is concatenated with number 2
String s = "Chapter" + 2; // s becomes Chapter2
3. String Supplement is concatenated with character B
String s1 = "Supplement" + 'B';
// s1 becomes SupplementB

44
Formatting Output
■ Java has a printf method that is similar to C’s. The syntax is
System.out.printf(format, items);
where
format a string comprising format specifiers (%s, %d)
items a comma-separated list of numeric, character,
boolean or string values

■ Example:
int quantity = 10;
double salesAmt = 250.80;
System.out.printf(“Sales: Qty:%d, Amt:RM%.2f\n”,
quantity, salesAmt);

45
Programming Style & Documentation
■ Appropriate Comments
■ Naming Conventions
■ Choose meaningful and descriptive names to ensure
readability
■ See Java naming conventions for variables, method
names, class names and constant names
■ Proper Indentation and Spacing Lines
■ Block Styles

46
Appropriate Comments
■ Include a summary at the beginning of the program to
explain what the program does, its key features, its
supporting data structures, and any unique techniques it
uses.
■ Include your name, class section, instructor, date, and a
brief description at the beginning of the program.

47
Naming Conventions (1)
■ Variables and method names:
■ Use lowercase.
■ If the name consists of several words:
■ concatenate all words in one,
■ use lowercase for the first word, and
■ capitalize the first letter of each subsequent word in the name.
E.g. of variable names - radius and area
Eg. of method name - computeArea.

48
Naming Conventions (2)
■ Class names
■ Capitalize the first letter of each word in the name.
E.g. the class name ComputeArea.

■ Constants
■ Capitalize all letters in constants, and use underscores to
connect words.
■ E.g. the constant PI and MAX_VALUE

49
Proper Indentation and Spacing
■ Indentation
■ Indent two spaces.
■ Spacing
■ Use blank line to separate segments of the code.
public class ComputeArea {
public static void main(String[] args) {
double radius, area;

radius = 20;
area = radius * radius * 3.14159;

System.out.println("The area for the circle of radius " +


radius + " is " + area);
}
}
50
Block Styles
Use end-of-line style for braces.

51
Review of learning outcomes
You should now be able to
■ Explain what is structured design
■ Describe the object-oriented design approach
■ Write a simple program that uses an object for console
input

52
To Do
■ Review the slides and source code for Appendix A1, A2, A3 and A4.
■ Review the slides and source code for this chapter.
■ Read up the relevant portions of the recommended text.

53

You might also like