Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data...

Preview:

Citation preview

Java Review

Phuc H. Duong, M.Sc.phuc@it.tdt.edu.vn

CS502052 / Chapter 1

Outline1. Basic datatypes

2. Loop control

3. Decision making

4. Strings, Arrays

5. Java Object Oriented

2019 502052 - Chapter 1 1

Environment Setup• You should install the following software for this couse:

• NetBeans 8.2

• MS Visual Studio Code / Notepad++

• jBoss EAP 7.0.0

• PostgreSQL 9.6.3

• pgAdmin 4.1.6

• Java SE Development Kit 8u144 (add to JAVA_HOME also)

2019 502052 - Chapter 1 2

Basic Datatypes (1)• Variables are nothing but reserved memory locations to store

values

• This means that when you create a variable you reserve some space in the memory

• The operating system allocates memory based on your declaration

2019 502052 - Chapter 1 3

Basic Datatypes (2)• There are 2 datatypes in Java

• Primitive Data Types

• Built-in datatypes

• Reference/Object Data Types

• User defined

2019 502052 - Chapter 1 4

Basic Datatypes (3)• Primitive data types

• boolean true or false 1 bits

• byte twos complement integer 8 bits

• char Unicode character 16 bits

• short twos complement integer 16 bits

• int twos complement integer 32 bits

• long twos complement integer 64 bits

• float IEEE-754 floating point 32 bits

• double IEEE-754 floating point 64 bits

2019 502052 - Chapter 1 5

Basic Datatypes (4)• Primitive data types

2019 502052 - Chapter 1 6

Basic Datatypes (5)• Reference Datatypes

• Reference variables are created using defined constructors of the classes

• They are used to access objects

• Class objects and various type of array variables come under reference datatype

• Default value is null

• Can be used to refer any object of the declared type or any compatible type

2019 502052 - Chapter 1 7

Basic Datatypes (6)• Reference Datatypes

2019 502052 - Chapter 1 8

Outline1. Basic datatypes

2. Loop control

3. Decision making

4. Strings, Arrays

5. Java Object Oriented

2019 502052 - Chapter 1 9

Loop Control (1)• A loop statement allows us to execute a statement or group of

statements multiple times

• There are 3 types:

• while loop

• for loop

• do…while loop

2019 502052 - Chapter 1 10

Loop Control (2)• A while loop statement repeatedly executes a target statement if a

given condition is still true

2019 502052 - Chapter 1 11

Loop Control (3)• A for loop is a repetition control structure that allows you to

efficiently write a loop that needs to be executed a specific number of times

• It is useful when you know how many times a task is to be repeated

2019 502052 - Chapter 1 12

Loop Control (4)• A do…while loop is similar to a while loop, except that it is

guaranteed to execute at least one time

2019 502052 - Chapter 1 13

Loop Control (5)• Loop control statement

• When execution leaves a scope, all objects that were created in that scope are destroyed

• Java supports the following control statements:

• break statement: terminates the loop or switch statement

• continue statement: causes the loop to skip the remainder and back to prior to reiterating

2019 502052 - Chapter 1 14

Loop Control (6)• Enhanced for loop

• To traverse the collection of elements including arrays

2019 502052 - Chapter 1 15

Outline1. Basic datatypes

2. Loop control

3. Decision making

4. Strings, Arrays

5. Java Object Oriented

2019 502052 - Chapter 1 16

Decision Making (1)• Decision making structures have one or more conditions to be

evaluated

• Along with statement(s) that are to be executed if the condition is true

• And optionally, other statements to be executed if the condition is false

2019 502052 - Chapter 1 17

Decision Making (2)• There are 4 types:

• if statement

• if…else statement

• nested if statement

• switch statement

2019 502052 - Chapter 1 18

Decision Making (3)• It is always legal to nest if-else

statements which means you can use one if or else if statement inside another if or else if statement

2019 502052 - Chapter 1 19

Outline1. Basic datatypes

2. Loop control

3. Decision making

4. Strings, Arrays

5. Java Object Oriented

2019 502052 - Chapter 1 20

String (1)• String are a sequence of characters

• In Java, strings are treated as objects

• Java provides the String class to create and manipulate strings

2019 502052 - Chapter 1 21

String (2)• There are some popular methods:

• int length()

