Tải bản đầy đủ (.pptx) (42 trang)

Methods for classes (lập TRÌNH cơ bản SLIDE)

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (291.29 KB, 42 trang )

Basic Programming Course

Section 02

Methods for classes
Week 3


Methods



Designing a good data representation for a problem is the first step toward the
design of a good program, but it isn't enough. We also need functions that
compute results from some given data. In an object-oriented language, functions
are implemented as METHODS. Designing methods proceeds in the fashion as
designing functions, which we are familiar with from How to Design Programs.


At a Specialty Coffee Seller


Coffee example



Develop a program that computes the cost of selling bulk coffee at a specialty
coffee seller from a receipt that includes the kind of coffee, the unit price, and
the total amount (weight) sold



Examples

1.
2.
3.

100 pounds of Hawaiian Kona at $15.95/pound
→ $1,595.00
1,000 pounds of Ethiopian coffee at $8.00/pound
→ $8,000.00
1,700 pounds of Colombian Supreme at $9.50/pound
→ 16,150.00


Class Diagram
Class

Data type

CoffeeReceipt
- String kind
- double pricePerPound
- double weight

Property or field

+ ??? sellingCost(???)
Method



Method Template
public class CoffeeReceipt {
private String kind;
private double pricePerPound;
private double weight;

public CoffeeReceipt(String kind, double pricePerPound, double weight) {
this.kind = kind;
this.pricePerPound = pricePerPound;
this.weight = weight;
}
// to compute the total cost of this coffee purchase [ in cents]
public ???? sellingCost(???) {
… this.kind …

Add a contract,
a purpose statement

… this.pricePerPound …
… this.weight …
}
}

METHOD SIGNATURE


Method Implementation
public class CoffeeReceipt {
private String kind;
private double pricePerPound;

private double weight;

public CoffeeReceipt(String kind, double pricePerPound, double weight) {
this.kind = kind;
this.pricePerPound = pricePerPound;
this.weight = weight;
}
// to compute the total cost of this coffee purchase [ in cents]
public double sellingCost() {
return this.pricePerPound * this.weight;
}
}


Testing
Instance of class
public class TestCoffeeReceipt extends TestCase {
public void testConstructor() {
new CoffeeReceipt("Hawaiian Kona", 15.95, 100);
new CoffeeReceipt("Ethiopian", 8.00, 1000);
new CoffeeReceipt("Colombian Supreme ", 9.50, 1700);
}

public void testSellingCost() {
CoffeeReceipt hk = new CoffeeReceipt("Hawaiian Kona", 15.95, 100);
Assert.assertEquals(hk.sellingCost(), 1595.00, 0.001); //0.00

CoffeeReceipt e = new CoffeeReceipt("Ethiopian", 8.00, 1000);
Assert.assertEquals(e.sellingCost(), 8000.00, 0.001);


CoffeeReceipt cs = new CoffeeReceipt("Colombian Supreme ", 9.50, 1700);
Assert.assertEquals(cs.sellingCost(), 16150.00, 0.001);
}
}


New Requirement



Requirement: The coffee shop owner may wish to find out whether a coffee sale
involved more than a certain number of pounds.



Q: Supplement the class diagram to include this requirement to the current
design


Class Diagram
CoffeeReceipt
- String kind
- double pricePerPound
- double weight
+ double sellingCost()
+ ???? weighsMore(???)



Q: Develop some examples for weighsMore()



Examples

1.

