1. Trang chủ
  2. » Giáo án - Bài giảng

Giáo trình Java cơ bản 19

51 268 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 51
Dung lượng 489,26 KB

Nội dung

Lecture 19  Covers – Methods that return a value – void methods – Parameter passing (call-by-value)  Reading: Savitch 4.1 19/1 Example - bank accounts  Create and test a class representing a bank account that – Has an id, name, and balance – Allows a user to deposit, withdraw, and get the balance  Rules – Id’s and names must not be missing – Balance must not be negative (no overdraft) 19/2 Defining class BankAccount  To define the BankAccount class and test it, we go through the following typical steps Sketch Define Define Define Define a model of the class the class header the attributes the constructors the methods 19/3 Sketching a model of the class BankAccount class name String accountNumber String customerName double balance attributes BankAccount(String accNo, String custName) void deposit(double amount) void withdraw(double amount) methods double getBalance( ) String toString( ) 19/4 Specifying the class header public class BankAccount { } 19/5 Declaring the attributes public class BankAccount { private String accountNumber; private String customerName; private double balance; } 19/6 Defining the constructors public BankAccount(String accNo, String custName) { accountNumber = accNo; customerName = custName; balance = 0; } 19/7 Defining the methods To make a deposit  To withdraw money  To get the balance  To display a BankAccount object  19/8 Method to get the balance public double getBalance( ) { return balance; } 19/9 Methods that return a value Sometimes a method’s invocation should result in a value being sent back to the sender of the invoking message  When a method is expected to send a result back to the sender, it is said to return a value  Each method must define a return type, i.e the type of the value it sends back  19/10 Object instantiation a1.deposit(100); a2.deposit(300); a3.withdraw(20); a1 1024 a2 2106 a3 1024 1024 accountNumber: customerName: balance: … 2106 accountNumber: customerName: balance: “3210” “marvin” 100 “1245” “arthur” 19/37 Object instantiation a1.deposit(100); a2.deposit(300); a3.withdraw(20); a1 1024 a2 2106 a3 1024 1024 accountNumber: customerName: balance: … 2106 accountNumber: customerName: balance: “3210” “marvin” 100 “1245” “arthur” 300 19/38 Object instantiation a1.deposit(100); a2.deposit(300); a3.withdraw(20); a1 1024 a2 2106 a3 1024 1024 accountNumber: customerName: balance: … 2106 accountNumber: customerName: balance: “3210” “marvin” 80 “1245” “arthur” 300 19/39 Testing the BankAccount class Define a launcher class to test BankAccount  This class has a main( ) method  In the main method, we create instances of BankAccount and send messages to them  19/40 Testing // Test - create an account and display it BankAccount a1 = new BankAccount( "A10", "Smith"); System.out.println(a1.toString( ) ); • Observe the output • Note that statement System.out.println(a1) would produce the same effect 19/41 Testing // Test - make a deposit and display the account a1.deposit(200); System.out.println(a1); • Observe the output 19/42 Testing // Test - make an invalid deposit request and see // how the object handles it a1.deposit(-100); System.out.println(a1); • Observe how the object rejects the request • Better ways of handling error and exceptional conditions will be learnt later 19/43 Testing // Test - make a withdrawal a1.withdraw(100); System.out.println(a1); 19/44 Testing // Test - make an invalid withdrawal request a1.withdraw(-100); System.out.println(a1); 19/45 Testing // Test - make another invalid withdrawal request a1.withdraw(300); System.out.println(a1); 19/46 Testing // Test - get the balance double balance = a1.getBalance( ); System.out.println("balance: " + balance); • Note that balance in the statements above is a local variable of the main method 19/47 Testing // Test – display the account details a1.displayAccountDetails( ); 19/48 Class exercise - employees  Create and test a class representing an employee that – Has a name and the details associated with their pay – Allows users to update the amount of hours they should be paid, and pay them  Rules – Employees have a standard number of hours they work a week – They can work a standard week or a non-standard week 19/49 Defining the class Employee  To define the Employee class and to test it, we go through the following typical steps Sketch Define Define Define Define a model of the class the class header the attributes the constructors the methods 19/50 Next lecture Information hiding and encapsulation  Access modifiers  Class interfaces  javadoc  19/51 [...]... values are called the actual parameters or arguments  19/ 19 Parameter passing public void deposit(double amount) { … } formal parameters BankAccount b = new BankAccount("3210 4359", "B Gates"); b.deposit(150.55); actual parameters or arguments 19/ 20 Parameter passing double depositAmount = 55.55; b.deposit(depositAmount); main method depositAmount 55.5 19/ 21 Parameter passing double depositAmount = 55.55;... main method depositAmount deposit method 55.5 amount 19/ 22 Parameter passing double depositAmount = 55.55; b.deposit(depositAmount); main method depositAmount deposit method 55.5 amount 55.5 19/ 23 Method to withdraw money public void withdraw(double amount) { if (amount balance) { return; } balance = balance - amount; } 19/ 24 Overuse of return statements As with break statements... value by giving it the return type void  19/ 15 return in void methods There is no need to place a return statement inside a void method as it does not need to specify a return value  However, the return statement can be used in void methods to terminate the execution of the method at that point  The return statement in this case does not specify a return value  19/ 16 Parameter passing When we expect... arguments must be specified inside brackets  19/ 17 Parameter passing   The type of each argument and its identifier (the name by which it will be referred in the method) are needed to specify the expected arguments The order of the expected arguments and the actual values used in a method invocation must match public void deposit(double amount) { … } 19/ 18 Parameter passing The expected arguments... value or calculation it sends back  This is called the return value  19/ 11 Methods that return a value A return value is specified by a return statement  In the previous example, balance was the return value, so therefore, the value stored in the balance attribute of the object is sent back as the result of the method’s invocation  19/ 12 Methods that return a value Processing of a method terminates... invoked the method)  When a method terminates, processing in the program then continues at the call site  19/ 13 Method to make deposits public void deposit(double amount) { if (amount ... 19/ 19 Parameter passing public void deposit(double amount) { … } formal parameters BankAccount b = new BankAccount("3210 4359", "B Gates"); b.deposit(150.55); actual parameters or arguments 19/ 20... later 19/ 43 Testing // Test - make a withdrawal a1.withdraw(100); System.out.println(a1); 19/ 44 Testing // Test - make an invalid withdrawal request a1.withdraw(-100); System.out.println(a1); 19/ 45... the attributes the constructors the methods 19/ 50 Next lecture Information hiding and encapsulation  Access modifiers  Class interfaces  javadoc  19/ 51

Ngày đăng: 24/03/2016, 22:15

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN