Download presentation
Presentation is loading. Please wait.
1
public class BankAccount{
Class Structure public class BankAccount{ fields constructor(s) methods } Sample Code Ticket Machine (2.1) copyright by Scott Grissom
2
also called instance variables use lower case for names
Fields stores information also called instance variables use lower case for names identify data type Examples int balance; int accountNum; String name; Draw object diagram copyright by Scott Grissom
3
initializes the object to an appropriate state same name as Class
Constructors initializes the object to an appropriate state same name as Class public BankAccount (String name, int id){ name = last; accountNum = num; balance = 0; } copyright by Scott Grissom
4
ask object about its state method signature body
Accessor Methods ask object about its state method signature body generally contains a return statement recommend a name starting with ‘get’ public int getBalance ( ){ return balance; } copyright by Scott Grissom
5
generally changes the object state might ask object to do something
Mutator Methods generally changes the object state might ask object to do something public void makeDeposit (int amount){ balance = balance + amount; } copyright by Scott Grissom
6
displays results in the terminal window
Print Statements displays results in the terminal window public void printStatement( ){ System.out.println(“Bank of GVSU”); System.out.println(“Name: “ + name); System.out.println(“Account #: “ + id); System.out.println(“Balance: “ + balance); System.out.println(); } copyright by Scott Grissom
7
ask questions about the TicketMachine code
Group Exercises ask questions about the TicketMachine code write a method called identityTheft that sets the balance to zero write a method makeWithdrawal that removes the provided amount from the balance write a method setBalance that replaces the existing balance with the provided amount copyright by Scott Grissom
8
Conditional Statements (2.11)
Sample Code (2.8) if (some condition){ do if true }else{ do if false } boolean expressions six relational operators >, <, ==, >=, <=, != copyright by Scott Grissom
9
Local variables temporary values public int refundBalance(){
return balance; } int amountToRefund = balance; return amountToRefund; copyright by Scott Grissom
10
Group Exercises 2.51 2.60 2.63 2.66 2.82 result = x + y + z;
result = (x + ) * z; copyright by Scott Grissom
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.