new CoffeeReceipt("Hawaiian Kona“, 15.95, 100).weighsMore(200)

// should produce → false
2. new CoffeeReceipt("Ethiopian", 8.00, 1000).weighsMore(1000)
// should produce →false
3. new CoffeeReceipt(“Columbian“,
9.50, 200).weighsMore(100)
// should produce → true

Q: Write a template for weighsMore() and implement it


weighsMore() Template
public class CoffeeReceipt {
private String kind;
private double pricePerPound;
private double weight;



public ???? weighsMore(???) {
… this.kind …
… this.pricePerPound …
… this.weight …

}
}


weighsMore() Implementation
public class CoffeeReceipt {
private String kind;
private double pricePerPound;
private double weight;



public boolean weighsMore(double amount) {
return this.weight > amount;
}
}



Q: Write a unit test for weighsMore()


Unit Test for weighsMore()
public class CoffeeReceiptTest extends TestCase {


public void testWeighsMore() {
Assert.assertFalse(new CoffeeReceipt("Hawaiian Kona“, 15.95, 100).weighsMore(200));
Assert.assertFalse(new CoffeeReceipt("Ethiopian", 8.00, 1000).weighsMore(1000));
Assert.assertTrue(new CoffeeReceipt(“Columbian“,

9.50, 200).weighsMore(100));
}
}


Class Diagram

CoffeeReceipt
- String kind
- double pricePerPound
- double weight
+ double sellingCost()
+ boolean weighsMore(double amount)


Another New Requirement



Requirement: A coffee shop customers want to determine whether one coffee is cheaper
than some other coffee (by comparing the price per pound)



Q: Supplement the class diagram to include this requirement to the current design


Class Diagram

CoffeeReceipt

- String kind
- double pricePerPound
- double weight
+ double sellingCost()
+ boolean weighsMore(double amount)
+ boolean isCheaperThan(CoffeeReceipt that)



Q: Develop some examples for isCheaperThan()


Examples
1.

new CoffeeReceipt("Hawaiian Kona“, 15.95, 100)
.isCheaperThan(new CoffeeReceipt("Ethiopian", 20.00, 100))
// should produce→ true

2.

new CoffeeReceipt("Hawaiian Kona“, 15.95, 200)
.isCheaperThan(new CoffeeReceipt("Ethiopian", 15.95, 100))
// should produce → false

3.

new CoffeeReceipt("Hawaiian Kona“, 15.95, 300)
.isCheaperThan(new CoffeeReceipt("Ethiopian", 8.00, 100))
// should produce→ false


Q: Write a template for isCheaperThan() and implement it


IsCheaperThan()
public class CoffeeReceipt {
private String kind;
private double pricePerPound;
private double weight;



public boolean isCheaperThan(CoffeeReceipt that) {
return this.pricePerPound < that.pricePerPound;
}
}



Q: Write a unit test for isCheaperThan()


Unit Test for IsCheaperThan()
public class CoffeeReceiptTest extends TestCase {


public void testIsCheaperThan() {
Assert.assertTrue(new CoffeeReceipt("Hawaiian Kona“, 15.95, 100)
.isCheaperThan(new CoffeeReceipt("Ethiopian", 20.00, 100));


Assert.assertFalse(new CoffeeReceipt("Hawaiian Kona“, 15.95, 200)
.isCheaperThan(new CoffeeReceipt("Ethiopian", 15.95, 100)) ;

Assert.assertFalse(new CoffeeReceipt("Hawaiian Kona“, 15.95, 300)
.isCheaperThan(new CoffeeReceipt("Ethiopian", 8.00, 100));
}
}


Another New Requirement



The coffee shop customers want to determine whether one coffee receipt costs
less than some other coffee receipt


Class Diagram

CoffeeReceipt
- String kind
- double pricePerPound
- double weight
+ double sellingCost()
+ boolean weighsMore(double amount)
+ boolean isCheaperThan(CoffeeReceipt that)
+ boolean costsLessThan(CoffeeReceipt that)


Examples

1.

new CoffeeReceipt("Hawaiian Kona", 15.95, 100)
.costsLessThan(new CoffeeReceipt("Ethiopian“, 8.00, 1000))
// should produce
→ true

2.

new CoffeeReceipt("Ethiopian", 8.00, 1000).costsLessThan(new CoffeeReceipt("Ethiopian", 8.00, 1000))
//should produce
→ false

3.

new CoffeeReceipt(" Colombian Supreme", 9.50, 1700)
.costsLessThan(new CoffeeReceipt("Ethiopian“, 8.00, 1000))
// should produce
→ false


costsLessThan()
public class CoffeeReceipt {
private String kind;
private double pricePerPound;
private double weight;



public boolean costsLessThan(CoffeeReceipt that) {

return this.sellingCost() < that.sellingCost();
}
}


×