Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloading Constructors.

 
Grant Gibson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to create multiple constructors, one that is a default value, of 100, and the other, if the user chooses to do so, has a value of whatever they decide. I'm confused on how to make the constructors, and allow them to be edited by the methods below. However, the constructors are my main priority. The methods I'll about when I correct my constructor situation.

 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you initialising static variables in a constructor ? You do realise the value will be overwritten for every instance of your class whenever you create a new instance.
 
Grant Gibson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:Why are you initialising static variables in a constructor ? You do realise the value will be overwritten for every instance of your class whenever you create a new instance.


I didn't realize that. Our instructor didn't go over constructors very well, so I'm trying to learn a lot of it on my own. Even if I take static out, I'm still registering errors in eclipse.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post the new code and the errors (cut and paste them - don't try to paraphrase) and we'll see what we can do to help.
 
Grant Gibson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:Post the new code and the errors (cut and paste them - don't try to paraphrase) and we'll see what we can do to help.




Overall, the code just doesn't seem like it's going to do what I want it to do, as of right now. Default = 100, anything else equals the users input.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give an example of what it should be doing and what it is actually doing. That code looks fine to me.

Edit - having said that, your buypoints method looks wrong. Shouldn't it be adding amt to startbalance - you don't have a balance variable anymore ?
 
Grant Gibson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:Can you give an example of what it should be doing and what it is actually doing. That code looks fine to me.

Edit - having said that, your buypoints method looks wrong. Shouldn't it be adding amt to startbalance ?


Now that I have the class created, I want to use it in the main class. So lets say I created a new MealCard.



Shouldn't I be able to out.print a line that has the meal card amount? Which, I think it should print 100, seeing as that is the default value.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, as previously asked, please copy and past the real error message, don't paraphrase, because you might be pointing us to the wrong position.

Second, You have an error on line 25 of the code you posted:

The variable balance does not exist.

Third, you might try saving the .java file, deleting any .class file, and recompiling just in case you aren't compiling current code
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Grant Gibson wrote:
Shouldn't I be able to out.print a line that has the meal card amount? Which, I think it should print 100, seeing as that is the default value.


Only if you have a means to access the amount. For example, a getBalance() method which returns the value you want to print, or a toString() method which converts the contents (balance and other information if present) into a displayable String.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would need to add a method to your Mealcard class to return the current balance. Something like

You could then call this from your main class with

 
Grant Gibson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:First, as previously asked, please copy and past the real error message, don't paraphrase, because you might be pointing us to the wrong position.

Second, You have an error on line 25 of the code you posted:

The variable balance does not exist.

Third, you might try saving the .java file, deleting any .class file, and recompiling just in case you aren't compiling current code


Alright. I changed balance to startBalance.

I'm using eclipse to compile and run, so I should be running the current code (hopefully, ha).

And the "error" I'm getting is not really an "error", it's just out.printing wrong. But here it is,

"MealCard@199a0c7c"

While I'm using this as the main class.



I just want to make sure the MealCard class is actually defining the value at 100, or whatever value is inserted. Which it is not doing.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to override toString() in your MealCard class. What you're seeing is the default implementation provided by Object - the class name followed by @ and the hexadecimal hash code.
 
Grant Gibson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow. I feel dumb =.=. Now I should be able to start working on my main class with no problems. -fingers crossed-

Thank you for your patience by the way, I haven't really had the chance to sit and ask questions with someone fluent in Java.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You need an instance field with the balance in; this will be different for each instance. At least you have the value in integer format; that means it will be limited in value (if that means pence, then you need to keep the balance less than slightly over £20000000 [or $20000000, etc.]).
If you want a static field as a constant to maintain a default start balance, that is a good idea. It should really be final so it never changes.
I usually warn against no-arguments constructors, but in the case where you want a default initial value (100), that is all right. You want (startBalance) to pass to the other constructor. In the other constructor, you want to initialise the value of balance, which is not static.
You will, of course, use balance, not startBalance, throughout the rest of the class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic