i write code with class f that
have constructor :

public f(int number){
this.number=number;
}

i want to write method that get one f then add it with f which the method was called on it

public void add(f number)
{
//the code
}

what is the code????

Recommended Answers

All 13 Replies

In your method you are passed an instance of f and you need to access that instance's value of number.
How you do that depends on how the class f was written. If its number field is public then you can refer to it like this

    public void someMethod(f someF) {
       System.out.println("the other number is " + someF.number);
    }

but if number is private and you have a getNumber method (which is considered the better way to do it), it's like this

     public void someMethod(f someF) {
        System.out.println("the other number is " + someF.getNumber());
     }

obce you have the other f's number you can simply add that to your own local number

in this method i do not want to print anything.
i want two add two number

parsa: you don't have two numbers there. you have one f, and God knows what else. but f is not a number.
JamesCherrill has shown you how to get the number from that f, I'm pretty sure you'll manage to find how to add two numbers together?
if it's a method of the f class (badly chosen name, btw, not only because it breaks naming conventions) just add it to the number of that class

one number is number the other on is the f that this method is called on

in this method i do not want to print anything.
i want two add two number

Yes, I know that. I'm not going to do your homework by giving you the complete answer! I was showing you how to access the second number so you can add it.

ps: f is not a number. It's a class that has a number as (one of) its instance variables.

i write this

number+=number.getnumber(number);

but it don not work

public int getNumber(int number){
        return this.number;
    }

So you have 2 classes, the main class containing the main method, and a class f? You also have 2 instances of class f, both with numbers passed into them when they were instantiated? What I think you want to do is get the numbers from both instances and add them together?

Some seudo code for you.

all inside the main class

instance f1 called with the number 8
instance f2 called with the number 4

numf1 equals f1.getNumber
numf2 equals f2.getNumber
sum equals numf1 plus numf2

Hopefully you should be able to figure it out with that. Use the getNumber method you defined in class f.

i write this

number+=number.getnumber(number);
but it don not work

of course this doesn't work.
there are at least two reasons why that code doesn't make sense.

  1. you have two variables with an identical name, which means the one you try to use is hidden by the other one. you are trying to add a number to an instance of f, which is not possible
  2. you are not supposed to pass a parameter to a get-method, and surely not the instance you're calling the method on.

so, what you should have is something like:

this.number += instanceOfF.getNumber();

in main:

f b=new f(3);
f d=new f(4);
b.add(d);

the b will be 7 and d will be 4
but i want b will be 3 and d will be 7
how can i do that??

by carefully reading your code and figuring out where you went wrong.
It is rather obvious.
Using proper class and variable naming conventions makes it easier to read btw.
So descriptive names, class names starting with an uppercase letter, variable names with a lowercase letter.

What's wrong with d.add(b); ?

But be clear: b is NOT 3, it's not even a nunber. It's an instance of the class f that contains an int whose value is 3. This is an essential distinction.

Please keep in mind you then have to code everything you want to do with the final value in the f class. If you code it the way I showed you then it gives you access to it whenever you need to. It seems like it will work best for what you need to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.