Sign in to follow this  

New in Java

This topic is 3492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I'm start learning Java 3 week ago. Though, i stuck at "Class" and can not go further. Please help me with this program. Please look at this code: public class SoftwareSales { private int quantity; private double rate; private double charge; public SoftwareSales(int qua) { quantity = qua; Calculate(); } private void setRate() { if(quantity < 10) { rate = 0.0; } else if(quantity < 20) { rate = 0.2; } else if(quantity < 50) { rate = 0.3; } else if(quantity < 100) { rate = 0.4; } else { rate = 0.5; } } private void Calculate() { setRate(); charge = quantity*99 - quantity*rate*99; } public int getQuantity() { return quantity; } public double getRate() { return rate; } public double getCharge() { return charge; } } ---------------------------------------------------------- //And here is it main program import java.util.*; public class TestSales { static Scanner console = new Scanner(System.in); public static void main(String[] args) { int quantity; SoftwareSales sales; System.out.print("How manay software do you want to buy? "); quantity = console.nextInt(); if(quantity <= 0) { System.out.println("Enter positive integer only"); } else { sales = new SoftwareSales(quantity); System.out.println("The quantity is: " + sales.getQuantity()); System.out.println("The rate is: " + sales.getRate()); System.out.println("The total is: " + sales.getCharge()); } System.exit(0); } } ------------------------------------------------------------------------- but when i add some more to the class SoftwareSales some more code, it's still have the same result public class SoftwareSales { private int quantity; private double rate; private double charge; public SoftwareSales(int qua) { quantity = qua; Calculate(); } public void setQuantity(int q) { quantity = q; } public void setCharge() { return charge; } private void setRate() { if(quantity < 10) { rate = 0.0; } else if(quantity < 20) { rate = 0.2; } else if(quantity < 50) { rate = 0.3; } else if(quantity < 100) { rate = 0.4; } else { rate = 0.5; } } private void Calculate() { setRate(); charge = quantity*99 - quantity*rate*99; } public int getQuantity() { return quantity; } public double getRate() { return rate; } public double getCharge() { return charge; } } the difference of those 2 class is public void setQuantity(int q) { quantity = q; } public void setCharge() { return charge; } Please show me with one is correct, and which onee is not Thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by titi3000
the difference of those 2 class is public void setQuantity(int q)
{
quantity = q;
}
public void setCharge()
{
return charge;
}

Please show me with one is correct, and which onee is not
Thanks

1) never return something if the return type is void
2) which ones is correct depends on what you expect

Share this post


Link to post
Share on other sites
By the way, if I may suggest it, it might be a good idea to use [ source] [ /source] tags (without the spaces added within the tags to foil tag detection) to encapsulate code that's more than a few lines long (or when you want formatting to be preserved, of course). It improves the readability and neatness of your post, I think, and I believe that this probably improves the chances of a given reader responding. ^_^

The result should look something like this:

//Some code here
//More code here, but indented

//Note that if we have enough lines, a scrollbar appears
// (although this should be too short), keeping the post
// shorter than it might have been without the box.

Share this post


Link to post
Share on other sites

This topic is 3492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this