This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!

Greg Stevens

Ranch Hand
+ Follow
since Jul 23, 2009
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Greg Stevens

What would the interface for the Add class be? How would you use an object of type Add?
9 years ago
I remember now that the solution is the Euclidean algorithm. Never mind. I will try some other recursive problem.
9 years ago
I have not searched the forums on this because I want to try to work out as much of this on my own as I can.
I know how to solve this with with a loop. If the the larger integer can be divided by the smaller integer with a remainder
of 0, then the smaller integer is the greatest common divisor. Otherwise, iterate from (smaller - 1) down to 1 and check
for divisibility for both larger and smaller integer.

I know I should be thinking in terms of a base case and a simplification of the original problem, but I don't see it.

Any small hint?
9 years ago
Do you know how to get a numeric value from a String representation? What kind of numeric value are you
expecting the user to input?

Also, I think lunch is a confusing name for your variable. I read it and I think it should contain a menu item or
or some type of food. I don't think this is your intention. What input exactly are you expecting from the user?
9 years ago
Does this demonstrate that operator precedence is not the same thing as order of evaluation?




Any suggestions on how to make it cool and object-oriented like Campbell Ritchie's?
9 years ago
Concerning inheritance and overriding part of your question

You should read Sun's bicycle inheritance tutorial. Notice that the more specific type of bicycle class still uses most of the superclass's
definition (fields and methods) but may override a method or initialize some of the instance variables to different values.
9 years ago
Also interfaces can be used to describe a class that has two or more different sets of behavior. A class
can only have one superclass (inheritance), but it can implement the behavior of any number of interfaces.
Perhaps it will implement seemingly unrelated behavior that would never be described through inheritance
but can be expressed with interfaces:

9 years ago
Classes that implement interfaces don't override methods they implement them. There was never
a previous definition of the method to override. They are providing the first definition. This mechanism
of stating that a class will have a method but not providing a default implementation is useful if a set of
objects all do a certain activity, but there is no default way for doing it. They each do it their own way.

9 years ago
It is an introduction to object-oriented programming using Java class. The instructor clearly has operator precedence and
order of evaluation confused. I'm glad I came here, because even after reading the JLS, I knew something was wrong, but
not exactly what -- the specification is not very easy to decipher for mere mortals like myself. I would still be confused if
you guys hadn't told me operator precedence is not the same as order of evaluation.
9 years ago
Thanks for the patience guys. I think I am getting a little closer to understanding this.

Precedence has more to do with which operator is applied to an operand when it is shared by two operators. Precedence does not mean
order of evaluation (like order of evaluation in math).

So,



The higher precedence of ++ and -- compared to * means that they are each applied to their operands before *, which shares an operand
with each. There really is no meaning to saying postdecrement has higher precedence than preincrement because those two operators
would never share an operand?

And next example is the one I need to definitely be sure I understand, because this is how it is being presented in my class:


The instructor is saying x++ is evaluated first (like one would do with PEMDAS and exponents), giving 5 and changing x to 6, then the first x
evaluates to 6 and we have 6 + 5 == 11. He says it is a compiler error causing it to evaluate 10 and we will have "order of evaluation
problems" on our midterm and we need to know how to do them correctly, according to the "ANSI standard". I know this is wrong. I at least
want to make sure I understand them if I get them all wrong on the test.

But now I understand that the higher precedence of ++ above only means that x is incremented before the two operands of * are
multiplied. And according to JLS 15.7.1, the left operand of a binary operator is evaluated first. This is not in contradiction to the higher
precedence of ++ because ++ is applied to its operand before * is applied to its two operands.

Precedence and Evaluation Order are two different things. I feel much better now. The world is all well again -- as well as it was before
I freaked out anyway.
9 years ago
I'm still confused. I've been trying to figure this out all day. I think I'll just have to let it go for a while. It just makes no sense whatsoever to me to
say post increment operator has highest precedence post decrement operator has higher precedence than pre increment operator, but
pre increment is evaluated first. Obviously I am confused about the meaning of precedence. Common usage of precedence refers to one thing happening
before another. How can something that happens first happen last?
9 years ago

but I hope this will teach you not to write illegible code



I would never write code like that. I have definitely learned not to use pre or post increment in an expression in which the variable occurs
more than once.

I see that the compiler gives the answer it does. Actually, my problem is that that I am studying in a class and the instructor is teaching that
the rules of precedence apply this way (obviously incorrect). I think he is confused. I am trying to figure out what is correct.

9 years ago
Thank you for walking through the examples. What I don't understand is what does it mean for the unary operators to have higher
precedence than the equality operators if the are not evaluated first.

In other words

num = 10
num == num++

shouldn't num++ be evaluated first (evaluating to 10, but incrementing num to 11), then when num on the left is evaluated it
would be 11. 11 == 10: false
9 years ago
In both of the examples the terms are simplified from left to right. The rightmost postfix operator is not applied before the middle prefix operator.
What is the point of saying postfix has higher precedence than prefix if it doesn't get evaluated first?

int i = 10;
int a, b, c, d;
a = (b = i++) + (c = ++i) + (d = i--);

if postfix evaluates first shouldn't we get

b = 10, i = 11
d = 11, i = 10
+ c = 11, i = 11
----------
a = 32
9 years ago
I am confused with the following code:


Here is the output:


"Conditional 2" and "conditional 4" do not behave as I would expect according to the operator precedence
rules. If the unary operators have higher precedence than the equality operators, then why aren't num++
and ++num evaluated first when they are on the right side of the == operator?

I am especially confused by this because the Java Language Specification says:

15.7.1 Evaluate Left-Hand Operand First
The left-hand operand of a binary operator appears to be fully evaluated before
any part of the right-hand operand is evaluated.



I'm sure I'm probably taking this out of context, but in "conditional 2" and "conditional 4", the left-hand
operands certainly seem to be evaluated first. If left-hand operands of binary operators are evaluated
first, what is the point of the higher precedence of unary operators? Are the unary operators applied
first seperately on each side of the binary operator, recusrively in some sort of tree structure. That seems
rediculously confusing. I know this is supposed to be very simple, but I can't seem to get past the apparent
contradiction. Thanks in advance for any help.
9 years ago