Sign in to follow this  

New in Java again

This topic is 3483 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 start learning Java and i'm confused of something. Please feel free to help me figure this out. I'm self-learning so, i'm not really know which one is right, which one is wrong Here is the program: //This program returns bank's service fees for the month // Bank Charge $10 per month plus $0.1 cent for each check if number of check is // less than 20, $0.08 cents if checks < 40, 0.6 if checks < 60, 0.04 if checks >= 60 // The bank charge $15 if balance falls below $400 public class BankCharge { private double balance; private int numChecks; private double rate; private double charge; private double monthlyCharge; private double current; public BankCharge(double beginBalance, int numberOfCheck) { balance = beginBalance; numChecks = numberOfCheck; Calculate(); } public void setBalance(double b) { balance = b; } public void setNumOfChecks(int c) { numChecks = c; } private void setRate() { if(numChecks < 20) { rate = 0.10; } else if(numChecks < 40) { rate = 0.08; } else if(numChecks < 60) { rate = 0.06; } else { rate = 0.04; } } private void Calculate() { if(balance < 400) { charge = 15.0; } else { charge = 0.0; } setRate(); monthlyCharge = numChecks*rate + 10 + charge; current = balance - monthlyCharge; } public double getBalance() { return balance; } public int getNumOfCheck() { return numChecks; } public double getRate() { return rate; } public double getCharge() { return charge; } public double getMonthlyCharge() { return monthlyCharge; } public double getCurrent() { return current; } } ------------------------------------------------------------------- and here is the main: import java.util.*; public class TestBankCharge { static Scanner console = new Scanner(System.in); public static void main(String[] args) { int numChecks; double balance; ; System.out.print("Enter beginning balance: "); balance = console.nextDouble(); System.out.print("Enter the number of checks written: "); numChecks = console.nextInt(); BankCharge bankAccess = new BankCharge(balance, numChecks); System.out.println("The number of checks written: " + bankAccess.getNumOfCheck()); System.out.println("Beginning balance: $" + bankAccess.getBalance()); System.out.println("Rate: " + bankAccess.getRate()); // The rate charge for each check System.out.println("Bank Charge: $" + bankAccess.getCharge()); // This will be charged if beginning balance less 400 System.out.println("Monthly Charge: $" + bankAccess.getMonthlyCharge()); // Bank monthly charge System.out.println("Current balance: $" + bankAccess.getCurrent()); // Current balance System.exit(0); } } ----------------------------------------------------------- This one working well when i ran it and when i deleted some codes from the "BankCharge" class. The one i deleted is: public void setBalance(double b) { balance = b; } public void setNumOfChecks(int c) { numChecks = c; } 1. Which one is correct? the original one or the one has been deleted? 2. Do we actually need the codes i deleted when we already have "Constructor" (Because for me they are the same) 3. This program i took from the "Starting out with Java" by Tony Gaddis. I try to learn Java to not only for fun but also to look for a Job. But Tony Gaddis has many Java book, these are: a) Starting Out with Java 5: Control Structures to Objects (Gaddis Series) by Tony Gaddis b) Starting Out with Java: From Control Structures through Objects c) Starting Out with Java: Early Objects d) Starting Out with Java: From Control Structures through Data Structures e) and "Starting out with Java" (the one i using now) I don't know which one is the right one for me to learn Java as pro and why there are so many of them which i can not buy them all. If any of you know please tell me which one is the right one to learn Thanks alot!

Share this post


Link to post
Share on other sites
Hi,

I will try to give some answers to your questions:

1) I would say both are technically correct. If you remove those 2 methods from your class, you just remove some functionality from it. Since you're not using that functionality anywhere else in your code(you never call one of the methods inside your class, or you never call them from out of your class), they will both work (no compilation/linking errors).

2) The constructor is a special method that is called when you create a new object and it can only be called to create a new object. In your constructor you initialize some members of the object. If you want to change the members of that object later on, you need some other way to do it. One way to do this, is to make your members public and change them directly (eg bankAccess.numChecks = someValue), but this is not considered as a good practise. That's why you need to create some methods that can change the members (called setters).
An important thing you should consider is whether it should be possible for another user of your class to use the setters by making the setters private, protected or public (so he can or cannot access the members of the object) (the standard answer should be no :-)). But that's more a design issue (which is ofcourse equally important but might be to overwhelming to start with if you're just learning the syntax).

3) My advice: read "Thinking in Java" from Bruce Eckel. You can download it for free (you sure will find a link yourself). At first it might be a little bit difficult and maybe you have to read it a couple of times to completly understand it, but if you plan on becomming a pro in java, you should have a good understanding of all the concepts described in the book.

Hope this helps,

Jan

Share this post


Link to post
Share on other sites

This topic is 3483 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