subject

Requirements:

1) You must follow the pattern (and starter code) given by Dr. Rimland in class

2) You are not allowed to use Iterators

3) Your list cannot be doubly linked.

Step 1: Create a new Java project named LinkedListProject



Step 2: Create a new Java class in your project named LinkedStringList



Step 3:

Add an inner Node class as demonstrated in class.



Step 4:

You will be given code for the following methods in class. You should include these in your project:

LinkedStringList() (an empty constructor)

addFirst() (adds an element in the first position)

setFirstValue() (sets the value of the element in the first position)

setCurrentValue() (sets the value of the element in the current position)

moveNext() (moves the current reference to the next element)

moveFirst() (moves the current reference to the first position)

isEmpty() (returns whether the list is empty)

getLength() (returns the length of the list)

displayList() (displays all elements in the list)

Here is the code for these ^

import java. util. NoSuchElementException;

public class LinkedStringList {

private Node first;

private Node currentNode;

private int length;

class Node

{

private String data;

private Node next;

public void printNodeData()

{

System. out. println("Node data: " + data);

}

public Node getNext()

{

return next;

}

}

public LinkedStringList()

{

first = null;

currentNode = null;

length = 0;

}

public void addFirst(String value)

{

//create the Node and set its value

Node newNode = new Node();

newNode. data = value;

// if newNode is the first node, this will be null

// otherwise it will point to the former "first" now

newNode. next = first;

//set our "first" Node to the Node we just created

first = newNode;

currentNode = newNode;

length++;

}

public void setFirstValue(String value)

{

first. data = value;

}

public void setCurrentValue(String value)

{

currentNode. data = value;

}

public void moveNext()

{

if (currentNode. next == null)

{

throw new NoSuchElementException();

}

else

{

currentNode = currentNode. next;

}

}

public void moveFirst()

{

currentNode = first;

}

public boolean isEmpty()

{

return (first == null);

}

public int getLength()

{

return length;

}

public String getFirstValue()

{

if (first == null)

{

throw new NoSuchElementException();

}

else

{

return first. data;

}

}

public String getCurrentValue()

{

if (currentNode == null)

{

throw new NoSuchElementException();

}

else

{

return currentNode. data;

}

}

public void displayList()

{

Node currentNode = first;

System. out. println("List contents:");

while (currentNode != null)

{

currentNode. printNodeData();

currentNode = currentNode. getNext();

}

}

}



Step 5:

Add the following methods on your own:

add(String value) - add an element in the current position

getCurrentValue() - returns the value of the current element

removeFirst() - remove the first element

remove() - remove the current element

indexOf(String value) - return the position of value, or -1 if not found

sortAscending() - sort your linked list in ascending order using a Selection Sort as shown in class.



Step 6:

In your main method:

Create an instance of your LinkedStringList class named list.

Call your add() method 3 times to add the values "First", "Second", and "Third" to list.

Use moveFirst(), moveNext(), and setCurrentValue() to replace those values with "Red", "Green", and "Blue" (respectively).

Use your indexOf() method to display the position of the "Green" string.

Use displayList() to display the contents of list.

Use your sortAscending() method to sort the list in ascending order.

Use displayList() to display the contents of mySortedList.

Call your remove() method twice on mySortedList.

Use displayList() to display the contents of mySortedList (again).

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 23.06.2019 17:30
When making changes to optimize part of a processor, it is often the case that speeding up one type of instruction comes at the cost of slowing down something else. for example, if we put in a complicated fast floating-point unit, that takes space, and something might have to be moved farther away from the middle to accommodate it, adding an extra cycle in delay to reach that unit. the basic amdahl's law equation does not take into account this trade-off. a. if the new fast floating-point unit speeds up floating-point operations by, on average, 2ă—, and floating-point operations take 20% of the original program's execution time, what is the overall speedup (ignoring the penalty to any other instructions)? b. now assume that speeding up the floating-point unit slowed down data cache accesses, resulting in a 1.5ă— slowdown (or 2/3 speedup). data cache accesses consume 10% of the execution time. what is the overall speedup now? c. after implementing the new floating-point operations, what percentage of execution time is spent on floating-point operations? what percentage is spent on data cache accesses?
Answers: 2
question
Computers and Technology, 24.06.2019 09:30
What is the definition of digital literacy?
Answers: 1
question
Computers and Technology, 24.06.2019 13:30
Which of the following is not a “fatal four” event?
Answers: 2
question
Computers and Technology, 24.06.2019 15:30
If you want to delete an entire word at a time, which key should you press along with the backspace or delete key?
Answers: 1
You know the right answer?
Requirements:

1) You must follow the pattern (and starter code) given by Dr. Rimland in...
Questions
question
Mathematics, 28.06.2019 04:00
question
Health, 28.06.2019 04:00
Questions on the website: 13722363