aspose file tools*
The moose likes Beginning Java and the fly likes Convert application to object oriented application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convert application to object oriented application" Watch "Convert application to object oriented application" New topic
Author

Convert application to object oriented application

Gary Taylor
Greenhorn

Joined: May 23, 2004
Posts: 2
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.


import javax.swing.*;
public class EnhancedInvoiceApp
{
public static void main(String[] args)
{
try
{
String choice = "";
while (!(choice.equalsIgnoreCase("x"))) // begin while loop
{
String inputString = JOptionPane.showInputDialog(
"Enter order total: ");
double orderTotal = Double.parseDouble(inputString);
double discountAmount = 0;
if (orderTotal >= 100)
discountAmount = orderTotal * .2;
else
discountAmount = orderTotal * .1;
double invoiceTotal = orderTotal - discountAmount;
String message = "Order total: " + orderTotal + "\n"
+ "Discount amount: " + discountAmount + "\n"
+ "Invoice total: " + invoiceTotal + "\n\n"
+ "To continue, press Enter.\n"
+ "To exit, enter 'x': ";
choice = JOptionPane.showInputDialog(message);
} // end while loop
}
catch(NullPointerException e)
{
System.exit(0);
}
}
}

----------------------------------
public class Invoice
{
private String message;
public Invoice(double orderTotal)
{
double discountAmount;
double invoiceTotal;
if (orderTotal >= 100)
discountAmount = orderTotal * .2;
else
discountAmount = orderTotal * .1;
invoiceTotal = orderTotal - discountAmount;
}

public String toString()
{
String message = "Order total: " + orderTotal + "\n"
+ "Discount amount: " + discountAmount + "\n"
+ "Invoice total: " + invoiceTotal + "\n\n"
+ "To continue, press Enter.\n"
+ "To exit, enter 'x': ";
return message;
}
}
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
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 ]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923


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.


http://home.arcor.de/hirnstrom/bewerbung
Gary Taylor
Greenhorn

Joined: May 23, 2004
Posts: 2
Thanks Craig and Stefan. This was very useful information and your explanations helped me to understand.

Gary
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert application to object oriented application