Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to convert '1' into '-1' and vice versa?

Reply
Thread Tools

How to convert '1' into '-1' and vice versa?

 
 
-
Guest
Posts: n/a
 
      06-10-2005
Is there a function to convert '1' into '-1' and vice versa?
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      06-10-2005
- wrote:

> Is there a function to convert '1' into '-1' and vice versa?


public int negate(int n) {
return -n;
}

or just;

n = -n;

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
 
 
 
JScoobyCed
Guest
Posts: n/a
 
      06-10-2005
- wrote:
> Is there a function to convert '1' into '-1' and vice versa?


public class Negate {

private Object lock = new Object();

public static int negate(int n) {
int tmp = 0;
synchronized(lock) {
tmp = n - (2 * n);
int loop = 0;
if(n > 0) {
loop = (new Random()).nextInt(n*1000);
for(int i=0; i<loop; i++) {
int j = i * (-1);
if((0 - j) == n) {
tmp = j;
break;
}
}
}
else {
loop = (new Random()).nextInt((-n)*1000);
for(int i=0; i<loop; i++) {
int j = i;
if((0 - j) == n) {
tmp = j;
break;
}
}
}
}
return tmp;
}

We can still find more complexe

--
JSC
 
Reply With Quote
 
.
Guest
Posts: n/a
 
      06-10-2005
On Fri, 10 Jun 2005, - wrote:

> Is there a function to convert '1' into '-1' and vice versa?


What datatype is '1' and '-1'? This is not proper notation for Java. The
use of single quotes denotes a char but the '-1' would be an invalid char.

So are you asking to convert "1" to "-1"? Or maybe '1' to "-1"? Or more
likely 1 to -1? The last option is very easy. i.e.

int n = 1;
n = -n;

--
Send e-mail to: darrell dot grainger at utoronto dot ca

 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      06-10-2005
.. wrote:
> On Fri, 10 Jun 2005, - wrote:
>
>
>>Is there a function to convert '1' into '-1' and vice versa?

>
>
> What datatype is '1' and '-1'? This is not proper notation for Java. The
> use of single quotes denotes a char but the '-1' would be an invalid char.
>
> So are you asking to convert "1" to "-1"? Or maybe '1' to "-1"? Or more
> likely 1 to -1? The last option is very easy. i.e.
>
> int n = 1;
> n = -n;
>


..

What is it you are really trying to do?

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
Roland
Guest
Posts: n/a
 
      06-10-2005
On 10-6-2005 19:59, Knute Johnson wrote:

> . wrote:
>
>> On Fri, 10 Jun 2005, - wrote:
>>
>>
>>> Is there a function to convert '1' into '-1' and vice versa?

>>
>>
>>
>> What datatype is '1' and '-1'? This is not proper notation for Java. The
>> use of single quotes denotes a char but the '-1' would be an invalid
>> char.
>>
>> So are you asking to convert "1" to "-1"? Or maybe '1' to "-1"? Or more
>> likely 1 to -1? The last option is very easy. i.e.
>>
>> int n = 1;
>> n = -n;
>>

>
> .
>
> What is it you are really trying to do?


I don't think that OP ("-") is the same person you are replying to (".").
--
Regards,

Roland de Ruiter
` ___ ___
`/__/ w_/ /__/
/ \ /_/ / \
 
Reply With Quote
 
-
Guest
Posts: n/a
 
      06-11-2005
.. wrote:
> On Fri, 10 Jun 2005, - wrote:
>
>
>>Is there a function to convert '1' into '-1' and vice versa?

>
>
> What datatype is '1' and '-1'? This is not proper notation for Java. The
> use of single quotes denotes a char but the '-1' would be an invalid char.
>
> So are you asking to convert "1" to "-1"? Or maybe '1' to "-1"? Or more
> likely 1 to -1? The last option is very easy. i.e.
>
> int n = 1;
> n = -n;
>


Indeed, the last option was what I wanted to do.

I was thinking along the line that since there is a Math.abs(double)
function, I thought there was a method to do what I wanted.

Didn't realise it was that easy. No wonder it doesn't warrant a method
of its own.
 
Reply With Quote
 
Wibble
Guest
Posts: n/a
 
      06-11-2005
JScoobyCed wrote:
> - wrote:
>
>> Is there a function to convert '1' into '-1' and vice versa?

>
>
> public class Negate {
>
> private Object lock = new Object();
>
> public static int negate(int n) {
> int tmp = 0;
> synchronized(lock) {
> tmp = n - (2 * n);
> int loop = 0;
> if(n > 0) {
> loop = (new Random()).nextInt(n*1000);
> for(int i=0; i<loop; i++) {
> int j = i * (-1);
> if((0 - j) == n) {
> tmp = j;
> break;
> }
> }
> }
> else {
> loop = (new Random()).nextInt((-n)*1000);
> for(int i=0; i<loop; i++) {
> int j = i;
> if((0 - j) == n) {
> tmp = j;
> break;
> }
> }
> }
> }
> return tmp;
> }
>
> We can still find more complexe
>
> --
> JSC

I love that! You're my hero!
 
Reply With Quote
 
Antti S. Brax
Guest
Posts: n/a
 
      06-11-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in comp.lang.java.programmer:
> JScoobyCed wrote:
>> - wrote:
>>> Is there a function to convert '1' into '-1' and vice versa?

>>

<crap removed>
>>
>> We can still find more complexe

> I love that! You're my hero!


That is bullshit. If you're going to write a comedy response
for a trivial question, at least make it work properly.

Hint 1: the code will fail miserably if n is greater than
Integer.MAX_VALUE / 1000.

Hint 2: you can't access instance variables from static
context.

Also, it is trivial to make a complex example by adding
useless code (ie. the unnecessary synchronization block).
You might as well take the source code for Tomcat, embed
it inside your method and then return -n in the end.

If you need a hero in the field of comedy coding, please
read this: http://home.tiac.net/~cri/2001/badsort.html

--
Antti S. Brax Rullalautailu pitää lapset poissa ladulta
http://www.iki.fi/asb/ http://www.cs.helsinki.fi/u/abrax/hlb/

[1385 messages expunged from folder "Spam"]
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert UTC to local time(system time) and vice vesra in VB.NET deepak ASP .Net 1 02-06-2008 02:34 PM
Convert std::string to std::vector<unsigned char> and vice versa timor.super@gmail.com C++ 3 04-02-2007 02:12 PM
how to convert narrow string to wide string and vice versa? thinktwice C++ 4 09-07-2006 10:55 AM
Why convert Word doc to PDF? Or vice versa Harry Computer Support 3 03-08-2005 12:44 PM
Convert a string to float and vice versa Flyingaway C++ 6 02-19-2005 11:08 PM



Advertisments