Method Templatepublic class CoffeeReceipt { private String kind; private double pricePerPound; private double weight; public CoffeeReceiptString kind, double pricePerPound, double weight
Trang 1Methods for Classes
Week 5
HOW TO DESIGN CLASS
HIERARCHIES
Basic Java Programming
Course material developed by Mai Anh Tho (tho@hcmuaf.edu.vn)
Trang 2• 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
Trang 3At a Specialty Coffee Seller
Trang 4Problem Statement
• 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
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() {
return this.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
• 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
Trang 11Class Diagram
• Q: Develop some examples for weighsMore()
Trang 12// should produce → true
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“,
Trang 17Another 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
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
• The coffee shop customers want to
determine whether one coffee receipt
costs less than some other coffee receipt
Trang 23+ boolean weighsMore(double amount)
+ boolean isCheaperThan(CoffeeReceipt that)+ boolean costsLessThan(CoffeeReceipt that)
Class 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 27Exercise 3.1.2
• Study this class definition:
public class Image {
private int height; // pixels
private int width; // pixels
private String fileName;
private String quality; // low, medium, or high
public Image(int height, int width, String fileName, String quality) {
• And here are some examples
new Image(5, 10, "small.gif", "low");
new Image(120, 200, "med.gif", "low");
new Image(1200, 1000, "large.gif", "high");
Trang 28Exercise 3.1.2 (cont.)
Develop the following methods for this class:
1 isPortrait, which determines whether the
image is taller than wider;
2 size, which computes how many pixels
the image contains;
3 isLarger, which determines whether one
image contains more pixels than some
other image.
Solutions
Trang 29Exercise 3.1.3
Develop a Java class called House for this problem:
Develop a "real estate assistant'' program The "assistant'' helps the real estate agent locate houses of interest for clients The information about a house includes its kind, the number of rooms, its address, and the asking price An address consists of a house number, a street name, and a city Represent the following examples using your classes:
1 Ranch, 7 rooms, $375,000, 23 Maple Street, Brookline
2 Colonial, 9 rooms, $450,000, 5 Joye Road, Newton
3 Cape, 6 rooms, $235,000, 83 Winslow Road, Waltham
Develop the following methods for the class House:
• hasMoreRooms, which determines whether one house has more rooms
than some other house;
• inThisCity, which checks whether the advertised house is in some given
city (assume we give the method a city name);
• sameCity, which determines whether one house is in the same city as
some other house
Don't forget to test these methods. Solutions
Trang 30day's run .For each entry, the program should compute
how fast the runner ran in minutes per mile .Develop a
method that computes the pace for a daily entry.
Solutions
Trang 31Exercise 3.1.5
• A runner's log refers to Dates (see the figure) and a natural question concerning comparing dates is when one occurs earlier than another one Develop a method that determines whether one date occurs earlier than another date.
• Hint: The first possibility is that the first date is in the year preceding the other Next, if the years are the same, the
month in the first date is before the month in the second
date Finally, if both the year and the month values are the same, the date in the first date is before the day in the
Solutions
Trang 32Extended Exercises
(Lab hours)
Trang 33Exercise 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 34Exercise 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 35Relax &
GOOD JOB !
Trang 36Solution 3.1.2
Trang 37Solution 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 38Solution 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 39Solution 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));
}
Back
Trang 40Solution 3.1.3
Trang 41Solution 3.1.3: House
public class House {
private String kind;
private int nRooms;
private Address address;
private int price;
public House(String kind, int nRooms, Address address, int price) {
Trang 42Solution 3.1.3: Address
public class Address {
private String houseNumber;
private String street;
private String city;
public Address(String houseNumber, String street, String city) {
Trang 43Solution 3.1.3: hasMoreRooms()
• Method implementation
public boolean hasMoreRooms(House that) {
return this.nRooms > that.nRooms;
}
• Unit testing
public void testHasMoreRooms() {
House house1 = new House("Ranch", 7,
new Address("23", "Mapple Street", "Brooklyn"), 375000);
House house2 = new House("Colonial", 9,
new Address("5", "Jove Road", "Newton"), 450000);
House house3 = new House("Cape", 6,
new Address("83", "Winslow Road", "Waltham"), 235000);
Assert.assertFalse(house1.hasMoreRooms(house2));
Assert.assertTrue(house2.hasMoreRooms(house3));
}
Trang 44public void testThisCity() {
House house1 = new House("Ranch", 7,
new Address("23", "Mapple Street", "Brooklyn"), 375000);
House house2 = new House("Colonial", 9,
new Address("5", "Jove Road", "Newton"), 450000);
House house3 = new House("Cape", 6,
new Address("83", "Winslow Road", "Waltham"), 235000);
Assert.assertTrue(house1.thisCity("Brooklyn"));
Assert.assertFalse(house1.thisCity("Newton"));
}
Trang 45public void testSameCity() {
House house1 = new House("Ranch", 7,
new Address("23", "Mapple Street", "Brooklyn"), 375000);
House house2 = new House("Colonial", 9,
new Address("5", "Jove Road", "Newton"), 450000);
House house3 = new House("Cape", 6,
new Address("83", "Winslow Road", "Waltham"), 235000);
Assert.assertTrue(house1.sameCity(house1));
Assert.assertFalse(house1.sameCity(house2));
}
Back
Trang 46Solution 3.1.4: pace()
• Method implementation
public double pace() {
return this.durationInMinutes / this.distance;
}
• Unit testing
public void testPace() {
Entry entry = new Entry(new Date(5, 6, 2004), 5.3, 27, "good");
Trang 47Solution 3.1.5
Trang 48Method Implementation
public class Date {
private int day;
private int month;
private int year;
public Date(int day, int month, int year) { … }
public boolean earlierThan(Date that) {
if (this.year < that.year) return true;
if (this.year > that.year) return false;
if (this.month < that.month) return true;
if (this.month > that.month) return false;
if (this.day < that.day) return true;
return false;
}
}
Trang 49Unit Testing
public void testEarlierThan() {
Date date1 = new Date(30, 6, 2003);
Date date2 = new Date(1, 1, 2004);
Date date3 = new Date(1, 12, 2004);
Date date4 = new Date(15, 12, 2004);
Date date5 = new Date(31, 12, 2004);
Trang 50• A ranch is a large farm used for raising
animals, especially cattle, horses or
sheep.
Back
Trang 51• A colonial building or piece of furniture was built or made in a style that was popular in American in the 17th and 18th
centuries.
Trang 52• A cape is a large piece of land that sticks out into the sea from the coast.