Results 1 to 13 of 13
- 07-12-2011, 04:31 AM #1
Member
- Join Date
- May 2011
- Posts
- 46
- Rep Power
- 0
- 07-12-2011, 04:50 AM #2
When you create a class the standard practice is to make your instance variables private. Therefore you cannot access them outside the class directly. Instead you make use of accessor (get/set) methods.
Why?
I can think of two reasons and they both have to do with control over the variables.
You have a name. How would you like it if someone could just change your name without consulting you? Therefore you make the name private and if someone wanted to change your name, they would have to consult you first.
The other reason is that you can control what values are assigned to your variables. For example:
Java Code:class Foo { public int valueMustBePositive = 0; private int secondValueMustBePositive = 0; public void setValue(int val) throw InvalidArgumentException { if(val < 0) { throw new InvalidArgumentException("Value must be positive"); } else { secondValueMustBePositive = val; } } }
- 07-12-2011, 05:03 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
I think of classes as modelling things - so a Person class might be a programming model of a person.
It turns out to be helpful for this model to be based on behaviour rather than state. I am less concerned with whether a person's name is a String field, or is stored in a database or anything else. What is important is that I can set the name to what I want and get the name when I need it. The ability of a Person instance to set its name to something or the ability to return what its name is, is what I am calling behaviour. The methods that express this behaviour (setName(String) and getName()) are what I might make public while the details of what the methods do - to the state: the String or database etc - would be private.
These methods are often called setters and getters.
Having public setters/getters and private implementation that deals with state has a couple of advantages that occur to me off the top of my head:
(1) My Person class can be used by any class that wants to deals with named things, not just people. These other classes do not have to be concerned with the details of state like whether there is a String holding the name or some other mechanism.
(2) Since the classes using my Person class do not have access directly to the state I can do things like check input when those classes invoke a Person instance's behaviour. For instance if someone says "setName(null);" I can throw an IllegalArgumentException instead of allowing my person to become anonymous.
- 07-12-2011, 05:24 AM #4
Member
- Join Date
- May 2011
- Posts
- 46
- Rep Power
- 0
So, we use set and get to have better control of vars? Can't I just stick my int into a private variable and that would accomplish the same thing? Or does it also have to do with it takes more code to use a private variable than a set()?
- 07-12-2011, 05:47 AM #5
Member
- Join Date
- May 2011
- Posts
- 46
- Rep Power
- 0
My other problem is trying to rap my head around this assgnmnt: I have a InvoiceApp class that has the main and calls the methods and a validator class that checks input and a Invoice class that does the calculations. I have to use only the subtotal and customerType for parameters for the Invoice constructor along with using a get and set method, and the validator class was written and compiled already-working, and I have to use the InvoiceApp class to call the methods and do the clean-up work. my code so far is:
InvoiceApp Class:
Java Code:public class InvoiceApp { public static void main(String[] args) { String message; // display a welcome message System.out.println("Welcome to the Invoice Total Calculator"); System.out.println(); // print a blank line Scanner sc = new Scanner(System.in); String choice = "y"; while(choice.equalsIgnoreCase("y")) { // get user entries String customerType = Validator.getString(sc, "Enter customer type (r/c): "); double subtotal = Validator.getDouble(sc, "Enter subtotal: ", 0, 10000); } Invoice myInvoice=new Invoice(); myInvoice.getInvoice();//get formatted output in a string for printing } }
Java Code:public class Invoice{ double percent; double currency; String sSubtotal; String sCustomerType; public String setCustType(String customerType, double subtotal){ // calculate the discount amount and total double discountPercent = getDiscountPercent(subtotal, customerType); double discountAmount = subtotal * discountPercent; double total = subtotal - discountAmount; // format the values NumberFormat currency = NumberFormat.getCurrencyInstance(); NumberFormat percent = NumberFormat.getPercentInstance(); String sSubtotal = currency.format(subtotal); String sCustomerType = ""; if (customerType.equalsIgnoreCase("r")) sCustomerType = "Retail"; else if (customerType.equalsIgnoreCase("c")) sCustomerType = "College"; return customerType; } public String getInvoice(double discountPercent, double discountAmount, double total, double subtotal){ // output formatted invoice NumberFormat currency = NumberFormat.getCurrencyInstance(); NumberFormat percent = NumberFormat.getPercentInstance(); String sDiscountPercent = percent.format(discountPercent); String sDiscountAmount = currency.format(discountAmount); String sTotal = currency.format(total); // format and display the results String message = "Subtotal: " + sSubtotal + "\n" + "Customer type: " + sCustomerType + "\n" + "Discount percent: " + sDiscountPercent + "\n" + "Discount amount: " + sDiscountAmount + "\n" + "Total: " + sTotal + "\n"; System.out.println(); System.out.println(message); Scanner sc = new Scanner(System.in); System.out.print("Continue? (y/n): "); String choice = sc.next(); System.out.println(); return message; } static double getDiscountPercent(double subtotal, String type) { double discountPercent = 0.0; if (type.equalsIgnoreCase("r")) { if (subtotal >= 500) discountPercent = .2; else if (subtotal >= 250 && subtotal < 500) discountPercent =.15; else if (subtotal >= 100 && subtotal < 500) discountPercent =.1; else if (subtotal < 100) discountPercent =.0; } else if (type.equalsIgnoreCase("c")) { discountPercent = .2; } else discountPercent = .05; return discountPercent; } }
- 07-12-2011, 06:06 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
- 07-13-2011, 06:02 AM #7
Member
- Join Date
- May 2011
- Posts
- 46
- Rep Power
- 0
I have cleaned up my code and my output is not printing to the screen. I ran a dummy out.print in the getCustType and the values of sCustomerType and subtotal were right. But when I call the getInvoice method, the values do not follow. Can someone help me figure this out? do I need to use a setter method?
Here is my code:
Main method:
Java Code:public class InvoiceApp { public static void main(String[] args) { String message; double discountPercent=0; double discountAmount=0; double total=0; String customerType=""; double subtotal=0; String sCustomerType=""; // display a welcome message System.out.println("Welcome to the Invoice Total Calculator"); System.out.println(); // print a blank line //get customer type Invoice.getCustType(sCustomerType, subtotal); //get invoice output Invoice.getInvoice(sCustomerType, customerType, discountPercent, discountAmount, total, subtotal);//get formatted output in a string for printing } }
Java Code:public static String getCustType(String sCustomerType, double subtotal){ Scanner sc = new Scanner(System.in); String choice = "y"; while(choice.equalsIgnoreCase("y")) { // get user entries String customerType = Validator.getString(sc, "Enter customer type (r/c): "); subtotal = Validator.getDouble(sc, "Enter subtotal: ", 0, 10000); if (customerType.equalsIgnoreCase("r")){ sCustomerType = "Retail"; }else if (customerType.equalsIgnoreCase("c")){ sCustomerType = "College"; } System.out.print("Continue? (y/n): "); choice = sc.next(); System.out.println(); } return sCustomerType; } //output formatted invoice public static String getInvoice(String sCustomerType, String customerType, double discountPercent, double discountAmount, double total, double subtotal){ // calculate the discount amount and total discountPercent = getDiscountPercent(subtotal, customerType); discountAmount = subtotal * discountPercent; total = subtotal - discountAmount; System.out.println("D%: "+discountPercent+"D$: "+discountAmount+"ST: "+subtotal+"T: "+total); // format the values NumberFormat currency = NumberFormat.getCurrencyInstance(); NumberFormat percent = NumberFormat.getPercentInstance(); String sDiscountPercent = percent.format(discountPercent); String sDiscountAmount = currency.format(discountAmount); String sTotal = currency.format(total); String sSubtotal = currency.format(subtotal); // format and display the results String message = "Subtotal: " + sSubtotal + "\n" + "Customer type: " + sCustomerType + "\n" + "Discount percent: " + sDiscountPercent + "\n" + "Discount amount: " + sDiscountAmount + "\n" + "Total: " + sTotal + "\n"; System.out.println(); System.out.println(message); return message; } static double getDiscountPercent(double subtotal, String type) { double discountPercent = 0.0; if (type.equalsIgnoreCase("r")) { if (subtotal >= 500) discountPercent = .2; else if (subtotal >= 250 && subtotal < 500) discountPercent =.15; else if (subtotal >= 100 && subtotal < 500) discountPercent =.1; else if (subtotal < 100) discountPercent =.0; } else if (type.equalsIgnoreCase("c")) { discountPercent = .2; } else discountPercent = .05; return discountPercent; } }
- 07-13-2011, 06:49 AM #8
You call the getInvoice method but what do you do with the returned value? Also why have you made everything static?
- 07-13-2011, 12:08 PM #9
Member
- Join Date
- May 2011
- Posts
- 46
- Rep Power
- 0
I just had an assignment to make everything static...I just did it out of a habit, I changed everything to non-static now. I made the getInvoice method void and got rid of the return, but I still am not getting any values to console.
Sorry about starting new thread about this same prob. never again.
- 07-14-2011, 12:37 AM #10
- 07-14-2011, 01:54 AM #11
Member
- Join Date
- May 2011
- Posts
- 46
- Rep Power
- 0
So, if I change :
public String getInvoice(String sCustomerType, double subtotal, double discountPercent, double discountAmount, double total){ // output formatted invoice
System.out.println("D%: "+discountPercent+"D$: "+discountAmount+"ST: +subtotal+"T: "+total);
// format the values
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
String sDiscountPercent = percent.format(discountPercent);
String sDiscountAmount = currency.format(discountAmount);
String sTotal = currency.format(total);
String sSubtotal = currency.format(subtotal);
// format and display the results
String message = "Subtotal: " + sSubtotal + "\n"
+ "Customer type: " + sCustomerType + "\n"
+ "Discount percent: " + sDiscountPercent + "\n"
+ "Discount amount: " + sDiscountAmount + "\n"
+ "Total: " + sTotal + "\n";
return message;
}
- 07-14-2011, 02:18 AM #12
Member
- Join Date
- May 2011
- Posts
- 46
- Rep Power
- 0
Like this:
message=myInvoice.getInvoice();
Sytem.out.println(message);
?
- 07-14-2011, 03:02 AM #13
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
Anyone can explain this?
By kazumahits in forum New To JavaReplies: 1Last Post: 03-08-2011, 02:03 AM -
Please Explain me how i got this output.. I am new to java .. so please Explain me...
By vicky82 in forum New To JavaReplies: 2Last Post: 12-13-2010, 01:34 PM -
Please Explain me how i got this output.. I am new to java .. so please Explain me...
By vicky82 in forum New To JavaReplies: 3Last Post: 12-13-2010, 07:22 AM -
Could you plese explain somebody how come output of these codes.
By moonlanka in forum New To JavaReplies: 8Last Post: 06-24-2010, 08:59 AM
Bookmarks