0
0
C Sharp (C#)programming~30 mins

Methods that operate on state in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Methods that operate on state
📖 Scenario: You are creating a simple bank account program. The account has a balance that changes when you deposit or withdraw money.
🎯 Goal: Build a class with methods that change the account balance and show the current balance.
📋 What You'll Learn
Create a class called BankAccount with a private balance variable.
Add a method Deposit that adds money to balance.
Add a method Withdraw that subtracts money from balance.
Add a method GetBalance that returns the current balance.
Create an instance of BankAccount and use the methods to change and show the balance.
💡 Why This Matters
🌍 Real World
Bank accounts, game scores, or any object that keeps track of changing information use methods to update and read their state.
💼 Career
Understanding how to write methods that operate on an object's state is essential for software development, especially in object-oriented programming.
Progress0 / 4 steps
1
Create the BankAccount class with a balance variable
Create a class called BankAccount with a private integer variable balance set to 0.
C Sharp (C#)
Need a hint?

Use class BankAccount and inside it declare private int balance = 0;.

2
Add Deposit and Withdraw methods
Add a public method Deposit that takes an integer amount and adds it to balance. Add a public method Withdraw that takes an integer amount and subtracts it from balance.
C Sharp (C#)
Need a hint?

Write two methods: Deposit adds amount to balance, Withdraw subtracts amount from balance.

3
Add GetBalance method
Add a public method GetBalance that returns the current balance as an integer.
C Sharp (C#)
Need a hint?

Write a method GetBalance that returns the value of balance.

4
Create an instance and show the balance
Create an instance of BankAccount called account. Call Deposit(100) and Withdraw(30) on account. Then print the result of account.GetBalance().
C Sharp (C#)
Need a hint?

Create account, call Deposit(100), Withdraw(30), then print account.GetBalance().