Method Templatepublic class CoffeeReceipt { private String kind; private double pricePerPound; private double weight; public CoffeeReceiptString kind, double pricePerPound, double weight
Trang 1Section 02
Methods for classes
Basic Programming Course
Week 3
Trang 2design 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
Trang 3At a Specialty Coffee Seller
Trang 4Coffee example
coffee seller from a receipt that includes the kind of coffee, the unit price , and
Trang 6Property or field
Method Data type
Class
Trang 7Method Template
public class CoffeeReceipt {
private String kind;
private double pricePerPound;
private double weight;
public CoffeeReceipt(String kind, double pricePerPound, double weight) {
}
Add a contract,
a purpose statement
METHOD SIGNATURE
Trang 8Method 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() {
returnthis pricePerPound * this weight;
}
}
Trang 9public 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);
Trang 10New Requirement
design
Trang 11Class Diagram
• Q: Develop some examples for weighsMore()
Trang 12// should produce → false
2 new CoffeeReceipt("Ethiopian", 8.00, 1000 ) weighsMore ( 1000 )
3 new CoffeeReceipt(“Columbian“,
9.50, 200 ) weighsMore ( 100 )
Q: Write a template for weighsMore() and implement it
Trang 13weighsMore() Template
public class CoffeeReceipt {
private String kind;
private double pricePerPound;
private double weight;
Trang 14weighsMore() 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()
Trang 15Unit Test for weighsMore()
public class CoffeeReceiptTest extends TestCase {
…
public void testWeighsMore() {
Assert.assert False (new CoffeeReceipt("Hawaiian Kona“, 15.95, 100).weighsMore(200));
Assert.assert False (new CoffeeReceipt("Ethiopian", 8.00, 1000).weighsMore(1000)); Assert.assert True (new CoffeeReceipt(“Columbian“,
9.50, 200).weighsMore(100));
}
}
Trang 17Another New Requirement
than some other coffee (by comparing the price per pound)
• Q: Supplement the class diagram to include this requirement to the current design
Trang 18Class Diagram
• Q: Develop some examples for isCheaperThan()
Trang 191 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
Trang 20public 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()
Trang 21Unit Test for IsCheaperThan()
public class CoffeeReceiptTest extends TestCase {
…
public void testIsCheaperThan() {
Assert.assertTrue(new CoffeeReceipt("Hawaiian Kona“, 15.95, 100)
}
Trang 22Another New Requirement
less than some other coffee receipt
Trang 23Class Diagram
Trang 241. new CoffeeReceipt("Hawaiian Kona", 15.95, 100)
.costsLessThan(new CoffeeReceipt("Ethiopian“, 8.00, 1000))// should produce
→ false
Trang 25public class CoffeeReceipt {
private String kind;
private double pricePerPound;
private double weight;
…
public boolean costsLessThan( CoffeeReceipt that ) {
return this sellingCost() < that sellingCost() ;
}
}
Trang 26Unit Test for costsLessThan()
public class CoffeeReceiptTest extends TestCase {
…
public void testCostsLessThan() {
CoffeeReceipt hk = new CoffeeReceipt("Hawaiian Kona", 15.95, 100); CoffeeReceipt e = new CoffeeReceipt("Ethiopian", 8.00, 1000);
CoffeeReceipt cs = new CoffeeReceipt("Colombian Supreme ", 9.50, 1700);
Trang 27Java data type
we represent primitive forms of information Here we use four of them: int,
double, boolean, and String
Trang 30Character type
Java represent a Unicode character (2 bytes)
Name size Range Default value
char 2 bytes ‘\u0000’… ’\uffff’ null
Q: Distinguish char type and String type ?
A: char c = “t’;
String s = “tin hoc”;
Trang 31Boolean type
Trang 32Exercises
Trang 33Exercise 3.1.2 (HTDCH)
• Study this class definition:
public class Image {
private int width; // pixels
private int height; // pixels
private String fileName;
private String quality; // low, medium, or high
public Image(int width, int height, String fileName, String quality) {
• And here are some examples
new Image(10, 5, "small.gif", "low");
new Image(120, 200, "med.gif", "low");
new Image(1000, 1200, "large.gif", "high");
Trang 34Exercise 3.1.2 (HTDCH)(cont.)
Develop the following methods for this class:
some other image.
Solutions
Trang 35Extended Exercises (Lab hours)
Trang 36Exercise 3.1.6
• Modify the CoffeeReceipt class so that sellingCost() takes into account bulk discounts:
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 If the sale is for less than 5,000 pounds, there is no discount For sales of 5,000 pounds
to 20,000 pounds, the seller grants a discount of 10% For sales of 20,000 pounds or more, the discount is 25% .
Don't forget to adapt the examples, too.
Trang 37Exercise 3.1.7
• Design the method sizeString() for the Image class It produces one of three strings, depending on the number of pixels in the image:
• “small” for images with 10,000 pixels or fewer;
• “medium” for images with between 10,001 and 1,000,000 pixels;
• “large” for images that are even larger than that.
Trang 38Prepare for next week
How to design class hierarchy
• III Fun Methods
Trang 39& Do Exercis e
Trang 40Solution 3.1.2: isPortrait()
• Method implementation
public boolean isPortrait() {
return this.width < this.height;
}
• Unit testing
public void testIsPortrait() {
Assert.assertFalse(new Image(5, 10, "small.gif", "low“).isPortrait()); Assert.assertFalse(new Image(120, 200, "med.gif", "low").isPortrait()); Assert.assertTrue(new Image(1200, 1000, "large.gif", "high").isPortrait());
}
Trang 41Solution 3.1.2: size()
• Method implementation
public int size() {
return this.height * this.width;
}
• Unit testing
public void testSize() {
Assert.assertEquals(new Image(5, 10, "small.gif", "low").size(), 50); Assert.assertEquals(new Image(120, 200, "med.gif", "low").size(),
24000);
Assert.assertEquals(new Image(1200, 1000, "large.gif", "high").size(),
1200000);
}
Trang 42Solution 3.1.2: isLarger()
• Method implementation
public boolean isLarger(Image that) {
return this.size() > that.size();
}
• Unit testing
public void testIsLarger() {
Image img1 = new Image(5, 10, "small.gif", "low");
Image img2 = new Image(120, 200, "med.gif", "low");
Image img3 = new Image(1200, 1000, "large.gif", "high");
Assert.assertFalse(img1.isLarger(img2));
Assert.assertTrue(img3.isLarger(img2));