Notices
The Basement Non-Honda/Acura discussion. Content should be tasteful and "primetime" safe.

Who knows Java?

Thread Tools
 
Old Apr 6, 2004 | 04:31 PM
  #1  
å's Avatar
å
Thread Starter
åhhhhh
 
Joined: Mar 2002
Posts: 7,992
Likes: 0
From: Toronto, Canada
Default Who knows Java?

can you write a method that takes in an integer and checks to see if its a perfect square (ie returns a boolean value)?

ie 4 is a perfect square sqrt(4) = 2
2 is not a perfect square since sqrt(2) = not an int.


or is there a method that checks if something is an integer??
Reply
Old Apr 6, 2004 | 08:30 PM
  #2  
mayonaise's Avatar
mayonaise
Senior Member
 
Joined: Aug 2002
Posts: 3,181
Likes: 0
From: CA
Default

Originally Posted by å
can you write a method that takes in an integer and checks to see if its a perfect square (ie returns a boolean value)?

ie 4 is a perfect square sqrt(4) = 2
2 is not a perfect square since sqrt(2) = not an int.


or is there a method that checks if something is an integer??
of course you can, and of course there is.

but checking if something is an integer would only apply to an object that is not already an integer, like a string or a double (probably going to be a double). i don't think there's a built in method to check whether an object is an integer or not, so you might have to do it yourself. should be pretty easy, tho. look up the java API:
http://java.sun.com/j2se/1.4.2/docs/api/
Reply
Old Apr 6, 2004 | 08:55 PM
  #3  
cowanpp's Avatar
cowanpp
Card carrying badass
 
Joined: Mar 2004
Posts: 768
Likes: 0
From: Little Rock, AR
Default

something like:

boolean SquareCheck(int x){

double y = sqrt(x);
int q = sqrt(x);
y = y * 10000000; //use however many decimal places the double
//could hold
x = x * 10000000;
if( (y-x) != 0) return false;
return true;
}

This would rely on the sqrt function merely truncating the value returned from the sqrt function, which I can't remember if that's what happens or not. I'm a little rusty.
Reply
Old Apr 6, 2004 | 08:56 PM
  #4  
Baco95's Avatar
Baco95
that just ain't baaalla!
 
Joined: Jul 2002
Posts: 365
Likes: 0
From: Newburgh, NY
Default

public boolean perfectSquare(int anInteger)
{
boolean perfectSquare;
double theInteger = (double)anInteger;
double sqrt = sqrt(theInteger);

double perfectSquare = sqrt * sqrt;

if(theInteger == perfectSquare)
{
perfectSqaure = true;
}

else
{
perfectSquare = false;
}


return perfectSquare;

}


Something like that....include java.lang.Math

Check out http://java.sun.com/j2se/1.4.2/docs/api/index.html
Reply
Old Apr 6, 2004 | 09:36 PM
  #5  
mayonaise's Avatar
mayonaise
Senior Member
 
Joined: Aug 2002
Posts: 3,181
Likes: 0
From: CA
Default

man, you guys give the answer out way too easily. gotta make him think about it a little bit!
Reply
Old Apr 7, 2004 | 06:24 AM
  #6  
Baco95's Avatar
Baco95
that just ain't baaalla!
 
Joined: Jul 2002
Posts: 365
Likes: 0
From: Newburgh, NY
Default

lol...yeah I was drunk last night h:
Reply
Old Apr 7, 2004 | 07:00 AM
  #7  
reno96teg's Avatar
reno96teg
Moderator
 
Joined: Dec 2000
Posts: 21,573
Likes: 0
Default

um.. how about..

if the mantissa of sqrt(x) == 0, then x is a perfect square. but you'd have to figure out what function gets the mantissa, which puts you in a similar spot.

Last edited by reno96teg; Apr 7, 2004 at 07:08 AM.
Reply
Old Apr 7, 2004 | 10:20 AM
  #8  
å's Avatar
å
Thread Starter
åhhhhh
 
Joined: Mar 2002
Posts: 7,992
Likes: 0
From: Toronto, Canada
Default

i already got it last night h: thanks for the help anyways
Code:
public static boolean isSquare(int num)
{
if(Math.sqrt(num)==(int)(Math.sqrt(num)))
  return true;
else
  return false;
}
Reply
Old Apr 7, 2004 | 10:48 AM
  #9  
cowanpp's Avatar
cowanpp
Card carrying badass
 
Joined: Mar 2004
Posts: 768
Likes: 0
From: Little Rock, AR
Default

are you sure that works? I thought that when you caste from a double to an int, it truncates, which would cause problems with the way you are doing it. Have you checked it on several different numbers?
Reply
Old Apr 7, 2004 | 11:32 AM
  #10  
å's Avatar
å
Thread Starter
åhhhhh
 
Joined: Mar 2002
Posts: 7,992
Likes: 0
From: Toronto, Canada
Default

it gave me the output i wanted..

i wanted to find 4 numbers where the sum of each pair is a perfect square .. it gave me some that werent .. but bleh it gave me lots that were.. i needed 1 set

im done
Reply
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
TeggerLS
The Basement
6
Jan 29, 2004 11:14 AM
nizmugen
The Basement
5
Jan 20, 2004 04:26 AM
huviduc
The Basement
6
Oct 22, 2003 09:03 PM
sman789
The Basement
6
Sep 14, 2003 12:56 PM
hondav6
The Basement
3
Mar 23, 2003 07:59 AM




All times are GMT -8. The time now is 05:23 AM.