programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# CodingBat Warmup nearHundred

Lucas Mulherin
Greenhorn
Posts: 4
First off,I'm very new to java, and coding in general.
But this is the only problem that I can never think through, or figure out in the warm up section of this website, even when I look at the answer code that they give me.
I may not properly understand absolute numbers, or maybe I'm just not thinking about this problem correctly.

But here is the problem;

"Given an int n, return true if it is within 10 of 100 or 200. Note: Math.abs(num) computes the absolute value of a number. "

This is what they give you to write your code in;

And here is the solution;

I guess I'm just wondering if someone could explain exactly what this code is doing or what the problem is asking.
It also doesn't help that I'm super tired right now, but sorry if it's a pretty dumb question hahaha.

Campbell Ritchie
Marshal
Posts: 56541
172
Start by teasing the code apart. What does number - 100 mean? Then Math.abs (read about it in the API documentation first) then the <= bit.

fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
sometimes, it also helps to "be the computer". Pick some numbers, and try and try to step through the code to see what it does.

Domi Dori
Greenhorn
Posts: 6
2
My solution would be

Hey, they did not specify the return value for all the other cases.

But if we pretend that there is a requirement to return false otherwise the given solution is wrong.

For the beginners: Just iterate over every possible int value. You will get a surprise.

For all the others try to figure out why without code.

As a hint: there are 2 additional values which return true, but they are a lot more of 100 or 200 than by 10.

Campbell Ritchie
Marshal
Posts: 56541
172
What a poor solution: have a skull.

Domi Dori
Greenhorn
Posts: 6
2
Ok, I agree about the first part of my answer. In the original exercise is an example of returning false.

But the given solution here (and found on many websites) does not work for all int values.

Campbell Ritchie
Marshal
Posts: 56541
172
…and which int values will that solution fail for?

Domi Dori
Greenhorn
Posts: 6
2
-2147483548 and -2147483448.

100 - -2147483548 leads to an overflow => Integer.MIN_VALUE

Math.abs(Integer.MIN_VALUE) = Integer.MIN_VALUE

Integer.MIN_VALUE <= 10 is true.

Campbell Ritchie
Marshal
Posts: 56541
172
Yes, they will.

Campbell Ritchie
Marshal
Posts: 56541
172
Tbat merits removing the skull

Winston Gutkowski
Bartender
Posts: 10575
66
Domi Dori wrote:-2147483548 and -2147483448.
100 - -2147483548 leads to an overflow => Integer.MIN_VALUE
Math.abs(Integer.MIN_VALUE) = Integer.MIN_VALUE
Integer.MIN_VALUE <= 10 is true.

Very nicely thought out. That merits a cow. I have to admit that the solution looked wrong at first glance to me too, but hadn't actually worked out why.

I think it might have been better for Math.abs() to throw an exception in the case you point out; and I actually have an abs() function that returns Integer.MAX_VALUE for Integer.MIN_VALUE.

Just one of the problems with the asymmetric nature of two's-compliment.

Winston

Campbell Ritchie
Marshal
Posts: 56541
172
Winston Gutkowski wrote: . . .
Just one of the problems with the asymmetric nature of two's-compliment.

Winston
Like problems somebody else had yesterday? Look here.
It is a case of Swings and Roundabouts. For the errors caused by the asymmetry you gain the ability to calculate hash codes. Remember the hash code method on all but the smallest object will be risking overflow, but that is permissible as long as it is predictable and reproducible. Some languages do throw overflow exceptions but Gosling and those who designed Java thought the advantages of overflow made it worth retaining.

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?