• char charAt(int index)

• boolean contains(CharSequence s)

• boolean equals(Object another)

• boolean isEmpty()

• String replace(CharSequence old, CharSequence new)

• String[] split(String regex)

• String trim()

• static String valueOf(int value)

2019 502052 - Chapter 1 22

Array (1)• Array, which stores a fixed-size sequential collection of elements

of the same type

• An array is used to store a collection of data

• Array is often more useful to think as a collection of variables of the same type

• Declaration

• Form: dataType[] arrayRefVar;

• Ex: int[] prime;

2019 502052 - Chapter 1 23

Array (2)• A representation of an array in memory

2019 502052 - Chapter 1 24

Array (3)• Example

2019 502052 - Chapter 1 25

Outline1. Basic datatypes

2. Loop control

3. Decision making

4. Strings, Arrays

5. Java Object Oriented

2019 502052 - Chapter 1 26

Java Object Oriented• Access modifier

• Inheritance

• The extends and super keywords

• Relationship: IS-A, HAS-A

• Types of inheritance

• Overriding

• Polymorphism

• Abstraction

• Encapsulation

2019 502052 - Chapter 1 27

Access Modifier• Access modifiers is to set access levels for classes, variables,

methods and constructors

• The 4 access levels:

• default: visible to the package, no modifiers are needed

• private: visible to the class only

• public: visible to the world

• protected: visible to the package and all subclasses

2019 502052 - Chapter 1 28

Inheritance (1)• Inheritance is the process where one class acquires the properties

(methods and fields) of another

• Information is made manageable in a hierarchical order

• Subclass vs. Superclass:

• Subclass - class which inherits the properties of other

• Superclass - class whose properties are inherited

2019 502052 - Chapter 1 29

The extends Keyword• extends is the keyword used to inherit the properties of a class

2019 502052 - Chapter 1 30

The super Keyword• The scenarios where the super keyword is used:

• to differentiate the members of superclass from the members of subclass, if they have same names

• to invoke the superclass constructor and methods from subclass

2019 502052 - Chapter 1 31

Inheritance - Example

2019 502052 - Chapter 1 32

Super.java à Sub.java à Test.java

IS-A Relationship (1)

• IS-A is a way of saying

• This object is a type of that object

• Example:

• Animal is the superclass of Mammal, Reptile, Dog classes

• Dog is the subclass of both Mammal and Animal classes

2019 502052 - Chapter 1 33

IS-A Relationship (2)• Example (cont.):

• Mammal IS-A Animal

• Reptile IS-A Animal

• Dog IS-A Mammal

• Hence, Dog IS-A Animal, as well

• Subclasses will be able to inherit all the properties of the superclass, except those are private

2019 502052 - Chapter 1 34

HAS-A Relationship (1)• HAS-A relationships are mainly based on the usage

• It determines whether a certain class HAS-A certain thing

2019 502052 - Chapter 1 35

HAS-A Relationship (2)• This shows that class Van HAS-A Speed

• By having a separate class for Speed:

• We don't have to put the entire code of Speed inside the Van class

• Making it possible to reuse the Speed class in multiple applications

• The Van class hides the implementation details from the users of the Speed class

2019 502052 - Chapter 1 36

When using IS-A and HAS-A• IS-A relationship

• Inherit and extend the functionality of the base class

• HAS-A relationship

• The class is using another class, thus it has it as a member

2019 502052 - Chapter 1 37

Types of Inheritance (1)

2019 502052 - Chapter 1 38

Types of Inheritance (2)• A very important fact to remember is that Java does not support

multiple inheritance

• To get rid of it, a class can implement one or more interfaces

2019 502052 - Chapter 1 39

Overriding (1)• What is the different between overriding and

overloading?

2019 502052 - Chapter 1 40

Overriding (2)• We can override a method when:

• a class inherits a method from its superclass,

• it is not marked final.

• Benefit of overriding is the ability to define a behavior that's specific to the subclass type

• A subclass can implement a parent class method based on its requirement

2019 502052 - Chapter 1 41

Overriding (3)• Rules for Method Overriding

• The argument list should be exactly the same as that of the overridden method

• The return type should be the same of the original overridden method in the superclass

• The access level cannot be more restrictive than the overridden method's access level

• A method declared final cannot be overridden

