Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 116 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
116
Dung lượng
448,54 KB
Nội dung
Basic Programming Course Methods and Containment Arrows Week 4,5,6 Date example • Develop a program that helps you keep track of daily One date is described with three pieces of information: a day, a month, and a Class year Date Data type - int day - int month - int year Property or field Method Define Class, Constructor and test Constructor public class Date { private int day; private int month; private int year; public Date(int day, int month, int year) { this.day = day; this.month = month; this.year = year; } } import junit.framework.*; public class TestDate extends TestCase { public void testConstrutor(){ new Date(5, 6, 2003); // denotes June 5, 2003 new Date(6, 6, 2003); // denotes June 6, 2003 new Date(23, 6, 2000); // denotes June 23, 2000 } GPS Location example • Develop a GPS navigation program for cars The physical GPS unit feeds the program with the current location Each such location consists of two pieces of information: the latitude and the longitude Class GPSLocation - double latitude - double longitude Data type Property or field Method Define Class, Constructor and test Constructor public class GPSLocation { private double latitude /* degrees */; private double longitude /* degrees */; public GPSLocation(double latitude, double longitude) { this.latitude = latitude; this.longitude = longitude; } } import junit.framework.*; public class TestGPSLocation extends TestCase { public void testConstrutor(){ new GPSLocation (33.5, 86.8); new GPSLocation (40.2, 72.4); new GPSLocation (49.0, 110.3); } Class A class is a generic description of a “thing” It has: Attribute: describes the “thing” Attributes = Properties = Information = Fields = Data = Variables Behavior: describes what the “thing” does Behaviors = Methods = Functions = Operations State: describes the state that “thing” belongs to A “thing” in a computer system ‘come alive – they have behavior Car example With a Car • Attributes which we concern: manufacturer, color, speed,… • Behaviors can happen with a car: how to start up, how to speed up, • States: – when a car in good shape, it is in use state, – When it is damaged, it becomes not workable state, Book example A Book in library has: • Attributes that we concern : name, author, publisher, price,… • Behaviors can modeled with this book: borrow, payback,… • States: – If it is ready to be borrow, it in ready state – if it is borrowed, it becomes busy state – when it is old, damaged and taken stock of, it is in out of stock (thanh lý) state –… Room example A Room in hotel has: • Attributes we concern: name, kind (may be: usual, deluxe, VIP,…), price per day,… • Behavior can happen with a room: rent,… • States: – If it is ready to be rent, it is in ready state – if it is rent, it becomes busy state – when it is damaged, it is in-correct (sửa chữa) state –… Point example A point in monitor has: • Attributes: x coordinate, which tells us where the pixel is in the horizontal direction, and y coordinate, which tells us where the pixel is located in the downwards vertical direction and color • Behaivors: – Computes how far some pixel is from the origin – Computes the distance between pixels,… • State: ? So, some real-life objects not have state 10 Solution 3.1.3: inThisCity() way • Method implementation public boolean inThisCity(String city) { return this.address.getCity().equals(city); }// class House pubilc String getCity(){ return this.city; } //class Address • Unit testing public 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.inThisCity("Brooklyn")); Assert.assertFalse(house1.inThisCity("Newton")); } 102 Solution 3.1.3: inThisCity() way public boolean inThisCity(String city){ return this.address.inThisCity(city); }// class House public boolean inThisCity(String thatCity) { return this.city.equals(thatCity); }//class Address 103 Solution 3.1.3: sameCity() way • Method implementation public boolean sameCity(House that) { return this.address.getCity().equals(that.address.getCity()); } • Unit testing public 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)); } 104 Solution 3.1.3: sameCity() way public boolean sameCity(House that){ return this.address.sameCity(that.address); } //class House public boolean sameCity(Address that) { return this.city.equals(that.city); }// class Address Back 105 Ranch • A ranch is a large farm used for raising animals, especially cattle, horses or sheep 106 Back Colonial • 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 107 Cape • A cape is a large piece of land that sticks out into the sea from the coast 108 Solution 4.1.1 109 currentBook() • Method implementation public boolean currentBook() { int currentYear = 2004; return (this.year == currentYear) || (this.year == currentYear - 1); } • Unit testing public void testCurrentBook() { Author felleisen = new Author("Matthias Felleisen", 1960); Book htdch = new Book(felleisen, "How to Design Class Hierarchies", 0.0, 2004); Assert.assertTrue(htdch.currentBook()); } Author friedman = new Author("Daniel P Friedman", 1939); Book aljafp = new Book(friedman, "A Little Java, A Few Pattern", 25.9, 1998); Assert.assertFalse(aljafp.currentBook()); 110 currentAuthor() • Method implementation public boolean currentAuthor() { // in class Book.java return this.author.currentAuthor(); } public boolean currentAuthor() { // in class Author.java return this.birthYear >= 1940; } • Unit testing public void testCurrentAuthor() { Author felleisen = new Author("Matthias Felleisen", 1960); Book htdch = new Book(felleisen, "How to Design Class Hierarchies", 0.0, 2004); Assert.assertTrue(htdch.currentAuthor()); } Author friedman = new Author("Daniel P Friedman", 1939); Book aljafp = new Book(friedman, "A Little Java, A Few Pattern", 25.9, 1998); Assert.assertFalse(aljafp.currentAuthor()); 111 thisAuthor() • Method implementation public boolean thisAuthor(Author that) { // in class Book.java return this.author.thisAuthor(that); } public boolean thisAuthor(Author that) { // in class Author.java return (this.name.equals(that.name)) && (this.birthYear == that.birthYear); } • Unit testing public void testThisAuthor() { Author felleisen = new Author("Matthias Felleisen", 1960); Book htdch = new Book(felleisen, "How to Design Class Hierarchies", 0.0, 2004); Assert.assertTrue(htdch.thisAuthor(felleisen)); } Author friedman = new Author("Daniel P Friedman", 1939); Assert.assertFalse(htdch.thisAuthor(friedman)); 112 sameAuthor() • Method implementation public boolean sameAuthor(Book that) { // in class Book.java return this.author.thisAuthor(that.author); } • Unit testing public void testSameAuthor() { Author felleisen = new Author("Matthias Felleisen", 1960); Book htdch = new Book(felleisen, "How to Design Class Hierarchies", 0.0, 2004); Book htdp = new Book(felleisen, "How to Design Programs", 0.0, 2002); Assert.assertTrue(htdch.sameAuthor(htdp)); } Author friedman = new Author("Daniel P Friedman", 1939); Book aljafp = new Book(friedman, "A Little Java, A Few Pattern", 25.9, 1998); Assert.assertFalse(aljafp.sameAuthor(htdch)); 113 sameGeneration() • Method implementation public boolean sameGeneration(Book that) { // in class Book.java return this.author.sameGeneration(that.author); } public boolean sameGeneration(Author that) { // in class Author.java return Math.abs(this.birthYear - that.birthYear) = thatPrecipitation; } } public boolean recordDay() { return ! this.today.within(this.record); } public class TemperatureRange { private double low; private double high; … } public boolean within(TemperatureRange that) { return (this.low >= that.low) && (this.high