hi all,
i have 2 question :
1)what does the method "what " below does and if you can explain me each line will be awsome..
2)how can i write the main and test it,like input a alist abd test this method
public class IntNode{
private int _data;
private IntNode _next;
public IntNode (int data){
_data = data;
_next = null;
}
public static boolean what(IntNode node){
IntNode t = node;
IntNode h = node._next;
int sum = 0;
boolean ok = false;
if(t != null)
sum = t._data;
while(h != null)
{
if(sum != h.data)
return false;
sum += h.data;
t = h;
h = h._next;
}
return true;
}
}
what this method does
-
- Posts: 63
- Joined: Sun Jan 07, 2018 6:30 pm
Re: what this method does
Hi!
To make easier for anybody to help with your programs please indent them and enclose in code tags (use button </>)
I indented and updated your code with a starting point
What this code does is to check that the sum of the value in the nodes is the sum of the previous ones.
For example I created three nodes and linked them. So I have 5->5->10. In this case the program return "true". If I change the last node and put a 9 for example the code will return false.
You can check your code and add more nodes to test it
To make easier for anybody to help with your programs please indent them and enclose in code tags (use button </>)

I indented and updated your code with a starting point
Code: Select all
public class IntNode{
private int _data;
private IntNode _next;
public IntNode (int data){
_data = data;
_next = null;
}
public static boolean what(IntNode node) {
IntNode t = node;
IntNode h = node._next;
int sum = 0;
boolean ok = false;
if(t != null)
sum = t._data;
while(h != null) {
if(sum != h._data)
return false;
sum += h._data;
t = h;
h = h._next;
}
return true;
}
public static void main (String[] args) {
IntNode node1 = new IntNode(5);
IntNode node2 = new IntNode(5);
IntNode node3 = new IntNode(10);
node1._next = node2;
node2._next = node3;
System.out.println(what(node1));
}
}
For example I created three nodes and linked them. So I have 5->5->10. In this case the program return "true". If I change the last node and put a 9 for example the code will return false.
You can check your code and add more nodes to test it
-
- Posts: 32
- Joined: Thu Dec 21, 2017 10:06 pm
Re: what this method does
big help
thanks alot
thanks alot
