CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: getter setter

  1. #1
    Join Date
    Aug 2005
    Posts
    98

    getter setter

    class calculate {
    private int profitRate=20;
    public long i;
    customer obj=new customer("Mr.Stevens",189745329,40000000);
    public void CalculatedMoneyAmount(){
    i=( obj.getmoney() +( obj.getmoney()*profitRate)/100);
    obj.setmonney(i);
    }



    }


    class customer{
    private String customerName;
    private int accountNumber;
    private long money;
    public customer(String name,int number,long amount)
    {
    this.customerName=name;
    this.accountNumber=number;
    this.money=amount ;
    }


    public customer()
    {

    }
    public long getmoney(){
    return money;
    }

    public void setmonney(long i){

    this.money=i;
    }

    public void calmoney() {
    calculate obj=new calculate();
    obj.CalculatedMoneyAmount();


    }

    }



    public class Main {

    /** Creates a new instance of Main */

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args){

    customer o=new customer("Mr.Stevens",189745329,40000000);
    o.calmoney();
    System.out.println (o.getmoney());

    }

    }

    It must retuern and show to output 48000000

    But only shows 40 000000

    -In the main mehtod I careate an instance of customer class
    -After that I call calmoney
    -calmoney calls CalculatedMoneyAmount in calculate class
    -CalculatedMoneyAmount call getmoney
    -CalculatedMoneyAmount send the profit to money variable by setmoney
    -Now this line System.out.println (o.getmoney()); must print 48 000 000

    But I see 40 000 000 !!

  2. #2
    Join Date
    Apr 2007
    Posts
    442

    Re: getter setter

    Your calculate class creates a customer of its own. It does not matter, if you create another customer, with identical values in your main method. That IS NOT the same customer instance. You should have the calculations as a method in your calculate class, where the customer is given as a parameter. That way the right customers money would change.

  3. #3
    Join Date
    Aug 2005
    Posts
    98

    Re: getter setter

    I know ,But I want to use getter setter to learn about this matter .




    class calculate {
    private int profitRate=20;
    public long i,j;

    public void CalculatedMoneyAmount(customer obj){
    i=( obj.getmoney() +( obj.getmoney())/100);

    System.out.println( "i = " + i );

    }


    class customer {
    private String customerName;
    private int accountNumber;
    private long money;
    public customer(String name,int number,long amount)
    {
    this.customerName=name;
    this.accountNumber=number;
    this.money=amount ;
    }


    public customer()
    {

    }
    public long getmoney(){
    return money;
    }

    public void setmoney(long i){

    this.money=i;
    }


    }
    }

    public class Main {

    /** Creates a new instance of Main */

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args){

    customer o=new customer("Mr.Stevens",189745329,40000000);
    calculate l=new calculate();


    l.CalculatedMoneyAmount( o );
    o.setmoney(l.i);

    System.out.println (o.getmoney());


    }

    }


    I have problem in these lines :
    l.CalculatedMoneyAmount( o );
    o.setmoney(l.i);

    Please answer to this question

    - How to pass an object as a parameter in a method?

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: getter setter

    It would help if you used code tags when posting code, which after 79 posts you should be aware of. It would also help if you used the Java capitalisation conventions.

    I have problem in these lines :
    l.CalculatedMoneyAmount( o );
    o.setmoney(l.i);
    And it would help if you took the trouble to explain your problem in detail.

    What is obvious is the customer class doesn't have a field 'i' so trying to use l.i isn't going to work. What were you trying to do?

  5. #5
    Join Date
    Aug 2005
    Posts
    98

    Re: getter setter

    What is obvious is the customer class doesn't have a field 'i' so trying to use l.i isn't going to work. What were you trying to do?


    Sir , What is obvious is you answer carelessly .

    Code:
    class calculate {
    private  int profitRate=20;
    public long i,j;
    
    public void CalculatedMoneyAmount(customer obj){ 
    i=( obj.getmoney() +( obj.getmoney())/100);
     
    System.out.println( "i = " + i );
    
      }
    Good luck friend!

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: getter setter

    Sir , What is obvious is you answer carelessly
    Apologies, your code is somewhat confusing to read.

    So what exactly is your problem? Please state what you are trying to achieve, what is going wrong and give complete copies of any compiler/runtime error messages.

  7. #7
    Join Date
    Aug 2005
    Posts
    98

    Re: getter setter

    No problem Sir, The problem of that has been solved

    But Thank you very much indeed for your help,

    The most important thing is you decide to help others

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured