1

Every time this program loops, it resets the cash variable back to 500. How do I initially set it to 500, subtract the bet and then have it remember that value on its next iteration? My output for example:

Computer 1 bets: $50 I now have: $450 //
Computer 2 bets: $50 I now have: $450 //these are right
Computer 3 bets: $50 I now have: $450 //

Computer 1 bets: $50 I now have: $450 //
Computer 2 bets: $50 I now have: $450 //now should have $400
Computer 3 bets: $50 I now have: $450 //

Computer 1 bets: $50 I now have: $450 //
Computer 2 bets: $50 I now have: $450 //now should have $350
Computer 3 bets: $50 I now have: $450 //

My code:

public class Computer {
    private int id;
    private int bet;
    private int cash = 500;
    private static Computer[] c;

    public static void create(int numComps) {
        c = new Computer[numComps];

        for (int i = 0; i < numComps; i++) {
            c[i] = new Computer();
            c[i].id = i + 1;
            c[i].bet = 50;
            c[i].cash -= c[i].bet;
            c[i].display();

        }
    }

    private void display() {
        String name = "Computer " + id;
        System.out.println(name + " bets: $" + bet + " I now have: $" + cash);
    }

    public static void main(String[] args) {
        int i = 0;
        do {
            create(3);
            System.out.println();
            i++;
        } while (i < 3);
    }

}

2 Answers 2

1

In the loop, you are calling create, but logically, create should only be called once, as it creates new computer objects. Also, create does not do what it says. Not only does it create computer objects, it also makes them bet money, and displays them. bet should be a separate method and create should not call display at all:

public static void create(int numComps) {
    c = new Computer[numComps];

    for (int i = 0; i < numComps; i++) {
        c[i] = new Computer();
        c[i].id = i + 1;
        c[i].bet = 50;

    }
}

public void bet() {
    cash -= bet;
}

In the main method, you should call bet and display, but only call create once:

public static void main(String[] args) {
    create(3);
    int i = 0;
    do {
        for (int j = 0 ; j < 3 ; j++) {
            c[j].bet();
            c[j].display();
        }
        System.out.println();
        i++;
    } while (i < 3);
}
Sign up to request clarification or add additional context in comments.

Comments

1

tl;dr Your call to create(3); instantiates three new computers every time it is called.

You need to start following the separation of concerns rule of software design. Each method in your code should have one job. The create method is taking on too many responsibilities.

Your biggest problem here is that static method you have conjured up. It is making this more complicated than it needs to be.

How do I initially set [the cash variable] to 500

You need to initialize that private field in the constructor.

subtract the bet

You need to add a public method to a class to allow other code to make changes to its private fields.

and then have it remember that value on it's next iteration?

You have already taken care of this. As you wrote it, the Computer class "remembers" the value of the private field cash just fine. The problem is that you are just accidentally instantiating three new Computers on every iteration of your while-loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.