Computers and Technology

The Module 13 Assignment is to convert a non-generic Java class into a generic Java class and demonstrate functionality by instantiating with several different types The source code for the non-generic class came from GeeksForGeeks A Computer Science Portal for Geeks GeeksForGeeks
https://www. geeksforgeeks. org/doubly-linked-list/
The source code for the Doubly Linked List example in Java was copied, modified to move the attributes to the bottoms of classes, make the attributes private to support information hiding, and comments changed from C to Java style and then saved in this Java source file
The DLL class is written as a Doubly Linked List of ints. Modify the class to be generic and demonstrate with the following elements of the three types
Integer: 1, 5, 10
Double: 2.0, 6.0, 12.0
String: "Dog", "Cat", "Horse"
Submit Requirements, Design, Source Code, and Program Execution Output
package LinkedList;
//A complete working Java program to demonstrate all
// Doubly Linked list Node
class Node {
// Constructor to create a new node
// next and prev is by default initialized as null
Node(int data) {
this. data = data;
}
public int getData() {
return this. data;
}
public void setData(int data) {
this. data = data;
}
public Node getPrev() {
return this. prev;
}
public void setPrev(Node prev) {
this. prev = prev;
}
public Node getNext() {
return this. next;
}
public void setNext(Node next) {
this. next = next;
}
private int data;
private Node prev;
private Node next;
}
//Class for Doubly Linked List
public class DLL {
// Adding a node at the front of the list
public void push(int new_data) {
// 1. allocate node
// 2. put in the data
Node new_Node = new Node(new_data);
// 3. Make next of new node as head and previous as NULL
new_Node. setNext(head);
new_Node. setPrev(null);
// 4. change prev of head node to new node
if (head != null) {
head. setPrev(new_Node);
}
// 5. move the head to point to the new node
head = new_Node;
}
// Given a node as prev_node, insert a new node after the given node
public void InsertAfter(Node prev_Node, int new_data) {
// 1. check if the given prev_node is NULL
if (prev_Node == null) {
System. out. println("The given previous node cannot be NULL ");
return;
}
// 2. allocate node
// 3. put in the data
Node new_node = new Node(new_data);
// 4. Make next of new node as next of prev_node
new_node. setNext(prev_Node. getNext());
// 5. Make the next of prev_node as new_node
prev_Node. setNext(new_node);
// 6. Make prev_node as previous of new_node
new_node. setPrev(prev_Node);
// 7. Change previous of new_node's next node
if (new_node. getNext() != null) {
new_node. getNext().setPrev(new_node);
}
}
// Add a node at the end of the list
void append(int new_data) {
// 1. allocate node
// 2. put in the data
Node new_node = new Node(new_data);
Node last = head; // used in step 5
// 3. This new node is going to be the last node, so make next of it as NULL
new_node. setNext(null);
// 4. If the Linked List is empty, then make the new node as head
if (head == null) {
new_node. setPrev(null);
head = new_node;
return;
}
// 5. Else traverse till the last node
while (last. getNext() != null) {
last = last. getNext();
}
// 6. Change the next of last node
last. setNext(new_node);
// 7. Make last node as previous of new node
new_node. setPrev(last);
}
// This function prints contents of linked list starting from the given node
public void printlist(Node node) {
Node last = null;
System. out. println("Traversal in forward Direction");
while (node != null) {
System. out. print(node. getData() + " ");
last = node;
node = node. getNext();
}
System. out. println();
System. out. println("Traversal in reverse direction");
while (last != null) {
System. out. print(last. getData() + " ");
last = last. getPrev();
}
}

/* Drier program to test above functions */
public static void main(String[] args) {
/* Start with the empty list */
DLL dll = new DLL();
// Insert 6. So linked list becomes 6->NULL
dll. append(6);
// Insert 7 at the beginning. So linked list becomes 7->6->NULL
dll. push(7);
// Insert 1 at the beginning. So linked list becomes 1->7->6->NULL
dll. push(1);
// Insert 4 at the end. So linked list becomes 1->7->6->4->NULL
dll. append(4);
// Insert 8, after 7. So linked list becomes 1->7->8->6->4->NULL< br /> dll. InsertAfter(dll. head. getNext(), 8);
System. out. println("Created DLL is: ");
dll. printlist(dll. head);
}
private Node head; // head of list
}

answer
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 21:30, elsauceomotho
Im doing this last minute and literally none of my neighbors or people that my dad works with use excel so if anyone could me make up an example
Answers: 1
image
Computers and Technology, 22.06.2019 22:00, noeminm105
Consider the following declarations (1, 2, 3, 5, 7)class bagtype{public: void set(string, double, double, double, double); void print() const; string getstyle() const; double getprice() const; void get(double, double, double, double); bagtype(); bagtype(string, double, double, double, double); private: string style: double l; double w; double h; double price; }; a.) write the definition of the number function set so that private members are set according to the parametersb.) write the definition of the member function print that prints the values of the data membersc.) write the definition of the default constructor of the class bagtype so that the private member variables are initialized to "", 0.0, 0.0, 0.0, 0.0, respectively d.) write a c++ statement that prints the value of the object newbag. e.) write a c++ statement that declares the object tempbag of type bagtype, and initialize the member variables of tempbag to "backpack", 15, 8, 20 and 49.99, respectively
Answers: 3
image
Computers and Technology, 24.06.2019 02:30, Esmail
Write the pseudo code for this problem based on what you learned from the video. the purpose is to design a modular program that asks the user to enter a distance in kilometers, and then converts that distance to miles. the conversion formula is as follows: miles = kilometers x 0.6214
Answers: 3
image
Computers and Technology, 24.06.2019 20:20, jdkrisdaimcc11
Write python code that prompts the user to enter his or her age and assigns the user’s input to an integer variable named age.
Answers: 1
Do you know the correct answer?
The Module 13 Assignment is to convert a non-generic Java class into a generic Java class and demons...

Questions in other subjects: