FAQ: Learn Java: Methods - Review

In the review section of Learn Java:Methods, I tried the code without any return methods, and it still works with the same result. Then I switched in up and used a return deposit method and made a void withdraw method, and same result yet again!
Then why did we create a return method? :confused:

1 Like

I’m very confused over this exercise. I do not understand why the “withdraw” method needs to return “amountToWithdraw”. Why cant it be a void like the “deposit” method? Also why would you return the “amountToWithdraw”? wouldn’t you want to return the balance? could someone please make this simple to understand.

I think i realized that the only reason we made the “withdraw” method an “int” rather than a “void” was to learn different ways of doing the same thing. I’m still very confused about why we return “amountToWithdraw” rather than the balance.

Hello @sirfency.

As you’ve stated, the exercise is getting us to practice the things we’ve learned. They aren’t necessarily the best ‘real world’ examples, but hopefully we are learning how to do things as we go. After completing the required steps, I frequently go back, and play around with the code to add functionality, experiment, etc. As far as your specific question:

Consider that we already have a method to check the balance of the account. Since there is no actual reason to return anything from any of the methods, after completing the exercise, I changed all of the methods to void, and just print out information. At the end of my deposit and withdraw methods, I call the checkBalance method. Here’s my withdraw method:

  public void withdraw(int amountToWithdraw){
    balance = balance - amountToWithdraw;
    System.out.println("You just withdrew " + amountToWithdraw);
    checkBalance();
  }

Output:

You just withdrew 300
Retrieving balance …
Your balance is 1700

hi i have a rly hard time with this exercise. ive gotten past the 2nd step with your help. but now i’m still trying to work through the 3rd step.
this my program up until the withdraw method:

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);
  }
  
  public void deposit(int amountToDeposit){
    balance = amountToDeposit + balance;
    System.out.println("You have deposited " + amountToDeposit);
    
public int withdraw(int amountToWithdraw){
balance = balance - amountToWithdraw;
   return amountToWithdraw;
 System.out.println("You have withdrew "+ amountToWithdraw);
}

Hello @board5114052855.

Welcome to the forums.

When I try to run your code, I receive the following error:

SavingsAccount.java:48: error: illegal start of expression
public int withdraw(int amountToWithdraw){
^

I formatted your code in your post to make it easier to read, and to allow me to copy/paste & try it myself. Please review this post for future reference: How do I format code in my posts?

Since the error indicates public is an illegal start of expression, and we know that your intent is to have that line: public int withdraw(int amountToWithdraw){ define a new method named withdraw, there is likely something amiss right before that line. Notice anything missing in the previous method?

What mistake did I make? the method checkWithdraw is not working.

public class SavingsAccount {
  
  int balance;
  int sumWithdraw;
  
  public SavingsAccount(int initialBalance, int totalWithdraw){
    balance = initialBalance;
    sumWithdraw = totalWithdraw;
  }
  
  public void checkBalance(){
    System.out.println("Hello, Liliane!");
    System.out.println("Your balance is "+balance);
  }
  
  public int deposit(int amountToDeposit){
    int balanceAfterDeposit = balance + amountToDeposit;
    balance = balanceAfterDeposit;
    System.out.println("You just deposited "+ amountToDeposit);
    return amountToDeposit;
  }
  
  public int withdraw(int amountToWithdraw){
    int balanceAfterWithdraw = balance - amountToWithdraw;
    balance = balanceAfterWithdraw;
    System.out.println("You just withdrew "+amountToWithdraw);
    return amountToWithdraw;
    
  }
  //method to calculate total amount of withdrawels and it is not working why?
  public int checkWithdraw(){
    int amountToWithdraw = 0;
    int balanceAfterWithdraw = balance - amountToWithdraw;
    balance = balanceAfterWithdraw;
    int totalWithdraw = sumWithdraw + amountToWithdraw;
    sumWithdraw = totalWithdraw;
    System.out.println("Your total withdrawel is "+ totalWithdraw);
    return totalWithdraw;
  }
  
  
  public static void main(String[] args){
    SavingsAccount savings = new SavingsAccount(2000,0);
    
    savings.checkBalance();
    savings.withdraw(300);
    savings.checkWithdraw();
    savings.checkBalance();
    savings.withdraw(100);
    savings.checkWithdraw();
    savings.deposit(600);
    savings.checkBalance();
    savings.deposit(600);
    savings.checkBalance();
    
    
    
  }
  
}
1 Like

Hello, @lilianetop3406017437.

Welcome to the forums!

The method is doing exactly what you’ve programmed it to do. It isn’t want you wanted it to do, so you need to see what it is doing.

The first time you call savings.checkWithdraw() the only withdrawal, so far is in the amount of 300, so at that moment the value of balance is 1700. Let’s walk through your method:

int balanceAfterWithdraw = balance - amountToWithdraw;
using actual values:

int balanceAfterWithdraw = 1700 - 0;

balance = balanceAfterWithdraw;
using actual values:

balance = 1700;

int totalWithdraw = sumWithdraw + amountToWithdraw;
using actual values:

int totalWithdraw = 0 + 0;

sumWithdraw = totalWithdraw;
using actual values:

sumWithdraw = 0;

System.out.println("Your total withdrawel is "+ totalWithdraw);
using actual values:

System.out.println("Your total withdrawel is "+ 0);
Output: Your total withdrawel is 0

Now that you know what is happening, you just need to figure out what to do instead to get the output you wanted.

If it were me, I’d probably want to increment the sumWithdraw variable by the value of each withdrawal at the time they occur. I would place that code inside my withdraw method since the amount to withdraw is already included as a parameter there.

Wauh I’m impressed. Yes it works if I add the totalWithdraw within the method of withdraw(). But I was trying to create a new method and discovered that I can’t use a parameter that has been used in another methode. I was trying to find a way to create a new method that would refer to a parameter in another method. This can’t be done? Or is this something that will be discussed later?

public class SavingsAccount {
  
  int balance;
  int sumWithdraw;
  
  public SavingsAccount(int initialBalance, int totalWithdraw){
    balance = initialBalance;
    sumWithdraw = totalWithdraw;
  }
  
  public void checkBalance(){
    System.out.println("Hello, Liliane!");
    System.out.println("Your balance is "+balance);
  }
  
  public int deposit(int amountToDeposit){
    int balanceAfterDeposit = balance + amountToDeposit;
    balance = balanceAfterDeposit;
    System.out.println("You just deposited "+ amountToDeposit);
    return amountToDeposit;
  }
  public int withdraw(int amountToWithdraw){
    int balanceAfterWithdraw = balance - amountToWithdraw;
    balance = balanceAfterWithdraw;
    System.out.println("You just withdrew "+amountToWithdraw);
    int totalWithdraw = sumWithdraw + amountToWithdraw;
    sumWithdraw = totalWithdraw;
    System.out.println("Your total withdrawel is "+ totalWithdraw);
    return amountToWithdraw;
    
  }
  //method to calculate total amount of withdrawels and it is not working why?
 /* public int checkWithdraw(){
    int amountToWithdraw = 0;
    int balanceAfterWithdraw = balance - amountToWithdraw;
    balance = balanceAfterWithdraw;
    int totalWithdraw = sumWithdraw + amountToWithdraw;
    sumWithdraw = totalWithdraw;
    System.out.println("Your total withdrawel is "+ totalWithdraw);
    return totalWithdraw;
  }*/
  
  
  public static void main(String[] args){
    SavingsAccount savings = new SavingsAccount(2000,0);
    
    savings.checkBalance();
    savings.withdraw(300);
    //savings.checkWithdraw();
    savings.checkBalance();
    savings.withdraw(100);
    //savings.checkWithdraw();
    savings.deposit(600);
    savings.checkBalance();
    savings.deposit(600);
    savings.checkBalance();
    
    
    
  }
  
}

See my comments to your code. If I were attempting what you’ve described I might do this:

  public int withdraw(int amountToWithdraw){
    balance -= amountToWithdraw;
    System.out.println("You just withdrew "+ amountToWithdraw);
    sumWithdraw += amountToWithdraw;
    checkWithdraw(); //call your other method from here
    return amountToWithdraw;
  }
  
  public void checkWithdraw(){ //this method doesn't need to return anything
    System.out.println("Your total withdrawal is "+ sumWithdraw);
  }

You could also add calls to checkWithdraw() to your main() method, and it will always print the current total of all withdrawals made.

P.S. FYI, please refer to this post for future reference: How do I format code in my posts? :wink:

1 Like

Hi. I understand this withdraw method… but regarding the original withdraw method, I still don’t understand why we return amountToWithdraw. Wouldn’t that value whatever we pass in through the parameter? I thought that balance was what we should return…

Hello, @betomercado115268528.

Welcome to the forums.

My best guess as to why the creator of this lesson has us return the amount that was withdrawn is that it acts as sort of a confirmation that the expected action (withdrawing x amount) was completed. We could add code to the method that checks whether the amountToWithdraw is actually available, and decide what to do from there if it isn’t.

I think you just forgot to enclose the method in the {} properly. Just add a } above your ‘public int withdraw’

I normally do not write to forums. But I do have some mild complaints over the wordings used in this exercise. I tried it out, but I was confused at many stages in this exercise, which hasn’t happened previously. I felt that the instructions could be clearer.

Hello. I was playing around on the Review and wanted to ask what was wrong with the following code:

public class SavingsAccount {
  
  int balance;
  String accountName;
  
  public SavingsAccount(int initialBalance, String name){
    balance = initialBalance;
    name = accountName;
  }
  public void checkBalance(){
    System.out.println("Hello!");
    System.out.println("Your balance is " + balance);
  }
  public void deposit(int amountToDeposit){
    balance = amountToDeposit + balance;
  System.out.println("You just deposited " + amountToDeposit);
  }
  
  public int withdraw(int amountToWithdraw){
  int newBalance = balance - amountToWithdraw;
  balance = newBalance;
  System.out.println("You just withdrew " +amountToWithdraw);
  return amountToWithdraw;
  }
  
  public static void main(String[] args){
    SavingsAccount savings1 = new SavingsAccount(2000, "Rick");
    SavingsAccount savings2 = new SavingsAccount(300, "Jeff");
    savings1.checkBalance();
    savings1.deposit(150);
    savings1.checkBalance();
    savings2.checkbalance();
    savings2.deposit(150);
    savings2.checkbalance();
    savings1.withdraw(200);
    savings1.checkBalance();
    System.out.println(savings1);
  }
  public String toString(){
  return "This is " + accountName +"'s account.";
  }
}

Hello, @tag7826720941.

Welcome to the forums.

I see a few issues. You have a method called checkBalance. Look closely at your calls to this method from inside your main method:

SavingsAccount savings1 = new SavingsAccount(2000, “Rick”);
SavingsAccount savings2 = new SavingsAccount(300, “Jeff”);
savings1.checkBalance();
savings1.deposit(150);
savings1.checkBalance();
savings2.checkbalance();
savings2.deposit(150);
savings2.checkbalance();
savings1.withdraw(200);
savings1.checkBalance();
System.out.println(savings1);

In your constructor method, you have an issue here:

public SavingsAccount(int initialBalance, String name){
  balance = initialBalance;
  name = accountName;
}

Compare what you did with balance with what you did with accountName. You handled balance correctly. As written, name will be null.

Also, for future reference, please refer to this post: How do I format code in my posts?
Properly formatting your code makes it much easier for others to help you. I’ve formatted your previous post, so you can see the difference.

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);
}

