• Post Reply Bookmark Topic Watch Topic
  • New Topic

reading variables class to class  RSS feed

 
paul spriesterbach
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

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




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

 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Why have you got all static methods in the Mail class? Why is the letter marked static?
 
paul spriesterbach
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
paul spriesterbach
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
paul spriesterbach
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deitel? Early objects edition or late objects edition? I would have thought neither version of Deitel would have a static field like that.
 
paul spriesterbach
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
paul spriesterbach
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?



 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
paul spriesterbach
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
paul spriesterbach
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
paul spriesterbach
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great, I'm glad I could help. I look forward to seeing the completed work.
 
paul spriesterbach
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!