Hi, I'm doing self-study from Murach's Beginning Java 2. My exercise is to convert an application to object-oriented application. "Start a new class named Invoice and save it to .... Then, write the code for this class so it provides all of the data operations related to an Invoice object. Its constructor should require the order total as its only paranters, and it should initialize instance variables for order totals, discount amount, and invoice total. One of it's methods shold be the toString method, which returns a string that contains all of the data for an invoice. As you work you may want to move code from the InvoiceApp class to the Invoice class. Modify the code in the InvoiceApp class so it creates and uses Invoice objects."
I'm really stuck on the toString method. In my Invoice.java I get errors about being able to resolve orderTotal, discountAmount, and invoiceTotal. Not to say that the rest of Invoice.java is close to being right, but that's where I'm getting the errors.
FWIW this is in Chapter 4 and a few of the things that the chapter is trying to teach are : creating objects, constructors, methods, calling methods, static fields and methods.
When you make/start a new class always start with thinking about what you want it to do. What information do you want it to keep (this is the state). This translates to the instance variables (which have class scope, this will mean more to you in time). The assignment tells you up front what fields (instance variables) to declare in Invoice: and it should initialize instance variables for order totals, discount amount, and invoice total. So we can get started by declaring thes variables like so:
These three instance variables have class scope, that is, every instance of Invoice
will always know and be able to tell you the value of each of these three variables (once they have been initialized, values given to them).
So let's try this implementation of Invoice and see what you get:
Note that I removed the message declaration
This is because it isn't needed. It is declared and instantiated inside the toString method (and is therefore a local variable) and is returned, ie, sent back as the return value of the method. It's value is not required by the class (as an instance variable, ie with class scope).
Scope has to do with visibility, who can see it. Anything declared inside curley braces is private to (the scope of) the curley braces; no one outside the curley braces can see it. So a variable declared inside the class (curley braces) can be seen by anyone inside the class including anyone inside methods. This is class scope. A (local) variable declared inside a method cannot be seen outside the method.
So there's some subtlty and strategy in where we declare things. [ May 23, 2004: Message edited by: Craig Wood ]
I don't think a 'toString ()' - method should tell someone, to hit 'Enter' or 'x'. This will reduce the possibilities of it's (meaningfull) usage drastically. It makes too much assumptions of the context, where it is called.
It would be the job of an environment, in which an invoice is used.