Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 11th, 2005, 11:16 AM
Jenkins Jenkins is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 1 Jenkins User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 30 sec
Reputation Power: 0
Help on a Programming Problem :)

Hey guys. Im working on this question and im getting thrown completely becuase i dont understand how it is getting the answers shown below the code.... I have narrowed it down to the this.n.. if someone could please explain how the this.n is effecting the program and why it comes out with these answers id be so greatful.

public class Foo {
int n;
static int m = 0;

public Foo(int n) {
this.n = 2 * n + m;
m++;
}

public void calc(int n) {
System.out.println(n);
System.out.println(this.n);
System.out.println(m);
this.n = n * this.n + n * m;
}


public static void main(String[] args) {
Foo n = new Foo(2);
Foo m = new Foo(3);

n.calc(m.n);
m.calc(n.n);
System.out.println("n's n is " + n.n);
System.out.println("m's n is " + m.n);
System.out.println("The value of m is " + Foo.m);
}
}


ANSWERS :

n's n is 42

m's n is 378

The value of m is 2


Help Please

Reply With Quote
  #2  
Old June 13th, 2005, 08:30 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
Complaint: use better variable names... especially not M and N as oject names if they're used inside the class itself as well! =)
In fact, that might mess things up... be careful.
The code is unbelievely hard to follow

Should member variable n be declared as public?

What values do you expect to see?
What are the actual System.out.print in the calc method displaying?
__________________
Daryl's Homepage | My Blogroll | My Profile | Firefox supporter!
DevArticles Forum Moderator

"The net is a waste of time, and that's exactly what's right about it." -- William Gibson

Reply With Quote
  #3  
Old November 30th, 2005, 03:23 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2005
Posts: 632 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 2 Days 4 h 45 m 4 sec
Reputation Power: 3
this.n always points to the instance variable. This piece of code repeatedly shadows it's own variables (shadowing is the official term ) which is not good practice!

Reply With Quote
  #4  
Old November 30th, 2005, 08:37 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
I quite like the answers in this thread...
It's a shame Jenkins never followed up on his post.
Comments on this post
Icon agrees: yes, although I suspect this is an exam question where code is obfuscated on purpose and he does not
have real interest in working with this program

Reply With Quote
  #5  
Old November 30th, 2005, 05:37 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
heh, funny you should say that...
I had that exact same though after reading the inital post earlier today.

Reply With Quote
  #6  
Old December 1st, 2005, 02:05 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2005
Posts: 632 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 2 Days 4 h 45 m 4 sec
Reputation Power: 3
Talking

Yeah I recently passed the Sun java programmer certificate and this code example looks very much like the questions on that exam.. Very tricky, far fetched, and generally not-good java programs but excellent exam questions..

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Help on a Programming Problem :)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway