I have the first part done I just need help with part B. I also need comments pu
ID: 3634455 • Letter: I
Question
I have the first part done I just need help with part B. I also need comments put in the code. I asked this question yesterday and the code had some mistakes. To be clear for part b the user needs to be prompted for an invoice number and a sale amount. The program won't continue until the invoice number and sale amount are within the required values. Thank You I'll add what I have for part apublic 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);
}
a.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. Save the file as Purchase. java
b.Create an application that declares a Purchase object and prompts the user for purchase details. When you prompt for an invoice number, do not let the user proceed until a number between 1,000 and 8,000 has been entered. When you prompt for a sale amount, do not proceed until the user has entered a non-negative value. After a valid Purchase object has been created, display the object’s invoice number, sale amount, and sales tax. Save the file as CreatePurchase.java
Explanation / Answer
part a 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: 1 2 3 public int getInvoice(){ return invoice; } Now a different class can call the getInvoice() method and retrieve the value stored in the invoice variable The getter methods would go in the Purchase class directly above or below their corresponding setter method as below: Java Code: 1 2 3 4 5 6 7 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.