‘Need’ is an interesting word here. Consider the method declaration: public void deposit(int amountToDeposit) Since the return type is declared void not only do we not ‘need’ a return value. We cannot have a return value.
It’s up to the programmer to decide what or if any method ‘needs’ to return a value. In this case you are following the instructions of the exercise, so the instructor decided that the method doesn’t need a return value. The method performs its assigned function of adding the amountToDeposit to the value assigned to balance, prints a message, and is done.
There probably isn’t a ‘need’ to have a value returned from the withdraw() method either other than to illustrate the different method declarations. One with a void return type and one with an int return type.
If it wasn’t in the context of an instructive exercise, it would be considered inconsistent by many programmers. I suppose it still is inconsistent.
public SavingsAccount(int initialBalance){
balance = initialBalance;
}
public void checkBalance() {
System.out.println(“Hello!”);
System.out.println("Your balance is " + balance);
// no return statement because of the “void” in the method signature
}
public void deposit(int amountToDeposit) {
int afterDeposit = balance + amountToDeposit;
balance = afterDeposit;
System.out.println("You just deposited "+amountToDeposit);
// no return statement because of the "void" in the method signature
}
public int withdraw(int amountToWithdraw) {
int afterWithdraw = balance-amountToWithdraw;
balance = afterWithdraw;
System.out.println("You just withdrew " + amountToWithdraw);
return amountToWithdraw;
// here we do have a return statement because of the lack of the “void” keyword in the method signature
}
public static void main(String args){
SavingsAccount savings = new SavingsAccount(2000);
// call toString() method
public String toString() {
return "After all your account activity, your final balance is " + balance + “!”;
}
}
/*
This is what my code looked like for step 9/9 of this section. I tried to keep it simple and it seemed to satisfy the requirements needed to move on to the next section. Hope this helps.
*/
Yes the requirements are that you get rid of the code in the main() because it would be redundant. You have re-created the methods in the bottom section of your code so all that instruction could be removed from the main(). Also, I think its better if you copy paste all the methods you created and paste them above the main(). You might get an error because I believe java will look for the code in the order you write it in. So it’s better in my opinion to write it above the main() method so that it’s “ready to go” before you use it in the main() method.
The way you would call your methods that you created, the “withdraw()”, “deposit()”, and “checkBalance()” methods in the main() method would be like this…
in the main(), we created an instance of an object. The object we created is called “savings” and it is of the class “SavingsAccount”. The object is called “savings” so all the methods need to work from this object we created. The class “SavingsAccount” only tells us about the name of the file and type of general ‘object’ this class is trying to create. You can technically call the object anything you want… you can change the name of the object “savings” to “mySavingsAccount” or “myWifesSavingsAccount”. But you should keep it as “savings” for the purpose of this exercise.
In the main() method you will call the methods you created by spelling it out like this…
savings.checkBalance();
savings.deposit(1000); or any other amount you want to deposit, its up to you
savings.withdraw(1000); or any other amount you want to deposit, its up to you
So every time you call the methods you created ( like the checkBalance or deposit methods), they have to work on the object you created, which is “savings” in this case. The object you created is also inside the main() method, it was created using the line “SavingsAccount savings = new SavingsAccount(2000);” that line tells the program to create an object called “savings” of the class “SavingsAccount”.
Below is my main code and it is working. But I can’t help but notice that I have savings.checkBalance(); after every action. Make a withdraw --> Check balance, make a deposit --> Check balance. This can’t be the most efficient way to run the code.
Is there a way to include check balance into my withdraw/deposit methods? That way when i make a withdraw it automatically does that action and then calls the checkBalance method? That way I could remove multiple savings.checkbalance(); lines.
public static void main(String args){
SavingsAccount savings = new SavingsAccount(2000);
I finally completed the Review of the the Methods instruction. I passed it, but my final code still produced 3 errors. How can I pass with errors? Not understanding what is wrong. Oh, and it’s not printing to the screen.
public class SavingsAccount {
int balance;
public SavingsAccount(int initialBalance){
balance = initialBalance;
}
public void checkBalance(){
System.out.println(“Hello!”);
System.out.println("Your balance is " +balance);
savings.checkBalance();
}
public int withdraw(int amountToWithdraw){
balance = balance-amountToWithdraw;
System.out.println("You just withdrew "+amountToWithdraw);
return amountToWithdraw;
savings.withdraw();
}
public void deposit(int amountToDeposit){
balance = balance + amountToDeposit;
System.out.println("You just deposited "+amountToDeposit);
savings.deposit();
}
public static void main(String args){
SavingsAccount savings = new SavingsAccount(2000);
}
}
So are constructors the only method that don’t require a return type?
They’re only called during the instantiation of a new object, meaning they can’t return something, right?
The deposit() method doesn’t return anything, but the withdraw() method does.
I tested switching withdraw() over to void (even though it is not what the exercise asked for), and Java seems to have some incompatibility problem with it.
Why is that?
late reply,hope you dont mind.
regarding to those errors and also to get the print statements on the screen, its because you are supposed to call the methods in the main method and not in the method itself i.e it should go something like this
public static void main(String args) {
SavingsAccount savings = new SavingsAccount(2000)
savings.balance();
savings.withdraw();
savings.depsit();
}
Why do we have to return this value? What is the point of “return”? Why specifically do we have to return amountToWithdraw and not other values such as amountToDeposit? I don’t really understand the return feature…
but where is it making use of the returned value from withdraw method??
how would i know that it has to make use of amountToWithdraw instance somewhere?
In this case, the task contains such a demand.
In real life, if you want your method to return some reworked data for using it elsewhere in your program, you define a “return type” (i.e. the type of data you get back from the method). If your method prints something or changes variable(s), it does not mean it returns something and you right “void” instead of data type. It depends entirely on your “logic”.
Here a sample that can help:
public class Test {
private static int sumOfTwoInts(int a, int b) { // Gets x and y as arguments
int sum = a + b; // 30 + 10
return sum;
}
private static double multiply(int a, double b) {
// Gets return of sumOfTwoInts(x, y) and z as arguments
return a * b; // returns 40 * 2.5
}
public static void main(String[] args) {
int x = 30;
int y = 10;
double z = 2.5;
System.out.println("The result of calculations: " + multiply(sumOfTwoInts(x, y), z));
// Calls methods (the deeper method calculates earlier)
// Gets return of multiply(sumOfTwoInts(x, y), z) which is "100.0"
// Prints "The result of calculations: 100.0"
}
}
Hello. I am also on the Java methods lesson part 9 & on task 3 this error comes up:(below)
./LETest.java:8: error: incompatible types: void cannot be converted to int
int returned = savingsTest.withdraw(200);
^
./LETest.java:18: error: incompatible types: void cannot be converted to int
int returned = savingsTest.withdraw(500);
^
2 errors
Hey everyone i’m stuck on step 4 of the methods review in the object oriented java section… i think i just missed something but I’ve been stuck on it for a while. here is my program.
public class SavingsAccount {
int balance;
public SavingsAccount(int initialBalance){
balance = initialBalance;
}
public static void main(String args){
SavingsAccount savings = new SavingsAccount(2000);
//Check balance:
System.out.println("Hello!");
System.out.println("Your balance is "+savings.balance);
//Withdrawing:
System.out.println(amountToWithdraw);
//Check balance:
System.out.println("Hello!");
System.out.println("Your balance is "+savings.balance);
//Deposit:
int afterDeposit = savings.balance + 600;
savings.balance = afterDeposit;
System.out.println("You just deposited "+600);
//Check balance:
System.out.println("Hello!");
System.out.println("Your balance is "+savings.balance);
//Deposit:
int afterDeposit2 = savings.balance + 600;
savings.balance = afterDeposit2;
System.out.println("You just deposited "+600);
//Check balance:
System.out.println("Hello!");
System.out.println("Your balance is "+savings.balance);
}
public void checkBalance(){
System.out.println(“Hello!”);
System.out.println("Your balance is " + balance);
}
public void deposit(int amountToDeposit)
{
int newBalance = amountToDeposit + balance;
balance = newBalance;
}
public int withdraw(int amountToWithdraw){
int newBalance = balance - amountToWithdraw;
balance = newBalance;
return amountToWithdraw;
Can someone tell me what i have to do because i think the problem is i just didnt understand the question properly
Hey there, I don’t know if you’re still stuck, but task no. 4 asks you to call the methods you defined in the previous steps. I see that your main() still holds a lot of code that is not needed anymore.
Basically you can clear your main(), call the constructor to create a new SavingsAccount and call all of your methods one after the other (tho I would suggest you call your checkBalance() every time you called the withdraw() or deposit() method.
So your main() should look something like this:
public static void main(String[] args){
SavingsAccount savings = new SavingsAccount(2000);
savings.checkBalance();
savings.deposit(600);
savings.checkBalance();
savings.withdraw(600);
savings.checkBalance();
System.out.println(savings);
}