Classes and Objects
The course is part of this learning path
This course looks at Object-Oriented programming in Java and shows how classes are designed and constructed, and how objects are created from them. Then, we'll complete three projects: creating a bank account class, an ice cream class, and a circle class, as well as tests to make sure that they work in order to reinforce what you've learned.
- Beginner coders or anyone new to Java
- Experienced Java programmers who want to maintain their Java knowledge
- Developers looking to upskill for a project or career change
- College students and anyone else studying Java
This is a beginner-level course and can be taken by anyone with an interest in learning about Java.
In the previous lectures, we've learned about how to create our own classes and create objects based on those classes. In this lecture, you will complete a challenge to create a BankAccount class, which as the name suggests, is a class representing an individual's bank account. The specification in the UML class diagram that you see shows us what fields and methods we need in order to implement our bank account class. We could use doubles if we'd like but I chose to do everything in terms of US Dollars. So, let's use integers for this class and keep things simple. There are some things you need to keep in mind for this project.
The balance for the first constructor should be set to zero. The balance passed into the second constructor must be greater than zero. When a value is passed to deposit or withdrawal, it must be greater than zero. Also, one reason is that if you tried withdrawing a negative amount, it would actually increase your balance. That would be a significant security issue. Note that once the owner is set, it cannot be changed. Note that the balance can't be explicitly changed to a different value and must be interacted with using the deposit and withdrawal methods.
If the user ever tries to pass zero or a negative value to the constructor, or zero or negative value to the deposit or withdrawal methods, you should print out a message to the user to standard output, saying that the value must be greater than zero and not change the balance. Additionally, if a user tries to withdraw more than he or she has, you must warn them and not do anything to the balance either. So, make sure to create a BankAccount.java file and a BankAccountDemo.java file to test out some of these methods.
All right, let's work on this here. Run this demo. There we go. That's what the bank account should print out, something like this. I did a little bit of testing to make sure that the deposit was in range and that it didn't affect the balance and there's a warning printed out. I also made sure that you could have valid deposits as well. Hope that helps. So, pause the video and give this project your best shot come back when you're done or if you need some help. How did that work out for you? Were you able to complete this project? You might have noticed that some of these projects take a little more time to structure than the super simple applications we worked on very early in the course.
This is a natural progression to becoming better and more confident developers. Let's work on this together. Let's create the BankAccount Java class and the BankAccountDemo. Not Kotlin, we do not like Kotlin class. We want a Java class. There we go, BankAccountDemo. Inside BankAccountDemo, we will get the main setup here. There we go. And over here, the BankAccount will start over here. I usually bring it down a little bit first. BankAccount also not fully necessary but I put that there. It has a String owner and a balance. So, with BankAccount, we have one that takes just the owner. That's one constructor. We take another; that takes two. This one will set them directly and since this one does everything that the other one does and more we can use this to our advantage.
So, up here I'm going to just call owner or call this rather which we'll call this constructor. It will pass on the owner that they initially passed into this one and it will set the balance to zero as the requirements indicated. So, over here we overcome name shadowing obviously like we've done before using this keyword. Now, deposit an amount. Let's set that up. And we also have withdrawal which will do the second I guess. So, we have an amount passed in. If that amount is greater than or actually greater than zero, I believe is what we said. We're going to say, balance += amount and then, otherwise we have end of deposit. So, in the else, System.out.println, we'll say amount to deposit must be greater than zero. Here we go.
And now, we're going to go implement the withdraw method. Same thing here. Similar thing here at least. If the amount is greater than or equal to zero, actually for the balance I guess we're going to make it greater than zero and it's less than or equal to the balance then, that's valid. So, it's in range. Amount less than or equal to the balance then that's in range. So, what we do here is we're going to say balance -= amount. Remember that is basically a shortcut just like we used up here. It's a shortcut for balance = balance - amount. And here is the balance = balance + amount. So, that's the shortcut syntax using the combined addition assignment operator and the subtraction assignment operator respectively. So, here we'll give them a nice little warning. The amount to deposit must be greater than zero and in then the next line, we'll say and less than your balance.
Very good, and we could even print out the balance if we wanted to. We could say less than your balance which is. And then print it out. That's fine too, if you want to do that. Little side challenge for you. All right, string getOwner(). Just standard accessors, return owner and then getBalance. Excellent. Get rid of this extra space here and then we'll go over into the file with main in it, BankAccountDemo. So, with the BankAccount object that we're going to create, we're going to use the constructor with the two parameters and set the balance as well as the owner with another one we'll just set the owner's name. So, to test them out, myAccount; balance. Sorry about that.
BankAccount, your bank account, I have balance on the mind there. We'll say "John Baugh" and let's put 5000 into his account and then, BankAccount bobsAccount = new BankAccount Bob. Sorry. "Bob Robinson". We don't put the second argument because we want to call the constructor with only one argument that will set the balance to zero to test that out. But immediately, maybe we want to add something to Bob's account. So, we put 500 in there and then we'll say System.out.println, "owner", bobsAccount.getOwner(). And then, we'll say System.out.println "balance", getBalance().
And of course, let's do bobsAccount.withdraw. Let's try withdrawing 1000. That should give us a warning. And then, we'll say System.out, actually wait since it's the same stuff, I'm just going to copy these two lines right here with the owner and the balance. It should give you a hint that we could create a method to help us. All right so, the balance doesn't change because the withdraw had a value larger than the balance passed to it. So, it shouldn't affect. Let's also put a little extra space here and of course, we'll work on myAccount. We'll say owner and it should be myAccount.getOwner() and System.out.println will say balance and then that is myAccount.getBalance(). And we'll do some extra space, of course. I'll put it up with that right here, out.println.
Just some extra space. And let's see here, what do we want to do. Let's try a deposit here and see what happens. Deposit 1000 and then we'll say myAccount.deposit(1000). And then, we'll say, I will actually print out the same thing with myAccount owner and balance again. So, I'll just copy this again and we don't really need the print line at the end because that would be the last statement. You could test it with negatives too obviously. So, let's print this out and run this and see if we can get BankAccountDemo. Run it and there we go. Owner Bob Robinson has 500. And then, when we tried to make a withdraw of 1000, that's too much, right? So, it says the amount to deposit must be greater than zero and less than your balance which we know is 500.
So, the balance stays the same. Then John Baugh has 5000, deposited 1000 since that's valid. It now prints out that John Baugh has 6000. Awesome work, everyone. Hopefully, you were able to get similar output when you tested yours. Obviously, you probably use different values than I did if you were just working on it on your own. But if you use my BankAccountDemo class with your BankAccount class, you should get the exact same amounts. Otherwise, there might be something wrong. In the next lecture, we will work on creating an ice cream class. I'll see you there.
John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.