FAQ: Learn Java: Methods - Review

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.

6 Likes

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.

Hi,so at the Learn Java:9.Review exercise I got confused :
1.Why do we not need a return value for deposit() method just like we did with withdraw() method?
here:

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

i dont get it.

Hello, @object0478927626, and welcome to the forums.

‘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. :man_shrugging:

1 Like

Thank you very much for your thorough reply. I understood it :white_heart:

1 Like
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:
    int afterWithdraw = savings.balance - 300;
    savings.balance = afterWithdraw;
    System.out.println("You just withdrew "+300);
    
    //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);
    return;
  }
  public void deposit(int amountToDeposit){
    balance = balance + amountToDeposit;
    System.out.println("You just deposited " + amountToDeposit);
  }
  public int withdraw(int amountToWithdraw){
    balance = balance - amountToWithdraw;
    System.out.println("You just withdrew " + amountToWithdraw);
    return amountToWithdraw;
}
  public int balance
}

Hi,

I have managed to get through to step four with the above code. However, I do not understand how to call the method in the main.

Is it wishing for me to get rid of the current code in main and get it to run the methods I have written at the bottom?

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

//Check balance:
savings.checkBalance();

//Deposit:
savings.deposit(1000);
savings.deposit(1);
savings.deposit(110);

//Withdrawing:
savings.withdraw(100);

System.out.println(savings);

}

// 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.
*/

1 Like

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”.

1 Like

Thank you for your help. That solved it. Your explanation is very good at explaining objects.

What was it though??

You are supposed to write the code ABOVE the main method. So you have to write it all out on line 9.

Hi all,

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

//Check balance:
savings.checkBalance();


//Withdrawing:
savings.withdraw(300);

//Check balance:
savings.checkBalance();

//Deposit:
savings.deposit(600);


//Check balance:
savings.checkBalance();

//Deposit:
savings.deposit(600);

//Check balance:
savings.checkBalance();

}

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?