Generic Class & Tree
Generic Class & Tree
KEJURUTERAAN KELAUTAN
DAN INFORMATIK
2019/2020
Lab 6: Generic
Class & Tree
VERSION 1
STUDENT INFORMATION
MATRIC NUMBER:S58798
GROUP:K3
LAB:CISCO
DATE:7/12/2021
i
TABLE OF CONTENTS
INSTRUCTIONS .......................................................................................................................... 1
TASK 1: Understanding The Concept Of Generic Class .............................................................. 2
TASK 2: Implementing A General Tree ...................................................................................... 9
TASK 3: Develop A Tree With Double Data Type ...................................................................... 18
ii
INSTRUCTIONS
Manual makmal ini adalah untuk kegunaan pelajar-pelajar Fakulti Teknologi Kejuruteraan
Kelautan dan Informatik, Universiti Malaysia Terengganu (UMT) sahaja. Tidak dibenarkan
mencetak dan mengedar manual ini tanpa kebenaran rasmi daripada penulis.
Sila ikuti langkah demi langkah sebagaimana yang dinyatakan di dalam manual.
This laboratory manual is for use by the students of the Faculty of Ocean Engineering Technology
and Informatics, Universiti Malaysia Terengganu (UMT) only. It is not permissible to print and
distribute this manual without the official authorisation of the author.
1
TASK 1: UNDERSTANDING THE CONCEPT OF GENERIC CLASS
OBJECTIVE
ESTIMATED TIME
[60 Minutes]
Like C++, we use <> to specify parameter types in generic class creation. To create objects of
generic class, we use following syntax.
Note: In Parameter type we can not use primitives like 'int','char' or 'double'.
A generic class declaration looks like a non-generic class declaration, except that the class name
is followed by a type parameter section.
As with generic methods, the type parameter section of a generic class can have one or more
type parameters separated by commas. These classes are known as parameterised classes or
parameterised types because they accept one or more parameters.
2
The most commonly used type parameter names are:
Important Notes:
Type Parameter and Type Argument Terminology: Many developers use the terms "type
parameter" and "type argument" interchangeably, but these terms are not the same. When
coding, one provides “type arguments” in order to create a “parameterized type”. Therefore,
the T in Foo<T> is a type parameter, and the String in Foo<String> f is a type argument.
This lesson observes this definition when using these terms.
STEPS:
1. Open NetBeans and create a new java application project.
2. Name your project as GenericsExperiment and click finish.
3. Change author profiles to :
a. Name :
b. Program: <put your program. E.g., SMSK(SE) or SMSK with
IM
c. Course : CSF3104
d. Lab : <enter lab number>
e. Date : <enter lab date>
4. In the same GenericsExperiment project’s package, create a new file named
NonGenericClass.java.
5. Add the following codes to the file:
3
6. Now, create a test class to test the above class. Name your test class as
GenericClassDemo.java. Your output should look like below:
7. Next, we are going to modify the above class to become a generic class.
8. Create a new file and name it as MyGenericClass.java.
9. Add <T, U> after the name of the class (see below). For this example, we put T and U in the
parameter section to represent the generic type.
10. Copy the codes from NonGenericClass.java and paste it to the body of
MyGenericClass class. Modify it to make it looks like follows:
4
11. It’s time to test your class. Use the previous test class to test MyGenericClass. Unlike
previous steps, where we have supplied the class with String parameters, this time we
are going to supply a String and an Integer as parameters to the class. So, part of the
codes in the main method should look like below:
12. Save and compile your codes, and your output should look like below:
Starting from Java SE 7 and later, you can replace the type arguments required to invoke
the constructor of a generic class with an empty set of type arguments (<>) as long as the
compiler can determine, or infer, the type arguments from the context. This pair of angle
brackets, <>, is informally called the diamond. For example, you can create an instance of
MyGenericClass <String, Integer> with the following statement:
MyGenericClass <String, Integer> myGC =
new MyGenericClass <>("Final Exam", 2019);
5
14. Fix the codes by following the above example and observe the output. Do the warnings
disappear?
Answer:
Yes the warning disappear.
15. Copy and paste your Java codes into the text box below:
Answer:
package GenericExperiment;
/**
* @author
* Name: TEOH YI YIN
* Program: SMSK(SE)
* Course: CSF3104
* Lab: 6
* Date: 7/12/2021
*/
String obj1;
String obj2;
package GenericExperiment;
/**
6
* @author
* Name: TEOH YI YIN
* Program: SMSK(SE)
* Course: CSF3104
* Lab: 6
* Date: 7/12/2021
*/
package GenericExperiment;
/**
* @author
* Name: TEOH YI YIN
* Program: SMSK(SE)
* Course: CSF3104
* Lab: 6
* Date: 7/12/2021
*/
7
GC.print();
*/
QUESTIONS
1. Discuss the differences between Non-generic and generic classes.
Answer:
Non-generic classes ionly can refer to a specific data type while generic class can refer
to any data type.
8
TASK 2: IMPLEMENTING A GENERAL TREE
OBJECTIVE
ESTIMATED TIME
[45 Minutes]
INTRODUCTION TO TREE
There are many basic data structures that can be used to solve application problems. Array is a
good static data structure that can be accessed randomly and is fairly easy to implement. Linked
Lists on the other hand is dynamic and is ideal for application that requires frequent operations
such as add, delete, and update. One drawback of linked list is that data access is sequential.
Then there are other specialized data structures like, stacks and queues that allows us to solve
complicated problems (e.g.: Maze traversal) using these restricted data structures. One other
data structure is the hash table that allows users to program applications that require frequent
search and updates. They can be done in O(1) in a hash table.
One of the disadvantages of using an array or linked list to store data is the time necessary to
search for an item. Since both the arrays and Linked Lists are linear structures the time required
to search a “linear” list is proportional to the size of the data set. For example, if the size of the
data set is n, then the number of comparisons needed to find (or not find) an item may be as bad
as some multiple of n. So, imagine doing the search on a linked list (or array) with n = 106 nodes.
Even on a machine that can do million comparisons per second, searching for m items will take
roughly m seconds. This not acceptable in today’s world where speed at which we complete
operations is extremely important. Time is money. Therefore it seems that better (more
efficient) data structures are needed to store and search data.
In this chapter, we can extend the concept of linked data structure (linked list, stack, queue) to
a structure that may have multiple relations among its nodes. Such a structure is called a tree. A
tree is a collection of nodes connected by directed (or undirected) edges. A tree is
a nonlinear data structure, compared to arrays, linked lists, stacks and queues which are linear
data structures. A tree can be empty with no nodes or a tree is a structure consisting of one node
called the root and zero or one or more subtrees. A tree has following general properties:
• A is a parent of B, C, D,
B is called a child of A.
on the other hand, B is a parent of E, F, K
Each node can have arbitrary number of children. Nodes with no children are called leaves,
or external nodes. In the above picture, C, E, F, L, G are leaves. Nodes, which are not leaves, are
called internal nodes. Internal nodes have at least one child.
Nodes with the same parent are called siblings. In the picture, B, C, D are called
siblings. The depth of a node is the number of edges from the root to the node. The depth of K
is 2. The height of a node is the number of edges from the node to the deepest leaf. The height
of B is 2. The height of a tree is a height of a root.
STEPS:
1. Given below is the example of a general Tree structure. In this task, we will do the
implementation of a tree data structure using generic class.
10
2. First, open NetBeans and create new java application project.
3. Name your project as TreeExperiment and click finish.
4. Change author profiles to :
a. Name :
b. Program: <put your program. E.g., SMSK(SE) or SMSK with
IM
c. Course : CSF3104
d. Lab : <enter lab number>
e. Date : <enter lab date>
5. In the same TreeExperiment project’s package, create a new file named Node.java
and insert the following codes:
11
12
6. Next, create a test class for Node.java. This test class will create a tree as described in
Step 1.
7. Save, compile and run your source code. Observe the output. A correct output should look
like below:
root
node 1
node 11
node 111
node 112
node 12
node 2
node 21
node 22
node 23
8. Print screen your output from NetBeans and upload it using the following control box:
13
9. Copy and paste your Java codes from NetBeans into text box below:
package TreeExperiment;
/**
* @author
* Name: TEOH YI YIN
* Program: SMSK(SE)
* Course: CSF3104
* Lab: 6
* Date: 7/12/2021
*/
import java.util.ArrayList;
import java.util.List;
14
this.children.add(child);
return child;
}
public T getData(){
return data;
}
package TreeExperiment;
/**
* @author
* Name: TEOH YI YIN
* Program: SMSK(SE)
* Course: CSF3104
* Lab: 6
* Date: 7/12/2021
*/
15
public static void main(String[] args) {
Node<String> root = createTree();
printTree(root, " ");
}
return root;
}
private static <T> void printTree(Node<T> root, String appender){
System.out.println(appender + root.getData());
root.getChildren().forEach(each -> printTree(each, appender + appender));
}
}
QUESTIONS
1. List the basic operations in tree data structure.
Answer:
Insert, search, preorder traversal, postorder traversal and inorder traversal
16
2. Where can we apply tree data structure in a software development?
Answer:
Organization charts, file systems, programming environments
3. Write algorithm for finding the root of tree from any node.
Answer:
public Node getRoot(){
If(parent != null){
return parent.getRoot();
}else{
return this;
}
}
17
TASK 3: DEVELOP A TREE WITH DOUBLE DATA TYPE
OBJECTIVE
During this activity, students will apply a general tree concept in a simple programming task. A
student should understand how tree works.
ESTIMATED TIME
[30 Minutes]
INTRODUCTION
For this task, every node in a tree holds data with a double data type.
STEPS:
1. Open previously created Netbeans project.
2. Create a new class named DoubleTypeTreeDemo.
3. In DoubleTypeTreeDemo.java, create a test class for the following tree structure:
18
5. Upload your output using the control box below:
6. Copy and paste your Java codes from NetBeans into text box below:
package Task3;
/**
* @author
* Name: TEOH YI YIN
* Program: SMSK(SE)
* Course: CSF3104
* Lab: 6
* Date: 7/12/2021
*/
import java.util.ArrayList;
import java.util.List;
19
public Node<T> addChild(Node<T> child){
child.setParent(this);
this.children.add(child);
return child;
}
public T getData(){
return data;
}
package Task3;
/**
* @author
* Name: TEOH YI YIN
* Program: SMSK(SE)
* Course: CSF3104
* Lab: 6
* Date: 7/12/2021
*/
20
public class DoubleTypeTreeDemo {
public static void main(String[] args) {
Node<Double> root = createTree();
printTree(root, " ");
}
return root;
}
21
QUESTIONS
1. Explain about 3 types tree traversals.
Answer:
Preorder Traversal – a node is visited before tis descendants
Postorder Traversal – a node is visited after its descendants
Inorder Traversal – a node is visited after its left subtree and before its right subtree
Answer:
Answer:
Binary tree only has maximum 2 chidren for each internal node while general tree can have
more than 2 children for each internal node.
Finally, read the instruction regarding submission carefully. Submit your answer using the link
provided in Oceania UMT. Please ensure your codes are submitted to the correct group.
22