开发者

making sure i am doing this problem correctly

开发者 https://www.devze.com 2023-03-05 19:11 出处:网络
this is my first programming course, and i want to make sure i am doing this problem correctly. if you could check over m开发者_如何学Pythony work it would be greatly appreciated.

this is my first programming course, and i want to make sure i am doing this problem correctly. if you could check over m开发者_如何学Pythony work it would be greatly appreciated.

Write a method to compute and return the balance for a checking account, given the starting balance and an array of Check objects. You may assume that the Check class already exists and has a method to get the amount from a particular check object called: double getAmount()

The array is not full, and may have gaps in it – make sure you test to see if there is an object there before you try to access it! Make your code work for any length array!

The header for the method is provided for you:

public double computeBalance(double startingBalance, Check[] register) {

    int i = 0; // i must be initialized and declared somewhere at least
    double total = 0.0;

    while ((i >= check.length) && check[i] != null) { // is >= correct? you do i++!
           total = (total + getAmount(check[i])); // should you add/compute somewhere 
                                                      // the given amounts
           i++;

    }  

    System.out.println(total);
}


Forget programming for a second. If I told you "Here's the starting balance in your account." and then handed you a bunch of checks and told you to compute the ending balance, how would you do it? Once you understand that, you can start to work on the programming problem.

Some questions:

  • Where are you tracking the account balance?
  • What will happen in your loop if one of the slots in register is empty (i.e. null)?
  • What is this check variable in your loop? Where is it being declared? Is check really what it should be called?
  • The function is declared as returning double. What are you returning?
  • Have you tried compiling your code? What happens?


I understand that you are asking for more than for the solution itself but there are obviously better people to guide you. You can use my example as a reference to what others are explaining to you.

public double computeBalance(double startingBalance, Check[] register) {

    // let's start off from the starting balance
    double total = startingBalance;

    // go over all elements starting from 0
    for (int i = 0; i < check.length; i++) {

        // make sure you did not encounter null element
        if (register[i] != null) {

            // increase the total by the amount of the Check
            total += register[i].getAmount();
        }
    }

    // and finally return the resulting value
    return total;
}


The execution will end when you reach a gap. Use an if-statement inside the loop for the null check instead.


If you could run your code through a compiler (which it sounds like you can't, or at least aren't being encouraged to), it would tell you that it has no idea what i, check, or getAmount are.

A method body that doesn't refer to the method parameters is generally missing something -- especially if the parameter declarations were given by your instructor.

Look again at your loop condition. What is the value of i going to be at the beginning?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号