public void deposit( int amountToDeposit) {
balance += amountToDeposit;
System.out.println(“You just deposited “+amountToDeposit+”.”);
}

public int withdraw( int amountToWithDraw ) {
balance -= amountToWithDraw;
System.out.println("You just withdrew "+amountToWithDraw);
return amountToWithDraw;
}

public static void main(String args){
// Constructor
SavingsAccount savings = new SavingsAccount(2000);

savings.CheckBalance();

savings.withdraw(300);

savings.CheckBalance();

savings.deposit(600);

savings.CheckBalance();

}
}

Hi all. Above is my solution for the Review problem. The answers it prints appear to me to be correct:

Hello!
Your balance is 2000
You just withdrew 300
Hello!
Your balance is 1700
You just deposited 600.
Hello!
Your balance is 2300

Yet the compiler or what ever complains:

./LETest.java:44: error: cannot find symbol
savingsTest.checkBalance();
^
symbol: method checkBalance()
location: variable savingsTest of type SavingsAccount

Does any one have a clue? My guess is that “savings.Test” is a test driver and it’s set up wrong. On the other hand, I could be an idiot…

Has any seen anything like this before? Thanks soooooo much for any advice. I just can’t understand why I get could results as per above, and the test driver rejects it. --Robert

Hello, @method9791024881.

Welcome to the forums.

The instructions ask us to name the function checkBalance rather than CheckBalance, so when the SCT tests your code, it can’t find checkBalance to test that it works properly.

Also, for future posts, please review this topic: How do I format code in my posts?

You have two return statements. The return ends the execution of the block of code, so there is no way to reach the second return statement.