Hi I just want to be able to read type and weight in the Letter class I created (I created read out messages to check in the Letter class). I am able to read it with in the Mail class. Any help and/or advise with this simple problem is greatly appreciated!
There are several ways you can pass the type and weight values to your Letter class.
One way is to pass them in the constructor. Where you call new Letter(); you can pass the values here as parameters. You will then have to declare a custom constructor in the Letter class.
Another way is to pass the values into the LetterPrice() method as shown below:
You will have to modify the LetterPrice() method definition in the Letter class to accept these 2 parameters and then use them inside the method.
If the letter is marked static, then there is only one letter at any one time. If it is not static, then each Mail object has its own letter.
We are using the Deitel 9th edition one for the class im in. Is there any learning material you would recommend that I can use to supplement what I am using in the class, I dont want to just get a good grade I really want to understand everything.
Campbell Ritchie wrote:Deitel? Early objects edition or late objects edition? I would have thought neither version of Deitel would have a static field like that.
title is Java how to program.... ya I have changed my code considerably, ill post it a little later
Ok this is what I have so far. Im still trying to figure out the best way to send these values over to the LetterPrice class and use them in the two method 'check type' and 'calculate price' place holders I have. Any help is much appreciated, thank you!
It doesnt like my
LetterPrice myLetterPrice = new LetterPrice(theType, theWeight);
If you are going to pass the values into the constructor like you are trying on line 16, you would need to move line 16 below line 28 so both variables have been declared and initialized with the values from the user input.
You are also calling set methods for these values on line 31 which is redundant. You don't need to pass the values in the custructor as well as call the set methods. Here you can do one or the other to get the values into the LetterPrice Object.
Either you can move line 16 to line 29 and get rid of lines 31 and 32 OR on line 16 you can just call the default constructor passing no parameters "new LetterPrice()" and then use the set methods on lines 31 and 32.
Scott Winterbourne wrote:The "theType" and "theWeight" variables you used in the main() method have not been declared or initialized with values. That should work now? Edit: ok I removed redundant code
You will need to declare them as data types (i.e. String, float, etc.) and then assign values to them before you can pass them into the constructor.
ok I reordered the main, so they are declared first
Scott Winterbourne wrote:If you are going to pass the values into the constructor like you are trying on line 16, you would need to move line 16 below line 28 so both variables have been declared and initialized with the values from the user input.
You are also calling set methods for these values on line 31 which is redundant. You don't need to pass the values in the custructor as well as call the set methods. Here you can do one or the other to get the values into the LetterPrice Object.
Either you can move line 16 to line 29 and get rid of lines 31 and 32 OR on line 16 you can just call the default constructor passing no parameters "new LetterPrice()" and then use the set methods on lines 31 and 32.
You will want to save a reference to the new LetterPrice object in a reference variable so you can continue to use the object.
on line 30 you can use
like you had before. This way you can call myLetterPrice.getType() or myLetterPrice.getWeight() to check that the object was created and the type and weight values have actually been set in it. Stick these method calls inside of a "System.out.println();" to print the values to the consol for varification.
You can then add more methods to the LetterPrice class and call them using the dot (.) oporator as well.
Scott Winterbourne wrote:You will want to save a reference to the new LetterPrice object in a reference variable so you can continue to use the object.
on line 30 you can use
like you had before. This way you can call myLetterPrice.getType() or myLetterPrice.getWeight() to check that the object was created and the type and weight values have actually been set in it. Stick these method calls inside of a "System.out.println();" to print the values to the consol for varification.
You can then add more methods to the LetterPrice class and call them using the dot (.) oporator as well.
cool, yea i added the myLetterPrice back in. Thank you, Im starting to understand it a bit. Im going to try to add the method into my LetterPrice class and post back here my completed work or if I get stuck. Thanks!
Scott Winterbourne wrote:Great, I'm glad I could help. I look forward to seeing the completed work.
Awesome it works great, I still need to format it in the way my teacher wants it thats all. I enter in if its a envelope or a letter and the weight and it spits out the postage cost. What are the different ways I could make this code more efficient?
Post by:autobot
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
a bit of art, as a gift, that will fit in a stocking