My question is based on class relationships. I've written code for 2 classes, one of the "public int..." bits in 1 class has a variable which holds the cost of something. In the other class a variable holds the weight of something. I want to multiply the weight with the cost. Any help guys?

Recommended Answers

All 6 Replies

Ok sorry for the misleading title. ANY HELP GUYS?!

My question is based on class relationships. I've written code for 2 classes, one of the "public int..." bits in 1 class has a variable which holds the cost of something. In the other class a variable holds the weight of something. I want to multiply the weight with the cost. Any help guys?

Here is how you call variables from a seperate file and multiply them together.

double totalCost = class1name.varablename * class2name.variablename;

hope this helps. : )

A better title would have been "calling variables from separate files".

Unless Class1 and Class2 are nothing more than simple data structures, the convention is to write getters that return those values rather than declaring them public. i.e. class1thing.getValue()

haha, well i can't have my competition who needs the same help for their coursework benefit on my behalf can i now? Gotta have titles which are a bit misleading ;)

And appreciate it bro, still slight problem, the UML diagram shows that the variable in other file needs to be private only so I'm getting an error saying "var has private access only" :/ what now?

Create a function within the files that return the values of weight and cost then call the functions and multiply them together to get your result.

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.