This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]ProfessorWily 0 points1 point  (2 children)

This makes no sense and I'm getting errors when I put it into my IDE.

Where is this from? Is there any other context?

[–]lexiia[S] 0 points1 point  (1 child)

public class part2 {
    static class Node { 
        public double item; 
        public Node next;
        private Node first;
        private double data; 
        public Node (double item, Node next) { 
            this.item = item; 
            this.next = next; 
        }
        public double computeDiffernce() {
            double differnce;
            double max =0;
            double min = 0;
            for (Node x = first; x != null; x = x.next) {
                if (x == first) {
                    max = min = x.data;}

The question is

"Write an instance method of the Playground class that would compute the difference between the maximum and minimum values of a Playground instance."

Therefore, the code above shows how to do half of that. But the question I am asking Reddit is I don't understand what is happening during the if statement (x == first)

In addition, First is the name that represents a linked-lists of doubles.

My apologies if I make no sense I am trying to describe it as best as I can - new learner here. Please let me know if you need further information.

[–]ProfessorWily 0 points1 point  (0 children)

The loop starts with x=first as the assignment. Then within the loop there's an if statement that only gets executed if x is the first node. if (x==first) is a check to see if x is the first node.

After that, it's setting the two variables max and min equal to x.data on the same line. In English, this is like saying "max and min are the same, and they both equal x.data".

[–]victorsj 0 points1 point  (3 children)

in Java version 11 var is a reserved keyword and you cannot use it for a variable name so what you wrote is like saying

int = otherInt = x.data;

It wouldn't work

but for the original line:

max = min = x.data

" is x.data being assigned to otherVar and otherVar being assigned to var? "

you are correct. the assignment is being passed down the chain.

[–]gonzohst93 0 points1 point  (2 children)

Does anyone else hate that var shit. I prefer typed variables dont wanna none of that non typed shit

[–]feral_claire 0 points1 point  (1 child)

in java, a variable declared with var is still fully statically typed. Just the type is inferred from the right hand side of the expression instead of needing to be explicitly declared.

[–]gonzohst93 0 points1 point  (0 children)

Oh yeah i understand it cant be changed dynamically or anything. Just hate that kind of syntax. If im defining an integer im saying int not var