• A method declared static cannot be overridden, but can be re-declared

• Constructors cannot be overridden

2019 502052 - Chapter 1 42

Polymorphism (1)• Polymorphism is the ability of an object to take on many forms

• In other words, polymorphism allows you define one interface and have multiple implementations

• A superclass type can refer to a subclass type of object, but the vice versa is not possible

• Example:

• Let's consider Car example for discussing

• Take any brand like Ford, Honda, Audi, …

• For moving, each has their own advanced features and more advanced technology involved

2019 502052 - Chapter 1 43

Polymorphism (2)

2019 502052 - Chapter 1 44

Polymorphism (3)• Example (cont.):

2019 502052 - Chapter 1 45

Output???

Polymorphism (4)

2019 502052 - Chapter 1 46

Data Abstraction• Data abstraction or information hiding refers to providing only

essential information to the outside world and hiding their background details

2019 502052 - Chapter 1 47

Data Abstraction

Information Hiding

Data Encapsulation

Abstract Class (1)• A class which contains the abstract keyword in its declaration

• Abstract classes may or may not contain abstract methods; abstract method does not contain body

• If a class has at least one abstract method, then the class must be declared abstract

• If a class is declared abstract, it cannot be instantiated

• To use an abstract class, you must inherit it from another class, provide implementations to the abstract methods

2019 502052 - Chapter 1 48

Abstract Class (2)

2019 502052 - Chapter 1 49

Abstract Class (3)

2019 502052 - Chapter 1 50

Abstract Class (4)• Error version:

• Right version:

2019 502052 - Chapter 1 51

Encapsulation• Encapsulation in Java is a mechanism of wrapping the data

(variables) and code acting on the data (methods) together as a single unit

• The variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class

• It is also known as data hiding

• To achieve encapsulation in Java:

• Declare the variables of a class as private.

• Provide public setter and getter methods to modify and view the variables values

2019 502052 - Chapter 1 52

Best Practice

2019 502052 - Chapter 1 53

Constructor• A Java class should have 3 constructors:

• Default constructor

• Parameterized constructor

• Copy constructor

2019 502052 - Chapter 1 54

Example

2019 502052 - Chapter 1 55

Output?

Example• Output:

2019 502052 - Chapter 1 56

Do you see the problem here?

Constructor• There is an implicit "pointer" in Java

• The statement "Book y = x;" is not really an assignment, it is actually a pointer from y to x

• Therefore, if we change any value of y, x will be changed too

• From now, when defining a new Java class, we should have at least 3 constructors:

• Default constructor

• Parameterized constructor

• Copy constructor

2019 502052 - Chapter 1 57

Example (Fixed)

2019 502052 - Chapter 1 58

Example (Fixed)• Output:

2019 502052 - Chapter 1 59

Exercises

2019 502052 - Chapter 1 60

Question 1• Write program to count odd and even numbers in given integer

number.

• Example:

• n = 12345 à even = 2; odd = 3

• n = 412561 à even = 3; odd = 3

2019 502052 - Chapter 1 61

Question 2• Write a program to print a multiplication table, given n is the order

of table.

2019 502052 - Chapter 1 62

Question 3• Write a program to reverse a given number (not use String, Array).

• Example:

• n = 12345 à S = 54321

• n = 23980 à S = 8932

2019 502052 - Chapter 1 63

Question 4• Implement the following Java program based on the class diagram

below.

2019 502052 - Chapter 1 64

Question 5• Implement the following Java program based on the class diagram

below.

2019 502052 - Chapter 1 65

Question 6

2019 502052 - Chapter 1 66

• Implement the following Java program based on the class diagram below.

Question 7

2019 502052 - Chapter 1 67

• Explain why this code prints 0 rather than 7.

Question 8• Imagine you have two classes: Employee (which represents

being an employee) and Ninja (which represents being a ninja). An Employee has both state and behavior; a Ninja has only behavior.

• You need to represent an employee who is also a ninja (a common problem in the real world). By creating only one interface and only one class (NinjaEmployee), show how you can do this without having to copy method implementation code from either of the original classes.

2019 502052 - Chapter 1 68

Question 9

2019 502052 - Chapter 1 69

• Implement the following Java program based on the class diagram below.

END OF CHAPTER

502052 - Chapter 1 702019

Recommended