Results 1 to 8 of 8
  1. #1
    BeginnerJava is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default 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);
    }
    }

  2. #2
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default

    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.

  3. #3
    BeginnerJava is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default

    Thanks for the reply and your help. Now I'm moving on to creating an application that declares a Purchase object. Wish me luck!!! ........

  4. #4
    JDScoot is offline Member
    Join Date
    Jun 2011
    Location
    San Diego, CA
    Posts
    24
    Rep Power
    0

    Default

    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:
    Java Code:
    public int getInvoice(){
        return invoice;
    }
    Now a different class can call the getInvoice() method and retrieve the value stored in the invoice variable.

  5. #5
    BeginnerJava is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default

    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???.

  6. #6
    JDScoot is offline Member
    Join Date
    Jun 2011
    Location
    San Diego, CA
    Posts
    24
    Rep Power
    0

    Default

    The getter methods would go in the Purchase class directly above or below their corresponding setter method as below:
    Java Code:
    public int getInvoice(){
        return invoice;
    }
    public void setInvoice(int invoiceNumber)
    {
        invoice = invoiceNumber;
    }
    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.

  7. #7
    BeginnerJava is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default

    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?

  8. #8
    JDScoot is offline Member
    Join Date
    Jun 2011
    Location
    San Diego, CA
    Posts
    24
    Rep Power
    0

    Default

    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

  1. Help with creating new class as a subclass in Java
    By Monkeeboy in forum New To Java
    Replies: 3
    Last Post: 01-22-2011, 04:06 PM
  2. creating class
    By J7571 in forum New To Java
    Replies: 4
    Last Post: 11-18-2010, 03:55 PM
  3. Help with creating a class
    By cdawg_2010 in forum New To Java
    Replies: 22
    Last Post: 11-03-2010, 06:34 AM
  4. Replies: 16
    Last Post: 05-08-2008, 10:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •