Edit: I forgot to add Tile.Java, and as requested i changed code in all text for
ID: 3593866 • Letter: E
Question
Edit: I forgot to add Tile.Java, and as requested i changed code in all text format.
This is for my java lab, and I need help with the Part A of it, which I have provided along with the IntNode.java. Any and all help is appreciated.
IntNode.java
public class IntNode
{
// instance variables
private int data;
private IntNode link;
// constructor
public IntNode(int initialData, IntNode initialLink)
{
data = initialData;
link = initialLink;
}
// the calling object is a reference to the
// node before the location where I want to add
public void addNodeAfter(int item)
{
link = new IntNode(item, link);
}
public int getData( )
{
return data;
}
public IntNode getLink( )
{
return link;
}
public static IntNode[ ] split ( IntNode original, int splitValue ) {
IntNode cursor = original;
IntNode answer[ ] = new IntNode [ 2 ];
IntNode list1=null, list1cursor=null;
IntNode list2=null, list2cursor=null;
while ( cursor != null ) {
if ( cursor.getData() < splitValue ) {
if ( list1cursor == null) { // list 1 is empty
list1 = new IntNode( cursor.getData(), null );
list1cursor = list1;
}
else {
list1cursor.setLink(new IntNode(cursor.getData(), null));
list1cursor = list1cursor.getLink();
}
cursor = cursor.link;
} // end if data < splitValue
else {
if ( list2cursor == null) { // list 2 is empty
list2 = new IntNode( cursor.getData(), null );
list2cursor = list2;
}
else {
list2cursor.setLink(new IntNode(cursor.getData(), null));
list2cursor = list2cursor.getLink();
}
cursor = cursor.link;
} // end data >= splitValue
} // end while
answer[0] = list1;
answer [1] = list2;
return answer;
}// end method
public static String listToString( IntNode head ) {
String answer = "";
IntNode cursor = head;
while (cursor != null) {
answer = answer + cursor.getData();
if (cursor.getLink() == null)
answer = answer + " End of List";
else
answer = answer + " --> ";
cursor = cursor.getLink();
}
return answer;
}
// call this method using IntNode.listCopy(reference to the head)
// it will duplicate the list and return a reference which
// is the head of the new list
public static IntNode listCopy(IntNode source)
{
IntNode copyHead;
IntNode copyTail;
// Handle the special case of the empty list.
if (source == null)
return null;
// Make the first node for the newly created list.
copyHead = new IntNode(source.data, null);
copyTail = copyHead;
// Make the rest of the nodes for the newly created list.
while (source.link != null)
{
source = source.link;
copyTail.addNodeAfter(source.data);
copyTail = copyTail.link;
}
// Return the head reference for the new list.
return copyHead;
}
public static IntNode[ ] listCopyWithTail(IntNode source)
{
IntNode copyHead;
IntNode copyTail;
IntNode[ ] answer = new IntNode[2];
// Handle the special case of the empty list.
if (source == null)
return answer; // The answer has two null references .
// Make the first node for the newly created list.
copyHead = new IntNode(source.data, null);
copyTail = copyHead;
// Make the rest of the nodes for the newly created list.
while (source.link != null)
{
source = source.link;
copyTail.addNodeAfter(source.data);
copyTail = copyTail.link;
}
// Return the head and tail references.
answer[0] = copyHead;
answer[1] = copyTail;
return answer;
}
public static int listLength(IntNode head)
{
int answer = 0;
for (IntNode cursor = head; cursor != null; cursor = cursor.link)
answer++;
return answer;
}
public static IntNode[ ] listPart(IntNode start, IntNode end)
{
IntNode copyHead;
IntNode copyTail;
IntNode cursor;
IntNode[ ] answer = new IntNode[2];
// Make the first node for the newly created list. Notice that this
will
// cause a NullPointerException if start is null.
copyHead = new IntNode(start.data, null);
copyTail = copyHead;
cursor = start;
// Make the rest of the nodes for the newly created list.
while (cursor != end)
{
cursor = cursor.link;
if (cursor == null)
throw new IllegalArgumentException
("end node was not found on the list");
copyTail.addNodeAfter(cursor.data);
copyTail = copyTail.link;
}
// Return the head and tail references
answer[0] = copyHead;
answer[1] = copyTail;
return answer;
}
public static IntNode listPosition(IntNode head, int position)
{
IntNode cursor;
int i;
if (position <= 0)
throw new IllegalArgumentException("position is not positive");
cursor = head;
for (i = 1; (i < position) && (cursor != null); i++)
cursor = cursor.link;
return cursor;
}
// search for a particular data value in the list
// return a reference to the node where the value
// was found
// if the value isn't in the list, return null
public static IntNode listSearch(IntNode head, int target)
{
IntNode cursor;
for (cursor = head; cursor != null; cursor = cursor.link)
if (target == cursor.data)
return cursor;
return null;
}
// this method is called using the node
// that is before the one we want to remove
public void removeNodeAfter( )
{
link = link.link;
}
// mutator
public void setData(int newData)
{
data = newData;
}
// mutator
public void setLink(IntNode newLink)
{
link = newLink;
}
}
Tile.java
public class Tile implements Cloneable {
// instance variables
private int color;
private int shape;
// Behavior #1: constructor
public Tile ( int c, int s ) {
// if invalid parameter values, default to 1
if (c >= 1 && c <= 6)
color = c;
else
color = 1;
if (s >= 1 && s <= 6)
shape = s;
else
shape = 1;
} // end constructor
// Behavior #2 : accessor for color
public int getColor( ) {
return color;
}
// Behavior #3 : accessor for shape
public int getShape( ) {
return shape;
}
// Behavior #4 : mutator for color
public void setColor( int c ) {
// doesn't change color if c is invalid
if ( c >= 1 && c <= 6 )
color = c;
}
// Behavior #5 : mutator for shape
public void setShape( int s ) {
// doesn't change shape if s is invalid
if ( s >= 1 && s <= 6 )
shape = s;
}
Download the instructor's Tile.java and QwirkleBag.java, and QwirkleTester.java. Save them in the folder for this assignment. CS 272-Lab 3 Using Linked Lists You will not modify these filles. They are the solution files for lab 2. Read through the code in the files to make sure you understand what each method is doing. The objectives of this lab are: 1) 2) Demonstrate mastery of using linked lists in Java. Show that you understand the IntNode ADT and can modify and extend the implementation to work with data other than integers A: Download IntNode.java from this assignment page in Canvas, then do the following steps Copy IntNode.java to a new file called TileNode java. This class will represent a node in a linked list. A TileNode object will have a Tile reference for data anda TileNode reference for link. In the Qwirkle game play, a linked list of TileNodes will represent the tiles in the player's hand. Change the class name. e Change the instance variables. There are 12 methods in the IntNode class. See the Javadoc given in class and with Exam 1. o Remove 3 methods: listPart, ListPosition, and listCopyWithTail. o Modify the other 9 methods as needed so that they work with Tile and TileNode (instead of int and IntNode). Incremental Programming: stop and compile after you write the code for each method. o Add 3 new methods: 1) public static void printList (TileNode head) This method should print the data from each tile in the list. Print two hyphens between nodes. Don't print a hyphen after the last node in the list. Example output: red equareblue dianond yellow clovereilow diamond zed circleblue cizcleExplanation / Answer
TileNode.java
public class TileNode
{
// instance variables
private int data;
private TileNode link;
// constructor
public TileNode(int initialData, TileNode initialLink)
{
data = initialData;
link = initialLink;
}
// the calling object is a reference to the
// node before the location where I want to add
public void addNodeAfter(int item)
{
link = new TileNode(item, link);
}
public int getData( )
{
return data;
}
public TileNode getLink( )
{
return link;
}
public static TileNode[ ] split ( TileNode original, int splitValue ) {
TileNode cursor = original;
TileNode answer[ ] = new TileNode [ 2 ];
TileNode list1=null, list1cursor=null;
TileNode list2=null, list2cursor=null;
while ( cursor != null ) {
if ( cursor.getData() < splitValue ) {
if ( list1cursor == null) { // list 1 is empty
list1 = new IntNode( cursor.getData(), null );
list1cursor = list1;
}
else {
list1cursor.setLink(new TileNode(cursor.getData(), null));
list1cursor = list1cursor.getLink();
}
cursor = cursor.link;
} // end if data < splitValue
else {
if ( list2cursor == null) { // list 2 is empty
list2 = new TileNode( cursor.getData(), null );
list2cursor = list2;
}
else {
list2cursor.setLink(new TileNode(cursor.getData(), null));
list2cursor = list2cursor.getLink();
}
cursor = cursor.link;
} // end data >= splitValue
} // end while
answer[0] = list1;
answer [1] = list2;
return answer
}// end method
public static String listToString( TileNode head ) {
String answer = "";
TileNode cursor = head;
while (cursor != null) {
answer = answer + cursor.getData();
if (cursor.getLink() == null)
answer = answer + " End of List";
else
answer = answer + " --> ";
cursor = cursor.getLink();
}
return answer;
}
// call this method using IntNode.listCopy(reference to the head)
// it will duplicate the list and return a reference which
// is the head of the new list
public static TileNode listCopy(TileNode source)
{
TileNode copyHead;
TileNode copyTail;
// Handle the special case of the empty list.
if (source == null)
return null;
// Make the first node for the newly created list.
copyHead = new TileNode(source.data, null);
copyTail = copyHead;
// Make the rest of the nodes for the newly created list.
while (source.link != null)
{
source = source.link;
copyTail.addNodeAfter(source.data);
copyTail = copyTail.link;
}
// Return the head reference for the new list.
return copyHead;
}
public static int listLength(TileNode head)
{
int answer = 0;
for (TileNode cursor = head; cursor != null; cursor = cursor.link)
answer++;
return answer;
}
// search for a particular data value in the list
// return a reference to the node where the value
// was found
// if the value isn't in the list, return null
public static TileNode listSearch(TileNode head, int target)
{
TileNode cursor;
for (cursor = head; cursor != null; cursor = cursor.link)
if (target == cursor.data)
return cursor;
return null;
}
// this method is called using the node
// that is before the one we want to remove
public void removeNodeAfter( )
{
link = link.link;
}
// mutator
public void setData(int newData)
{
data = newData;
}
// mutator
public void setLink(TileNode newLink)
{
link = newLink;
}
}
public static void printList(TileNode head)
{
if(head == null)
return;
TileHead node = head;
while(true) {
System.out.println(node.data);
if(node.link != null)
node=node.link;
else
break;
}
}
public static TileNode insertAtHead(TileNode head, TileNode tile)
{
TileNode node = new TileNode();
node.data = tile;
node.link = head;
return node;
}
public static TileNode reverse(TileNode original)
{
TileNode prev = null;
TileNode cur = original;
TileNode link = null;
while(cur != null) {
link = cur.link;
cur.link = prev;
prev = cur;
cur = link;
}
original = prev;
return original;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.