Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-26-2007, 06:00 PM
Member
 
Join Date: Apr 2007
Location: Sweden
Posts: 12
Legoland is on a distinguished road
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,
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-26-2007, 06:10 PM
Member
 
Join Date: Apr 2007
Location: Indiana
Posts: 84
pegitha is on a distinguished road
Send a message via Skype™ to pegitha
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-26-2007, 06:32 PM
Member
 
Join Date: Apr 2007
Location: Sweden
Posts: 12
Legoland is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-26-2007, 06:40 PM
Member
 
Join Date: Apr 2007
Location: Indiana
Posts: 84
pegitha is on a distinguished road
Send a message via Skype™ to pegitha
If I use JOptionPane.showMessageDialog(null, "e1 =" + t +" e2");
then what popup is:
e1=5 e2
Is that your question?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-26-2007, 09:14 PM
Member
 
Join Date: Apr 2007
Location: Sweden
Posts: 12
Legoland is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-27-2007, 03:03 PM
Senior Member
 
Join Date: Mar 2007
Posts: 136
goldhouse is on a distinguished road
Swapping Algorithm
This is one of the tricky question often asked in interviews.
The result for the below will be
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.

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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-27-2007, 03:51 PM
Member
 
Join Date: Apr 2007
Location: Sweden
Posts: 12
Legoland is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-27-2007, 08:24 PM
Senior Member
 
Join Date: Mar 2007
Posts: 136
goldhouse is on a distinguished road
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-27-2007, 10:55 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
Quote:
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?
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 04-28-2007, 11:45 AM
Member
 
Join Date: Apr 2007
Location: Sweden
Posts: 12
Legoland is on a distinguished road
Hi Levent
Hi!
When I runs the program I get the result e1=55 but I don´t get a value for e2
Legoland
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 04-28-2007, 12:50 PM
Member
 
Join Date: Apr 2007
Location: Sweden
Posts: 12
Legoland is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 07-02-2007, 07:01 PM
Member
 
Join Date: Jul 2007
Posts: 5
olatunbode is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

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


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


All times are GMT +3. The time now is 10:38 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org