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

Discussion in 'Java' started by -, Jun 10, 2005.

  1. -

    - Guest

    Is there a function to convert '1' into '-1' and vice versa?
     
    -, Jun 10, 2005
    #1
    1. Advertising

  2. - 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/
     
    Knute Johnson, Jun 10, 2005
    #2
    1. Advertising

  3. -

    JScoobyCed Guest

    - 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 :-D

    --
    JSC
     
    JScoobyCed, Jun 10, 2005
    #3
  4. -

    . Guest

    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
     
    ., Jun 10, 2005
    #4
  5. .. 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/
     
    Knute Johnson, Jun 10, 2005
    #5
  6. -

    Roland Guest

    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_/ /__/
    / \ /_/ / \
     
    Roland, Jun 10, 2005
    #6
  7. -

    - Guest

    .. 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. :p
     
    -, Jun 11, 2005
    #7
  8. -

    Wibble Guest

    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 :-D
    >
    > --
    > JSC

    I love that! You're my hero!
     
    Wibble, Jun 11, 2005
    #8
  9. 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 :-D

    > 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"]
     
    Antti S. Brax, Jun 11, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Flyingaway
    Replies:
    6
    Views:
    22,810
    Jack Klein
    Feb 19, 2005
  2. Byron
    Replies:
    6
    Views:
    504
    Byron
    Sep 16, 2004
  3. thinktwice
    Replies:
    4
    Views:
    904
    =?iso-8859-1?q?Kirit_S=E6lensminde?=
    Sep 7, 2006
  4. Replies:
    3
    Views:
    8,807
  5. chen li
    Replies:
    3
    Views:
    124
    Daniel Martin
    Jul 13, 2007
Loading...

Share This Page