1. Trang chủ
  2. » Giáo Dục - Đào Tạo

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

42 98 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 42
Dung lượng 399,94 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Method Templatepublic class CoffeeReceipt { private String kind; private double pricePerPound; private double weight; public CoffeeReceiptString kind, double pricePerPound, double weight

Trang 1

Section 02

Methods for classes

Basic Programming Course

Week 3

Trang 2

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 3

At a Specialty Coffee Seller

Trang 4

Coffee example

coffee seller from a receipt that includes the kind of coffee, the unit price , and

Trang 6

Property or field

Method Data type

Class

Trang 7

Method 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 8

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() {

returnthis pricePerPound * this weight;

}

}

Trang 9

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);

Trang 10

New Requirement

design

Trang 11

Class 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 13

weighsMore() Template

public class CoffeeReceipt {

private String kind;

private double pricePerPound;

private double weight;

Trang 14

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()

Trang 15

Unit 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 17

Another 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 18

Class Diagram

Q: Develop some examples for isCheaperThan()

Trang 19

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

Trang 20

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()

Trang 21

Unit Test for IsCheaperThan()

public class CoffeeReceiptTest extends TestCase {

public void testIsCheaperThan() {

Assert.assertTrue(new CoffeeReceipt("Hawaiian Kona“, 15.95, 100)

}

Trang 22

Another New Requirement

less than some other coffee receipt

Trang 23

Class Diagram

Trang 24

1. new CoffeeReceipt("Hawaiian Kona", 15.95, 100)

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

→ false

Trang 25

public class CoffeeReceipt {

private String kind;

private double pricePerPound;

private double weight;

public boolean costsLessThan( CoffeeReceipt that ) {

return this sellingCost() < that sellingCost() ;

}

}

Trang 26

Unit 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 27

Java data type

we represent primitive forms of information Here we use four of them: int,

double, boolean, and String

Trang 30

Character 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 31

Boolean type

Trang 32

Exercises

Trang 33

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) {

• 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 34

Exercise 3.1.2 (HTDCH)(cont.)

Develop the following methods for this class:

some other image.

Solutions

Trang 35

Extended Exercises (Lab hours)

Trang 36

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.

Trang 37

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.

Trang 38

Prepare for next week

How to design class hierarchy

III Fun Methods       

Trang 39

& Do Exercis e

Trang 40

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());

}

Trang 41

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);

}

Trang 42

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));

Ngày đăng: 29/03/2021, 10:41

TỪ KHÓA LIÊN QUAN

w