Really my Turkish instructor has problems with communicating the material. He li
ID: 3663451 • Letter: R
Question
Really my Turkish instructor has problems with communicating the material. He likes drawing children's pictures I need some help in uderstanding this linked ds method. Here are the instructions. In the code below only the section for the method can be made and altered.(IN BOLD) I am providing the List and Tester class codes.
Here is the List.java to create the method..
public class List {
private class Node {
private int data;
private Node next;
public Node(int d, Node nodePar){
data = d;
next = nodePar;
}
}
private Node head;
public List(){
head = null;
}
// Do not modify any code above this line.
// Write createListForProject3 method here.
public static void displayMyName(){
System.out.println("Programmed by name. ");
}
public void addToHead(int dataPar){
head = new Node(dataPar, head);
}
public void displayList(){
Node tmp = head;
while(tmp != null){
System.out.print(tmp.data+ " ");
tmp = tmp.next;
}
System.out.println();
}
}
Here is the Tester.Java to test the program.
public class Tester {
public static void main(String[] args) {
List.displayMyName();
List list1 = new List();
list1.addToHead(8);
list1.addToHead(20);
list1.addToHead(6);
list1.addToHead(4);
list1.addToHead(50);
list1.addToHead(2);
list1.displayList();
List list2 = list1.createListForProject3();
list1.displayList();
list2.displayList();
System.out.println("Good Bye..");
}
}
Here is what the final code should look like:
Explanation / Answer
//requested function
// Write createListForProject3 method here.
public List createListForProject3(){
Node tmp = head;
List Result = new List();
while(tmp != null){
if (tmp.data % 10 != 0)
Result.addToHead(tmp.data);
tmp = tmp.next;
}
return Result;
}
// full code
public class List {
private class Node {
private int data;
private Node next;
public Node(int d, Node nodePar){
data = d;
next = nodePar;
}
}
private Node head;
public List(){
head = null;
}
// Do not modify any code above this line.
// Write createListForProject3 method here.
public List createListForProject3(){
Node tmp = head;
List Result = new List();
while(tmp != null){
if (tmp.data % 10 != 0)
Result.addToHead(tmp.data);
tmp = tmp.next;
}
return Result;
}
public static void displayMyName(){
System.out.println("Programmed by name. ");
}
public void addToHead(int dataPar){
head = new Node(dataPar, head);
}
public void displayList(){
Node tmp = head;
while(tmp != null){
System.out.print(tmp.data+ " ");
tmp = tmp.next;
}
System.out.println();
}
public static void main(String[] args) {
List.displayMyName();
List list1 = new List();
list1.addToHead(8);
list1.addToHead(20);
list1.addToHead(6);
list1.addToHead(4);
list1.addToHead(50);
list1.addToHead(2);
list1.displayList();
List list2 = list1.createListForProject3();
list1.displayList();
list2.displayList();
System.out.println("Good Bye..");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.