Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getter setter

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
---------------
 
Henry Wong
author
Marshal
Pie
Posts: 21127
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-calmoney calls CalculatedMoneyAmount in calculate class
-CalculatedMoneyAmount call getmoney
-CalculatedMoneyAmount send the profit to money variable by setmoney


Basically, the calculate class instantiates a new customer object -- which is not the one that was used to call instantiate and call the calculate object.

So, you do set the customer object, but it's a different one. Furthermore, the calcmoney() method discards the calculate object, so that is also lost, upon return.

Henry
 
Campbell Ritchie
Sheriff
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Difficult to read because you haven't preserved indentation.

Confusing. You are adding 20% in the calculate class (that should start with a capital letter) but you are never passing the money back. You are creating a new customer (should read Customer) object, setting the 48000000 on that, then it isn't used because it is in a different class.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I dont think that the value of o.getmoney will ever be 40 000000.
Have you tried debuging your code in an IDE and stepping through each line to see where and how the values of customer
are being set?

This might help you to see where your values are getting set and is a valuable skill for a developer to learn.

Just out of interest, why is i a public field and not a vairable within the calculate method?

hope this helps
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-----------
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Create a calculate() constructor
pass the customer object to calculate() constructor and initialise the customer object
call the CalculatedMoneyAmount , which will now access the same customer object as initialsed in first step

or

Pass the customer object to CalculatedMoneyAmount () method and return the object back. // this more dumb way.



 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
///////
 
Campbell Ritchie
Sheriff
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:May you give me code about this?
Surely you can work that out for yourself?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*****
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:May you give me code about this?

No: http://faq.javaranch.com/java/NotACodeMill, http://faq.javaranch.com/java/DoYourOwnHomework
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
---------
 
salvin francis
Bartender
Posts: 1274
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:This is clear question:

I have a method in a class ,(customer class)

I want to access it,This method can not be static.I donot want use it as static

I want to know other than I create new instance ,Is there away to access it?

Balu Sadhasivam said:


Create a calculate() constructor
pass the customer object to calculate() constructor and initialise the customer object
call the CalculatedMoneyAmount , which will now access the same customer object as initialsed in first step


I want to know how is it possible?

Please answer me directly!



No, i dont think its possible


If a method is a part of the class (and its not static),
You would require an object to invoke it.


Question: Why do you want such approach ?

I can see directly that your code would not require any such special logic to run,




 
salvin francis
Bartender
Posts: 1274
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



here is my approach

Of course you have to change it a bit to make it work for your senario,

but here is my approach:

since calculation is an operation, i used a method instead of a class.

You can still use a calculation class that does the work, but i prefer this approach.


if you still want external class:

 
Henry Wong
author
Marshal
Pie
Posts: 21127
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:
Balu Sadhasivam said:

Create a calculate() constructor
pass the customer object to calculate() constructor and initialise the customer object
call the CalculatedMoneyAmount , which will now access the same customer object as initialsed in first step


I want to know how is it possible?

Please answer me directly!



Of course, it is possible -- in fact, Balu described it so well, that it is basically pseudo code !!

The issue here (and why no one is giving you exact code), is because you won't learn if we do. What was described is so clear, that you shouldn't have any issues. It is also releated to something basic in Java that you absolutely need to understand it.... Anyway, I suggest that you go back an review the chapter on methods, particularly parameter passing.

Henry
 
salvin francis
Bartender
Posts: 1274
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry, i hope i didnt show him too much
i didnt add the logic for calculations.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic