Class Structure public class Bank Account fields constructors
Class Structure public class Bank. Account{ fields constructor(s) methods } • Sample Code Ticket Machine (2. 1) copyright by Scott Grissom 1
Fields • • • stores information also called instance variables use lower case for names identify data type Examples int balance; int account. Num; String name; • Draw object diagram copyright by Scott Grissom 2
Constructors • initializes the object to an appropriate state • same name as Class public Bank. Account (String name, int id){ name = last; account. Num = num; balance = 0; } copyright by Scott Grissom 3
Accessor Methods • • • ask object about its state method signature body generally contains a return statement recommend a name starting with ‘get’ public int get. Balance ( ){ return balance; } copyright by Scott Grissom 4
Mutator Methods • generally changes the object state • might ask object to do something public void make. Deposit (int amount){ balance = balance + amount; } copyright by Scott Grissom 5
Print Statements • displays results in the terminal window public void print. Statement( ){ 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 6
Group Exercises • ask questions about the Ticket. Machine code • write a method called identity. Theft that sets the balance to zero • write a method make. Withdrawal that removes the provided amount from the balance • write a method set. Balance that replaces the existing balance with the provided amount copyright by Scott Grissom 7
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 8
• Local variables • temporary values public int refund. Balance(){ balance = 0; return balance; } public int refund. Balance(){ return balance; balance = 0; } public int refund. Balance(){ int amount. To. Refund = balance; balance = 0; return amount. To. Refund; } copyright by Scott Grissom 9
Group Exercises 2. 51 2. 60 2. 63 2. 66 2. 82 result = x + y + z; result = x + y * z; result = (x + ) * z; copyright by Scott Grissom 10
- Slides: 10