Results 1 to 8 of 8
- 06-10-2011, 08:14 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 12
- Rep Power
- 0
Java Beginner needs help with creating a Class
I am working on the same problem that flpanthers1 posted back on 6/6. Create a class named Purchase. Each Purchase contains an invoice number, amount of sale, and amount of sales tax. Include set methods for the invoice number and sale amount. Within the set () method for the sale amount, calculate the sales tax as 5% of the sale amount. Also include a display method that displays a purchase's details.
Here is what I have so far. It compiles but I am not sure if I set it up right. Any suggestions/help would be greatly appreciated!!
public class Purchase
{
private int invoice;
private double sale;
private double salesTax;
public void setInvoice(int invoiceNumber)
{
invoice = invoiceNumber;
}
public void setSale (double saleAmount)
{
double sale = saleAmount;
double salesTax = (saleAmount * .05);
}
public void DisplaySale ()
{
System.out.println("Invoice # >" + invoice + "Sale Amount = " + sale
+ "Sales Tax = " + salesTax);
}
}
- 06-10-2011, 08:45 PM #2
It looks good to me, only one issue. You are re-declaring the variables sale and salesTax locally in the setSale method. Since they are already defined globally in the class, you don't need the "double" in front of them.
- 06-10-2011, 08:48 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 12
- Rep Power
- 0
Thanks for the reply and your help. Now I'm moving on to creating an application that declares a Purchase object. Wish me luck!!! ........
- 06-10-2011, 08:50 PM #4
Member
- Join Date
- Jun 2011
- Location
- San Diego, CA
- Posts
- 24
- Rep Power
- 0
It is looking good so far. You will also want to create getter methods for those instance variable, i.e. getInvoice(). You will need these if you want other classes to be able to access the values stored in the the instance variables to use them since the variables are private. A simple example of a getter method is written like:
Now a different class can call the getInvoice() method and retrieve the value stored in the invoice variable.Java Code:public int getInvoice(){ return invoice; }
- 06-10-2011, 08:54 PM #5
Member
- Join Date
- Jun 2011
- Posts
- 12
- Rep Power
- 0
I'm not sure where to put the getter methods. I do have to write a second part to this problem. I have to create an application that declares a Purchase object and prompts user for an invoice number. I'm assuming that the getter methods would help with that part of the program???.
- 06-10-2011, 09:00 PM #6
Member
- Join Date
- Jun 2011
- Location
- San Diego, CA
- Posts
- 24
- Rep Power
- 0
The getter methods would go in the Purchase class directly above or below their corresponding setter method as below:
Since you are using the DisplaySale () method to print the values, the getters may not be used for this specific part of your assignment but it is good programming practice to have both getter and setter methods for private instance variables in case you create a class later that needs to retrieve the values of these variables.Java Code:public int getInvoice(){ return invoice; } public void setInvoice(int invoiceNumber) { invoice = invoiceNumber; }
- 06-10-2011, 09:05 PM #7
Member
- Join Date
- Jun 2011
- Posts
- 12
- Rep Power
- 0
Thanks for your help!! I added the Display method since it was asked to include it as part of the assignment that displays a purchase's details. I'm guessing I don't need to add the getters to this assignment, but should for future programs?
- 06-10-2011, 09:55 PM #8
Member
- Join Date
- Jun 2011
- Location
- San Diego, CA
- Posts
- 24
- Rep Power
- 0
It doesn't look like the current assignment will use the getters but it wouldn't hurt anything to add them in case your next assignment builds on this one.
And yes for future programs you should create both a getter and a setter method for each private instance variable. They allow you to restrict access to instance variables as well as allowing you to validate values that the variables are set to. You don't want for example somebody else' class setting your "dogWeight" variable to a negative number. That might break your code and doesn't make sense for a dog's weight to be negative.
Similar Threads
-
Help with creating new class as a subclass in Java
By Monkeeboy in forum New To JavaReplies: 3Last Post: 01-22-2011, 03:06 PM -
creating class
By J7571 in forum New To JavaReplies: 4Last Post: 11-18-2010, 02:55 PM -
Help with creating a class
By cdawg_2010 in forum New To JavaReplies: 22Last Post: 11-03-2010, 05:34 AM -
Beginner; Create a class to store info and constructor to initialize
By badness in forum New To JavaReplies: 16Last Post: 05-08-2008, 09:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks