Results 1 to 12 of 12
  1. #1
    Legoland is offline Member
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    12
    Rep Power
    0

    Default Calculate what e1 and e2 should be

    Hi!
    I hope someone can help me out.
    If e1 has value 5 and e2 has value 8 before this lines has been executed:
    int t = e1;
    e1 = e2;
    e2 = t;
    what value will e1 and e2 has after that.
    I have started like this: mport javax.swing.*;

    public class Tilldelning2 {
    public static void main (String[] arg) {
    int e1 = 5;
    int e2 = 8;
    int t = e1;
    e1 = e2;
    e2 = t;
    JOptionPane.MessageDialog(null, "e1 =" + t +" e2");
    System.exit(0);
    }
    }

    Thanks for help in advance Legoland,

  2. #2
    pegitha is offline Member
    Join Date
    Apr 2007
    Location
    Indiana
    Posts
    83
    Rep Power
    0

    Default

    I am not sure I am understanding your question. e1 will still =8 and e2 will still =5.
    However, I cannot compile this code because it fails on the
    "JOptionPane.MessageDialog". I do not find any static method called
    MessageDialog in JOptionPane. Maybe I am using a different version of java than you. I am still on 5.0.
    p

  3. #3
    Legoland is offline Member
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    12
    Rep Power
    0

    Default Thanks for trying.

    hi!
    Yes e1 will still have value 5 and e2 will have value 8. Maybe I have another version of Java than you.
    Legoland

  4. #4
    pegitha is offline Member
    Join Date
    Apr 2007
    Location
    Indiana
    Posts
    83
    Rep Power
    0

    Default

    If I use JOptionPane.showMessageDialog(null, "e1 =" + t +" e2");
    then what popup is:
    e1=5 e2
    Is that your question?

  5. #5
    Legoland is offline Member
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    12
    Rep Power
    0

    Default Thanks agin!

    Yes
    it helped me a bit on the way I must have missed to write show before message in JOptionPane.....
    anyway now I have to figure out how to get the new value into e2 i think the message to the suser should be something like e1= 5 e2=... and then some value.

  6. #6
    goldhouse is offline Senior Member
    Join Date
    Mar 2007
    Posts
    134
    Rep Power
    0

    Smile Swapping Algorithm

    This is one of the tricky question often asked in interviews.
    The result for the below will be
    Java Code:
    int a =5, b =8;
    temp = a;
    a = b;
    b = temp;
    System.out.println("a = " + a + " b = " + b) // give result a=8 b =5
    The above code swapped the variable values , Now the question is how can we achieve this without using the temp.

    Java Code:
    int double =5,double = 8;
    a = a + b;
    b = a -b;
    a = (a - b) /2 ;
    System.out.println("a = " + a + " b = " + b) // give result a=8 b =5
    I like somebody to explain the below code.

  7. #7
    Legoland is offline Member
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    12
    Rep Power
    0

    Default Calculate what e2 and e1 will be

    Hello!
    If e1 = 5 and e2 = 8 before the following lines has been executed what values will they have afterwards.
    The lines are: int t = e1;
    e1 = e2;
    e2 = t;

    Create a program that shows the new values of e1 and e2

    I hope I have exlained this correctly.
    Regards Legoland

  8. #8
    goldhouse is offline Senior Member
    Join Date
    Mar 2007
    Posts
    134
    Rep Power
    0

    Default

    :) :) :) :)

  9. #9
    levent Guest

    Default

    Hello!
    If e1 = 5 and e2 = 8 before the following lines has been executed what values will they have afterwards.
    The lines are: int t = e1;
    e1 = e2;
    e2 = t;

    Create a program that shows the new values of e1 and e2

    Your code should work. What is the problem?

  10. #10
    Legoland is offline Member
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    12
    Rep Power
    0

    Default Hi Levent

    Hi!
    When I runs the program I get the result e1=55 but I dont get a value for e2
    Legoland

  11. #11
    Legoland is offline Member
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    12
    Rep Power
    0

    Default

    Hi!
    I have created new program it looks like this:
    import javax.swing.*;

    public class Tilldelning4 {
    public static void main (String[] arg) {
    int e1 = 5;
    int e2 = 8;
    int t = e1;
    e1 = e2;
    e2 = t;
    JOptionPane.showMessageDialog(null, "e1 =" + t + e2 + " e2=" + e2 + t + e1 );
    System.exit(0);

    }
    }
    When I run the program I get e1= 55 and e2= 558
    How can I make sure that i have the right result??
    Legoland

  12. #12
    olatunbode is offline Member
    Join Date
    Jul 2007
    Posts
    5
    Rep Power
    0

    Default

    when you concatenate using a string and some other variables, all the values are concatenated together, so if a = 5 and b = 6, the output of the code:

    System.out.println("The values are: " + a + b);

    would be: The values are: 56

    That is because it converts the primitives a and b to strings and concatenates them together. If u want a and b to be added, this will to it:

    System.out.println("The values are: " + (a + b));

    This is becos parenthesis takes precedence anywhere!

    But if u want the two values to be displayed 'as is', you have to put a space btw them i.e

    System.out.println("The values are: " + a + " " + b);

    Take care!

Similar Threads

  1. Calculate Average
    By sthack99 in forum New To Java
    Replies: 4
    Last Post: 06-13-2008, 12:09 PM
  2. Calculate Tax in java
    By toby in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 10:03 AM
  3. Calculate average age for women and men?
    By Legoland in forum New To Java
    Replies: 3
    Last Post: 04-18-2007, 11:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •