Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need some help with this assignment. I have finished part of it and getting a fail message below: Will try to duplicate non-existent X in

Need some help with this assignment. I have finished part of it and getting a fail message below:

Will try to duplicate non-existent X in ABCDE, should see an exception ... ... FAIL: ... no exception was thrown.

-----------------------------------------------------------------------------------------------------------------------------------------------------------

Directions for the assignment.

In this assignemnt you will implement find() and duplicate() methods for a linked list class. To keep things simple, the list class and its node class arent generic. The data of each node is one char, stored in a CharNode class that is provided. In the Eclipse workspace of your choice, create a new Java project containing package linked and add to it the 3 provided source files: CharNode.java, CharLinkedList.java, and LinkedListTester.java. Add the following methods to CharLinkedList.java: public CharNode find(char ch) Returns the first (i.e. closest to the head) node in the list whose data is equal to ch. If there is no such node, returns null. public void duplicate(char ch) Finds (using the find() method above) the first node in the list whose data is equal to ch. If there is no such node, throws an IllegalArgumentException with a useful message. If the node is found, creates a new node containing the same data, and inserts that node into the list either immediately before or immediately after the found node (your choice). Pay attention to corner cases: the node you find might be anywhere in the list head, middle, or tail. The CharNode starter file contains a hasIntegrity() method that checks some (but not all) aspects of list integrity. Use it (maybe in assert statements) in your find() and duplicate() methods if it helps you. This is a win-or-lose assignment. If your code passes the graderbot (class LinkedListTester), you get 100 points. If your code fails any part of the graderbot, you get zero points.

-----------------------------------------------------------------------------------------------------------------------------------------------------------

Char.LinkedList.java

-----------------------------------------------------------------------------------------------------------------------------------------------------------

package linked;

import java.util.*;

public class CharLinkedList

{

private CharNode head; // Empty if head and

private CharNode tail; // tail are null

public CharLinkedList() { }

public CharLinkedList(String s)

{

for (int i=s.length()-1; i>=0; i--)

insertAtHead(s.charAt(i));

}

public void insertAtHead(char ch)

{

assert hasIntegrity(); // Precondition

CharNode node = new CharNode(ch);

node.setNext(head);

head = node;

if (tail == null)

tail = node; // Corner case: inserting into empty node

assert hasIntegrity(); // Postcondition

}

public String toString()

{

String s = "";

CharNode node = head;

while (node != null)

{

s += node.getData();

node = node.getNext();

}

return s;

}

//

// Returns true if this list has emptiness integrity, has tail integrity, has no loops,

// and tail is reachable from head.

//

// Caution: this checks for most but not all common integrity problems.

//

boolean hasIntegrity()

{

// Check emptiness. If either head or tail is null, the other must

// also be null. Different logic from what you saw in lecture. Returns

// immediately if this list is empty.

if (head == null || tail == null)

return head == null && tail == null;

// Check tail integrity (tail.next must be null).

if (tail.getNext() != null)

return false;

// Check for loops.

Set visitedNodes = new HashSet<>();

CharNode node = head;

while (node != null)

{

if (visitedNodes.contains(node))

return false; // Current node has been visited before, we must have a loop

visitedNodes.add(node); // First visit to this node

node = node.getNext();

}

// Make sure tail is reachable from head.

node = head;

while (node != null && node != tail)

node = node.getNext();

return node == tail;

}

// Returns the first node in the last whose data is equal to ch.

// If there is no such node, returns null.

// Code that was added due to instructions.

public CharNode find(char ch)

{

CharNode node1 = head;

CharNode node2 = null;

while(node1 !=null) {

if(node1.getData() == ch) {

node2 = node1;

break;

}

node1 = node1.getNext();

}

return node2;

}

// Finds the first node in the list whose data is equal to ch.

// If there is no such node, throws an exceptions with a message.

// If found, a duplicate node is created.

// Cde that was added due to instructions.

public void duplicate(char ch)

{

CharNode newNode = find(ch);

if(newNode == null)

return;

else {

CharNode dup = new CharNode(ch);

dup.setNext(newNode.getNext());

newNode.setNext(dup);

if(tail == newNode) {

tail = dup;

}

}

}

}

-----------------------------------------------------------------------------------------------------------------------------------------------------------

CharNode.java

-----------------------------------------------------------------------------------------------------------------------------------------------------------

package linked;

class CharNode

{

private char ch;

private CharNode next;

CharNode(char ch)

{

this.ch = ch;

}

CharNode getNext()

{

return next;

}

void setNext(CharNode next)

{

this.next = next;

}

char getData()

{

return ch;

}

}

----------------------------------------------------------------------------------------------------------------------------------------------------------

LinkedListTester.java

-----------------------------------------------------------------------------------------------------------------------------------------------------------

package linked;

public class LinkedListTester

{

private static void pass()

{

System.out.println("... PASSED. ");

}

private static void fail(String message)

{

if (!message.endsWith("."))

message += ".";

System.out.println("... FAIL: " + message);

System.exit(0);

}

public static void main(String[] args)

{

System.out.println("Searching for A in empty list ...");

CharLinkedList list = new CharLinkedList();

if (list.find('A') != null)

fail("Found a node in an empty list.");

pass();

String original = "ABCDE";

for (int i=0; i

{

// Find target.

list = new CharLinkedList(original);

char target = original.charAt(i);

System.out.println("Searching for " + target + " in " + original + " ...");

CharNode node = list.find(target);

if (node == null)

fail("Couldn't find it.");

if (node.getData() != target)

fail("Found node containing wrong char: " + node.getData());

if (!list.hasIntegrity())

fail("Found " + target + " but list has broken integrity");

if (!list.toString().equals(original))

fail("Found " + target + " but list has changed to " + list);

pass();

// Duplicate target.

list = new CharLinkedList(original);

System.out.println("Wil duplicate " + target + " in " + original + " ...");

list.duplicate(target);

if (!list.hasIntegrity())

fail("After duplicating, list doesn't have integrity");

String expected = original.substring(0, i+1) + original.substring(i);

if (!list.toString().equals(expected))

fail("After duplicating, list is " + list + ", expected " + expected);

pass();

}

// Duplicate non-existent char.

list = new CharLinkedList(original);

System.out.println("Will try to duplicate non-existent X in " + original + ", should see an exception ...");

try

{

list.duplicate('X');

fail("... no exception was thrown.");

}

catch (IllegalArgumentException x)

{

pass();

}

if (!list.hasIntegrity())

fail("After duplicating, list doesn't have integrity");

if (!list.toString().equals(original))

fail("List should not have changed, became " + list);

System.out.println(" YOU PASSED ... full marks.");

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

A 300N F 30% d 2 m Answered: 1 week ago

Answered: 1 week ago