Question

In: Computer Science

Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...

Code in Java

Given the LinkedList class that is shown below

Add the following methods:

  • add(String new_word): Adds a linkedlist item at the end of the linkedlist
  • print(): Prints all the words inside of the linkedlist
  • length(): Returns an int with the length of items in the linkedlist
  • remove(int index): removes item at specified index
  • itemAt(int index): returns LinkedList item at the index in the linkedlist
public class MyLinkedList {
private String name;
private MyLinkedList next;
public MyLinkedList(String n) {
this.name = n;
}
public void add(String n) {
MyLinkedList i = this;
while (i.next != null) {
i = i.next;
}
i.next = new MyLinkedList(n);
}
public int length() {
int ret_val = 1;
MyLinkedList i = this;
while (i.next != null) {
i = i.next;
ret_val++;
}
return ret_val;
}
public void print() {
// ????
}
public MyLinkedList itemAt(int index) {
int counter = 1;
MyLinkedList i = this;
while (i.next != null && counter < index) {
i = i.next;
counter++;
}
return i;
}
public static void main(String[] args) {
MyLinkedList student_list = new MyLinkedList("12");
student_list.add("7");
student_list.add("20");
student_list.add("40");
student_list.add("9");
System.out.println(student_list.length());
System.out.println(student_list.itemAt(3));
System.out.println();
}
}

Solutions

Expert Solution

Please find the answer for the above given question..

ANSWER :

OUTPUT :

CODE :   I have highlighted the code below which i have added.

class MyLinkedList {

private MyLinkedList next;

private String name;

public MyLinkedList(String n) {

this.name = n;

}

public void add(String n) {

MyLinkedList i = this;

while (i.next != null) {

i = i.next;

}

i.next = new MyLinkedList(n);

}

public int length() {

int ret_val = 1;

MyLinkedList i = this;

while (i.next != null) {

i = i.next;

ret_val++;

}

return ret_val;

}

public MyLinkedList itemAt(int index) {

int counter = 1;

MyLinkedList i = this;

while (i.next != null && counter < index) {

i = i.next;

counter++;

}

return i;

}

public void print() {

int counter =0;

MyLinkedList i = this;

System.out.println("Index\tElement");

while(i.next != null){

System.out.println(Integer.toString(counter)+"\t\t"+i.name);

i= i.next;

counter++;

}

System.out.println(Integer.toString(counter)+"\t\t"+i.name);

}

public static void main(String[] args) {

MyLinkedList student_list = new MyLinkedList("12");

student_list.add("7");

student_list.add("20");

student_list.add("40");

student_list.add("9");

System.out.println(student_list.length());

System.out.println(student_list.itemAt(3).name);

System.out.println();

student_list.print();

}

}

Thanks..


Related Solutions

java code Add the following methods to the LinkedQueue class, and create a test driver for...
java code Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging...
Add code (see below for details) to the methods "set" and "get" in the following class,...
Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index] =...
IN JAVA: Repeat Exercise 28, but add the methods to the LinkedStack class. Add the following...
IN JAVA: Repeat Exercise 28, but add the methods to the LinkedStack class. Add the following methods to the LinkedStacked class, and create a test driver for each to show that they work correctly. In order to practice your array related coding skills, code each of these methods by accessing the internal variables of the LinkedStacked, not by calling the previously defined public methods of the class. - String toString()—creates and returns a string that correctly represents the current stack....
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence;...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence; Message() { sentence=""; } Message(String text) { setSentence(text); } void setSentence(String text) { sentence=text; } String getSentence() { return sentence; } int getVowels() { int count=0; for(int i=0;i<sentence.length();i++) { char ch=sentence.charAt(i); if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') { count=count+1; } } return count; } int getConsonants() { int count=0; for(int i=0;i<sentence.length();i++)...
Remove the Head element from the code below: public class LinkedList {    class Node{ int...
Remove the Head element from the code below: public class LinkedList {    class Node{ int value; Node nextElement; public Node(int value) { this.value = value; this.nextElement = null; } } public Node first = null; public Node last = null; public void addNewNode(int element) { Node newValueNode = new Node(element);    if(first == null) { first = newValueNode; } else { last.nextElement = newValueNode; } last = newValueNode; } public void displayValues() { Node recent = first; if(first ==...
Write Java code for extending the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would include the...
Write Java code for extending the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would include the following method: public ExtLinkedList <E> mergeThreeLists (ExtLinkedList<E> list1, ExtLinkedList<E> list2) { } that returns an ExtLinkedList<E> which is the merged version of values from thislist, followed by values from list1, and then followed by values from list2.    For example, if E is Integer type and this listhas (5,3,1), list1has (8, 10,12,14), and list2has (22,23,24,25,26) in that order, then the returned list from a call to...
A. Add code (see below for details) to the methods "set" and "get" in the following...
A. Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT