Page 1 of 1

what this method does

Posted: Sat Feb 17, 2018 11:51 pm
by maty.tsoraro@gmail.com
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;
}
}

Re: what this method does

Posted: Mon Feb 19, 2018 1:54 am
by dbremmen@gmail.com
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

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));
        
    }
    
}
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

Re: what this method does

Posted: Tue Feb 20, 2018 4:43 am
by maty.tsoraro@gmail.com
big help
thanks alot :mrgreen: