Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 42 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
42
Dung lượng
399,94 KB
Nội dung
Basic Programming Course Section 02 Methods for classes Week 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 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 Integer type Name Size Range Default value byte byte -128……………… 127 short bytes -32,768…………….32,767 int bytes -2,147,483,648…….2,147,483,648 long bytes -9,223,372,036,854,775,808 9,223, 372,036,854,775,808 0L Decimal type Name Size Range Default value float bytes -3,402,923,47E+38………3,402,923, 0.0f 47E+38 double bytes -1,797693134,862,315,07E+308…1, 797693134,862,315,07E+308 0.0d Character type Java represent a Unicode character (2 bytes) Name size Range Default value char bytes ‘\u0000’… ’\uffff’ null Q: Distinguish char type and String type ? A: char c = “t’; String s = “tin hoc”; Boolean type Name Size Range Default value boolean bit false, true false Exercises • • Part I: From 2.1.1 to 2.1.4 (HTDCL) Part III: From 3.1.1 to 3.1.2 (HTDCL) Exercise 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) { this.height = height; this.width = width; this.fileName = fileName; this.quality = quality; } } • And here are some examples new Image(10, 5, "small.gif", "low"); new Image(120, 200, "med.gif", "low"); Exercise 3.1.2 (HTDCH)(cont.) Develop the following methods for this class: isPortrait, which determines whether the image is taller than wider; size, which computes how many pixels the image contains; isLarger, which determines whether one image contains more pixels than some other image Solutions Extended Exercises (Lab hours) Exercise 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 Exercise 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 Prepare for next week How to design class hierarchy • I The Varietes of Data Compound Data: Class Containment • III Fun Methods 4 Methods and Containment Arrows Relax… & Do Exercise Solution 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()); } Solution 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); } Solution 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 ... 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 Prepare for next week How... 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,... data type • Like Scheme, Java provides a number of built-in atomic forms of data with which we represent primitive forms of information Here we use four of them: int, double, boolean, and String