• Post Reply Bookmark Topic Watch Topic
  • New Topic

writing packages  RSS feed

 
Deyna Cegielski
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im a bit stuck! im trying to write a package that performs arithmetic on a weight object (e.g. in pounds and ounces). ive written a class file to create the weight object (this works) and im going to write a method to read the weight from a file into a string, and then convert the string representation of the weight into a necessary format to create my weight object. im stuck on the methods, i want an addweights method that adds 2 weights together and output the result, but im confused as to what the method declation will look like

something like:
public void addWeights("Weight object 1", "Weight object 2) <--what goes in there?? {
//implementation

please help!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Generally, you'll want a method to add a Weight to "this" Weight and return a third Weight -- i.e.,

Weight w1 = new Weight(10);
Weight w2 = new Weight(20);
Weight w3 = w1.add(w2);

So that last method would be declared in the Weight class as

public Weight add(Weight w) { ... }
 
Deyna Cegielski
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right i think i get u and the follow code wrote seems to work:

public Weight addWeight(Weight w){
w.pounds = getPounds() + w.getPounds();
//same for ounces
return(w);
}

getpounds is just an accessor method cos the data members pounds and ounces are private.

the test line was
Weight w3 = w1.add(w2);
getpounds() gets the pounds value for w1 and w.getpounds() gets the pounds value for w2

any comments?
 
Simon J Birch
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

There may, or may not, be a problem here. It depends on the behaviour you want the Weight to demonstrate.

Using the code you have suggested above will actually change the value of the Weight object passed to addWeight, so

will result in w3 and w2 referring to the same object with a weight of 30.

If you want to add w2 to w1, so that w1 now has their combined weight, something like this might be what you want

This assumes you have a set method!

If you want to create a seperate Weight to hold the result, you could use something like


Is that any use?
 
Deyna Cegielski
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ye i did notice after doing a similar subtract weights method, the weight object passed to the add weights method was changed so the subtact method didnt work as intended. the last thing u suggested is probably best, but is it good coding to create another weight object from within the weight class, where the addWeight subtractWeight methods are
 
Simon Birch
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it depends how you want the object to perform. I think if I saw a method on an object called addWeight I would assume it added a value to the object it was related to, so I'd expect the following code to alter the value of w1, and leave w2 as it was

I don't think there is any problem using the other approach and generating a new Weight object, but you might want to think about what you call the method. Another alternative would be to have e.g. a Weights class that contains static methods to manipulate Weight objects. This would be a similar appraoch to that used in the java.util.Arrays object.
 
Deyna Cegielski
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
essentially i will be reading the 2 weights from a txt file/text files (easy peasy). then i want to add the 2 weights, subtact the two weights, and multiplay/divide one of the weights by a constant, obviously outputing the result after each method...so i dont really want to change w1 and w2 after they are set, so creating a new weight object to store the result is probably best wouldnt you say?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!