Forums Register Login

reading variables class to class

+Pie Number of slices to send: Send
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!

///////////////////////////////////////////////////////////////////////////////




/////////////////////////////////////////////////////////////////////////////////////////////////////////////

+Pie Number of slices to send: Send
Hi Paul, welcome to the Ranch!

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.
+Pie Number of slices to send: Send
Welcome again

Why have you got all static methods in the Mail class? Why is the letter marked static?
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:Welcome again

Why have you got all static methods in the Mail class? Why is the letter marked static?



Because I am a noob and I am trying to follow my text book examples to do this problem
+Pie Number of slices to send: Send
 

paul spriesterbach wrote:

Campbell Ritchie wrote:Welcome again

Why have you got all static methods in the Mail class? Why is the letter marked static?



Because I am a noob and I am trying to follow my text book examples to do this problem



Let me go back and redo the code after I read up a bit more. Ive figured some stuff out now
+Pie Number of slices to send: Send
Text book? Which one?

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.
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:Text book? Which one?

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.
+Pie Number of slices to send: Send
Deitel? Early objects edition or late objects edition? I would have thought neither version of Deitel would have a static field like that.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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);

any insight into why?



+Pie Number of slices to send: Send
The "theType" and "theWeight" variables you used in the main() method have not been declared or initialized with values.

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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

+Pie Number of slices to send: Send
 

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.



how does what I did one post up look now?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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!
+Pie Number of slices to send: Send
Great, I'm glad I could help. I look forward to seeing the completed work.
+Pie Number of slices to send: Send
 

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?



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
https://gardener-gift.com


reply
reply
This thread has been viewed 935 times.
Similar Threads
completely lost
Noob java assigment
Need help fixing error
JOption problems
Separating the Digits in an Integer
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 13:30:33.