• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

How would i use Math.abs to solve this problem?

 
Ranch Hand
Posts: 264
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in this exercise:

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.

but after googling the Math.abs method I found this:

Java Math abs() method
The java.lang.Math.abs() returns the absolute value of a given argument.

If the argument is not negative, the argument is returned.
If the argument is negative, the negation of the argument is returned

but i am not sure how to use this to solve the problem

i tried this for a laugh


but it just returned the number that was int n

 
Sheriff
Posts: 13677
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about how you would calculate how far apart two numbers are. What arithmetic operation would you use. That is, to see if N is within 10 numbers of 100, what would you do? Dont think of abs() yet.

Now think of different values of N that could be provided. If you used the same formula, would you always get a positive result or will you sometimes get a negative result? Will the negative or positive nature of the result matter? If not then that is where abs() comes into play.
 
Marshal
Posts: 65372
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't google for the API. Bookmark it on your browser: link. You have missed out a corner case in the Math#abs(int) documentation, which might give you a wrong result if you are very unlucky.
 
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP

First, you haven't got method name right, "number" as method name is confusing and misleading for the task you were given, which in turn I trust pushes you away from the solution. You didn't think about the solution, but about the mechanics instead, or perhaps more accurate would be to say "tools" (Math.abs()) used in mechanics.

For this task to achieve I think you'd need at least 3 methods: [1]method which checks whether the given number N is within the 10 numbers of 100; [2]method which checks whether the given number N is within the 10 numbers of 200; [3]which gives the final answer whether [1] or [2] was the case (or maybe neither of those).

Method [1] maybe could return a boolean as well as method [2]. Method [3] perhaps String? Depending on what has been asked.

 
Campbell Ritchie
Marshal
Posts: 65372
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It says to return true, so the third method would return a boolean. That makes the third method easier to write.
 
Junilu Lacar
Sheriff
Posts: 13677
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
wayne brandon
Ranch Hand
Posts: 264
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar yes i appreciate that, number was just used as a quick test when i was messing with Math.abs

Campbell - take yourpoint on google, will only use that link you gave me from now

work is getting in the way of my coding today, so will give this a go later tonight thanks all
 
Campbell Ritchie
Marshal
Posts: 65372
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couple more links to bookmark: Java™ Tutorials Java® Language Specification (=JLS) Java12 version. You will only need the JLS occasionally.

wayne brandon wrote:. . . work is getting in the way of my coding today . . .

I thought you were a programminng student.
 
wayne brandon
Ranch Hand
Posts: 264
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell I had my own IT/Computer shop for 9 years in a small town in Ireland before moving to the big city of Cork.
I did a boot camp there recently as you know, im now starting a degree track in my community college in two months time (Cant wait)

I still have clients that i see about one to two days a week, right now im trying to recover an old database off an ide hard drive that is sixteen years old lol

sorry for boring you with my life stories
 
Campbell Ritchie
Marshal
Posts: 65372
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wayne brandon wrote:. . .  an ide hard drive that is sixteen years old lol . . .

An Eclipse drive or NetBeans? Oh no, you mean the old IDE hardware interface. I can still remember it .
 
wayne brandon
Ranch Hand
Posts: 264
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol yes the hdd standard that was before sata

whats your background Campbell?, where do you work?
you are in the UK right? I lived in London from 93 to 96, good times!!!

is England going to win the world cup?
 
Campbell Ritchie
Marshal
Posts: 65372
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wayne brandon wrote:. . . is England going to win the world cup?

Some people do ask silly questions
 
wayne brandon
Ranch Hand
Posts: 264
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LMAO good one!!!
 
Campbell Ritchie
Marshal
Posts: 65372
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I lived in London long before you did: about three years starting 1974.
 
Liutauras Vilda
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wayne brandon, any luck? Do you have something to show us already?
 
lowercase baba
Posts: 12760
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a side note, I think you approached this problem the wrong way.  No offense - I think you were set up wrong by the teacher.

You shouldn't approach any programming problem with "How do I use X to accomplish Y?".  The danger is you might be doing the equivalent of asking "How do I use a hammer to tighten a hex-nut?"  There probably is a way you could do it, but it may not be the best option for you.  It certainly could be - and in this case, using Math.abs can be useful...but it's not how you should approach the problem.

The first thing you should do is figure out how you, personally, would solve it using pencil and paper.  THEN try and figure out how you'd explain to a 3rd grade child how THEY can do it.  Only when you are done doing that should you start thinking about what tools Java has to let you do what you want.

just my 2 cents.
 
Campbell Ritchie
Marshal
Posts: 65372
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fred rosenberger wrote:. . .  "How do I use a hammer to tighten a hex-nut?" . . .

You drag somebody in from the street and threaten to hit them with the hammer until they tighten the nut for you You may have to lend them the correct size of spanner.
 
Junilu Lacar
Sheriff
Posts: 13677
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wayne brandon wrote:in this exercise:

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.


I'm not so sure about OP being steered wrong by the instructor. Given the above, it's kind of like saying "Build a house that's fit for a canary-sized bird. Note: A jigsaw can cut curves."

To someone who has built such things before, it's obvious that the note meant "You might want to try using a jigsaw at some point if your birdhouse has curves." The naive novice might think, however, that using a jigsaw is a key requirement to building the house and gets fixated on it.

To me, it seems like our OP might have just become fixated on when and how to use Math.abs() to solve the problem without necessarily thinking about the problem from a more holistic perspective. That's quite understandable and, of course, Fred is right in saying that you might want to take pen to paper first and work out the solution by hand before thinking about where in that process you might have a reason to use Math.abs().
 
wayne brandon
Ranch Hand
Posts: 264
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Liutauras Vilda no not yet, you see i try to work on multiple questions at once so that im not waiting for help on one topic and sitting idle. I am busy with a bank account exercise at the moment (Which I have just posted help on (surprise surprise) then will return to this now.

This question was from codebat and gave the Math.abs in a tip just below it.

also forgot to ask:

here is the question

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.

nearHundred(93) → true
nearHundred(90) → true
nearHundred(89) → false

do you think the question is referring to before 100 and 200? or after them too? how would you read this?

 
wayne brandon
Ranch Hand
Posts: 264
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok im thinking that n is the number you are given
so you should - 100 from n
then you should check to see if the difference between the deduction is
greater than or less than 10?

but i dont know how i would put this into code
 
Campbell Ritchie
Marshal
Posts: 65372
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't put anything into code.
Work out whether you know that 93 is (or isn't) within 10 of 100. Then work out how you know that. Write it down for us. Until you can explain the process, orr write it on paper, you can't program it.
 
wayne brandon
Ranch Hand
Posts: 264
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
100 - 93 = 7, yes it is within 10 of 100
100 - 89 = 11, no it is not within 10 of 100
 
Liutauras Vilda
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wayne brandon wrote:100 - 93 = 7, yes it is within 10 of 100
100 - 89 = 11, no it is not within 10 of 100


100 - 107 = ?
100 - 111 = ?

Is it within?
 
Liutauras Vilda
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How the result differs from your scenario?

Technically you don't need to use Math.abs(), but do you see how it can simplify the implementation logic?
 
Campbell Ritchie
Marshal
Posts: 65372
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:. . . Technically you don't need to use Math.abs() . . .

Would that prevent the risk of an arithmetic overflow? No, it probably wouldn't.
 
You showed up just in time for the waffles! And this tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!