Chapter 1-Introduction To Object-Orientation
Chapter 1-Introduction To Object-Orientation
Chapter 1
Introduction to Object-Orientation
Learning Outcomes
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.
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’.
Range increases
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);
33
Numeric Type Conversion
Consider the following statements:
byte i = 100;
long k = i * 3 + 4;
double d = i * 3.1 + k / 2;
34
What is a constant?
35
Syntax for Defining Constants
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)
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.
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;
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