Reputation: 1089
I am taking java class and I had to do an assignment which reads as follows:
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 7.5% (using a static filed in the Purchase class) of the sale amount. Also include a display method that displays a purchase's details in a well formatted output display. Save the file as Purchase.java. Compile and run your programs until it works and the output looks nice. Add the necessary documentation as described in Course Documents, and then attach your .java files to this assignment
My solution was as follows:
import java.util.*;
public class Purchase {
//Properties of Purchase class - static
private static int invoiceNumber;
private static double salesAmount;
private static double salesTax;
//setter for invoiceNumber
public static void setInvoiceNum(int invNo){
invoiceNumber = invNo;
}
//setter for salesAmount
public static void setSalesAmount(double salesAmnt){
salesAmount = salesAmnt;
salesTax = 0.075*salesAmnt;
}
//public static method to display purchase info
public static void displaySalesInfo(){
System.out.println("Invoice Number: " + Purchase.invoiceNumber);
System.out.println("Sales Amount: " + Purchase.salesAmount);
System.out.println("Sales Tax: " + Purchase.salesTax);
}
//main method that makes use of the static properties and display method
public static void main (String[] args) {
//ask user to input purchase details
Scanner scan = new Scanner(System.in);
System.out.println("Enter your invoice Number:" );
int inv = scan.nextInt();
System.out.println("Enter your Sales Amount:");
double sales = scan.nextDouble();
// send the user submitted purchase details to the setter methods and call display method
setInvoiceNum(inv);
setSalesAmount(sales);
displaySalesInfo();
}
}
And here is my teacher's comments: "For this assignment you were to provide a sales tax of 7.5% using a static field in the Purchase class. In the code you submitted you used a numeric literal, which most will consider a very bad programming practice. You did set the static variable salesTax, however the value you give it is based on the instance method parameter, which is a logic error. Only the tax rate was to be static, all other fields should not be, otherwise each purchase would be the same regardless of what was purchased. The assignment code submitted indicated you do not understand the concept of static fields."
I just don't understand?? He says I am not understanding static fields.. Am I that ignorant? This is simply embarrassing..Please shed some light.
Upvotes: 0
Views: 5774
Reputation: 127
Static variables are in memory only one time regardless of how many sales objects or transactions. Final means the tax rate is fixed at least till they change the law then You have to look for a variable named taxrate and change it to the new value of 8 percent. oh I am sorry you don't have a taxrate variable to define the constant so you will have to look at thousands of lines of code for 7.5 and change it to 8.0 hoping you didn't give the sales department commission from the current 7.5 commission to 8.another question could you have more than one invoice? could two sales happen at once? My point is taxrate should be the only static variable because it's fixed. Good job though but try to see the big picture.
Upvotes: 1
Reputation: 2584
Static fields are always class variables, which means that every instance of this class shares the same reference on this class' static fields.
In your example, it doesn't matter much BUT in the real world, your code would be useless. I think what you should have done is :
1 - define the salesTaxRate as a static field like Juned wrote
2 - define your other fields not static
3 - in your main, it would have been better to see somewhere Purchase myPur = new Purchase();
In other words (sorry for a possible mistake, i wrote the code directly here ^^) :
import java.util.*;
public class Purchase {
//Properties of Purchase class - static
private static double taxRate = 0.075; // Shared by all instances
// Members that are instance-visible
private int invoiceNumber;
private double salesAmount;
private double salesTax;
//setter for invoiceNumber, not static as it works on a non-static field
public void setInvoiceNum(int invNo){
invoiceNumber = invNo;
}
//setter for salesAmount, not static as it works on non-static fields
public void setSalesAmount(double salesAmnt){
salesAmount = salesAmnt;
salesTax = Purchase.taxRate*salesAmnt; // Note the Purchase.taxRate notation
}
//public static method to display purchase info
// I keep it static just as an example : here you HAVE to give the purchase to
// display BECAUSE the method is static
public static void displaySalesInfo(Purchase pur){
System.out.println("Invoice Number: " + pur.invoiceNumber);
System.out.println("Sales Amount: " + pur.salesAmount);
System.out.println("Sales Tax: " + pur.salesTax);
}
//main method that makes use of the static properties and display method
public static void main (String[] args) {
//ask user to input purchase details
Scanner scan = new Scanner(System.in);
System.out.println("Enter your invoice Number:" );
int inv = scan.nextInt();
System.out.println("Enter your Sales Amount:");
double sales = scan.nextDouble();
// send the user submitted purchase details to the setter methods and call display method
Purchase myPurchase = new Purchase();
myPurchase.setInvoiceNum(inv);
myPurchase.setSalesAmount(sales);
displaySalesInfo(myPurchase);
}
}
Upvotes: 2
Reputation: 4671
what you should have done is that make the salesTax field static as it would be same for all the purhase made.
So,
private static final double SALES_TAX= 0.075;
\\it is convention that final fields are given capitalized identifiers
the way you assigned the value to salesTax is wrong. it should be something like
private double salesTaxAmt;
\\ a variable to hold the sales tax amount specific to the purchase
and set it in the method setSalesAmount
as
salesTaxAmt = salesAmt * salesTax;
Since a static
variable is common to all objects of a class it should be set from a
static context rather from an instance context (code specific to each instance of the class). That is
private int i;
private static int j;
here i
is an instance variable as it may and would have different value with different
instances of the class whereas j
is a staic field and its value is common to all the instance of the class. That is no matter what instance access j
its value will be same for all the instances.
If you change the value of a static field from a particular instance then it would reflect in all other instance of the class also.
Upvotes: 2
Reputation: 77226
static
members in Java are ones that apply to the entire class collectively, not to any particular instance of the class, so the only fields that you should be making static
are the ones that are universal; the sales-tax rate would be an appropriate choice, and as your professor notes, you shouldn't bury "magic numbers" like 0.075
in the middle of your code. Instead:
static double salesTaxRate = 0.075;
// maybe static getter and setter
All of the fields that you currently have set static
(invoice number and amounts) don't apply to every purchase, just to specific ones, so they should be instance variables (not static
).
(Finally, you don't want to use floating-point for money in real-world code because of rounding errors, but it's fine for a homework assignment.)
Upvotes: 1
Reputation: 5585
Static fields are shared by all instances of a class. By making the sale amount and invoice number static, you've essentially created a situation where there's only one purchase (since they all have the same invoice number and sale amount).
What your teacher is trying to get you to demonstrate is the proper use of a shared (or static) field -- the sales tax. In this case, that property really is shared by all purchases.
Only the sales tax rate should be static. In your main
method, in your loop, you should be creating new
purchase instances.
Since you're just learning, I'm not going to provide code here, but hopefully this will get you pointed in the right direction.
Upvotes: 1
Reputation: 68715
If the salesTax amount is fixed and shared by all the instances then it is better to define and initialize it as a constant:
private static final double salesTax = 0.075;
Upvotes: 2