java review - duong huu phucbasic datatypes (2) •there are 2datatypes in java•primitive data...

71
Java Review Phuc H. Duong, M.Sc. [email protected] CS502052 / Chapter 1

Upload: others

Post on 11-Jun-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Java Review

Phuc H. Duong, [email protected]

CS502052 / Chapter 1

Page 2: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Outline1. Basic datatypes

2. Loop control

3. Decision making

4. Strings, Arrays

5. Java Object Oriented

2019 502052 - Chapter 1 1

Page 3: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 4: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 5: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 6: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 7: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Basic Datatypes (4)• Primitive data types

2019 502052 - Chapter 1 6

Page 8: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 9: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Basic Datatypes (6)• Reference Datatypes

2019 502052 - Chapter 1 8

Page 10: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Outline1. Basic datatypes

2. Loop control

3. Decision making

4. Strings, Arrays

5. Java Object Oriented

2019 502052 - Chapter 1 9

Page 11: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 12: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

given condition is still true

2019 502052 - Chapter 1 11

Page 13: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 14: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 15: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 16: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Loop Control (6)• Enhanced for loop

• To traverse the collection of elements including arrays

2019 502052 - Chapter 1 15

Page 17: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Outline1. Basic datatypes

2. Loop control

3. Decision making

4. Strings, Arrays

5. Java Object Oriented

2019 502052 - Chapter 1 16

Page 18: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 19: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Decision Making (2)• There are 4 types:

• if statement

• if…else statement

• nested if statement

• switch statement

2019 502052 - Chapter 1 18

Page 20: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 21: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Outline1. Basic datatypes

2. Loop control

3. Decision making

4. Strings, Arrays

5. Java Object Oriented

2019 502052 - Chapter 1 20

Page 22: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 23: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 24: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 25: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

2019 502052 - Chapter 1 24

Page 26: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Array (3)• Example

2019 502052 - Chapter 1 25

Page 27: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Outline1. Basic datatypes

2. Loop control

3. Decision making

4. Strings, Arrays

5. Java Object Oriented

2019 502052 - Chapter 1 26

Page 28: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 29: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 30: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 31: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

2019 502052 - Chapter 1 30

Page 32: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 33: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Inheritance - Example

2019 502052 - Chapter 1 32

Super.java à Sub.java à Test.java

Page 34: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 35: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 36: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 37: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 38: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 39: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Types of Inheritance (1)

2019 502052 - Chapter 1 38

Page 40: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 41: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

overloading?

2019 502052 - Chapter 1 40

Page 42: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 43: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 44: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 45: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Polymorphism (2)

2019 502052 - Chapter 1 44

Page 46: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Polymorphism (3)• Example (cont.):

2019 502052 - Chapter 1 45

Output???

Page 47: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Polymorphism (4)

2019 502052 - Chapter 1 46

Page 48: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 49: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 50: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Abstract Class (2)

2019 502052 - Chapter 1 49

Page 51: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Abstract Class (3)

2019 502052 - Chapter 1 50

Page 52: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Abstract Class (4)• Error version:

• Right version:

2019 502052 - Chapter 1 51

Page 53: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 54: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Best Practice

2019 502052 - Chapter 1 53

Page 55: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Constructor• A Java class should have 3 constructors:

• Default constructor

• Parameterized constructor

• Copy constructor

2019 502052 - Chapter 1 54

Page 56: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Example

2019 502052 - Chapter 1 55

Output?

Page 57: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Example• Output:

2019 502052 - Chapter 1 56

Do you see the problem here?

Page 58: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 59: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Example (Fixed)

2019 502052 - Chapter 1 58

Page 60: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Example (Fixed)• Output:

2019 502052 - Chapter 1 59

Page 61: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Exercises

2019 502052 - Chapter 1 60

Page 62: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 63: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

of table.

2019 502052 - Chapter 1 62

Page 64: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 65: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

below.

2019 502052 - Chapter 1 64

Page 66: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

below.

2019 502052 - Chapter 1 65

Page 67: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Question 6

2019 502052 - Chapter 1 66

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

Page 68: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Question 7

2019 502052 - Chapter 1 67

• Explain why this code prints 0 rather than 7.

Page 69: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

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

Page 70: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

Question 9

2019 502052 - Chapter 1 69

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

Page 71: Java Review - Duong Huu PhucBasic Datatypes (2) •There are 2datatypes in Java•Primitive Data Types •Built-in datatypes •Reference/Object Data Types •User defined 2019 502052

END OF CHAPTER

502052 - Chapter 1 702019