This is a code that I have for my Java class, however the last three will not co
ID: 3594457 • Letter: T
Question
This is a code that I have for my Java class, however the last three will not compile. If they are commented, out the rest will compile, if anyone can help me fix it, or show me how I would be grateful, the ones that wont compile have been marked, and the rest of the code is also with it. Thank you in advance.
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 sValue ) {
TileNode cursor = original;
TileNode answer[ ] = new TileNode [ 2 ];
TileNode list1=null, list1cursor=null;
TileNode list2=null, list2cursor=null;
while ( cursor != null ) {
if ( cursor.getData() < sValue ) {
if ( list1cursor == null) { // list 1 is empty
list1 = new TileNode( cursor.getData(), null );
list1cursor = list1;
}
else {
list1cursor.setLink(new TileNode(cursor.getData(), null));
list1cursor = list1cursor.getLink();
}
cursor = cursor.link;
} // end if data < sValue
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;
}
public void setLink(TileNode newLink){
link = newLink;
}
}
//added portion for the lab
//wont compile
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;
}
}
//wont compile
public static TileNode insertAtHead(TileNode head, TileNode tile){
TileNode node = new TileNode();
node.data = tile;
node.link = head;
return node;
}
//wont compile
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;
}
Explanation / Answer
public class TileNode{
// instance variables
private int data;
private TileNode link;
public TileNode()
{}
// 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 sValue ) {
TileNode cursor = original;
TileNode answer[ ] = new TileNode [ 2 ];
TileNode list1=null, list1cursor=null;
TileNode list2=null, list2cursor=null;
while ( cursor != null ) {
if ( cursor.getData() < sValue ) {
if ( list1cursor == null) { // list 1 is empty
list1 = new TileNode( cursor.getData(), null );
list1cursor = list1;
}
else {
list1cursor.setLink(new TileNode(cursor.getData(), null));
list1cursor = list1cursor.getLink();
}
cursor = cursor.link;
} // end if data < sValue
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;
}
public void setLink(TileNode newLink){
link = newLink;
}
//added portion for the lab
//wont compile
public static void printList(TileNode head){
if(head == null)
return;
TileNode node = head;
while(true) {
System.out.println(node.data);
if(node.link != null)
node=node.link;
else
break;
}
}
//wont compile
public static TileNode insertAtHead(TileNode head, TileNode tile){
TileNode node = new TileNode();
node.data = tile.data;
node.link = head;
return node;
}
//wont compile
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;
}
}
The errors i solved are
1) Included a default argument constructor when parameterized constructor is supplied for the below error
error: constructor TileNode in class TileNode cannot be applied to given types;
TileNode node = new TileNode();
2) The error i solved below is replaced TileHead with TileNode
error: cannot find symbol
TileHead node = head;
3) The error i solved below is replaced the below statement with node.data=tile.data
error: incompatible types: TileNode cannot be converted to int
node.data = tile;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.