This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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.
Joined: Aug 05, 2005
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
Joined: Nov 24, 2011
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,
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.
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 ofbalance, which is not static.
You will, of course, use balance, not startBalance, throughout the rest of the class.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com