Mutable immutable (lập TRÌNH cơ bản SLIDE)

59 27 0
Mutable immutable (lập TRÌNH cơ bản SLIDE)

Đ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

A2 Programming Course Section 02 Methods for Classes week 3, 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 year Class Date Data type - int day - int month - int year Property or field Method Define Class, Constructor and test Constructor class Date { int day; int month; int year; 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 Property or field Data type Method Define Class, Constructor and test Constructor class GPSLocation { double latitude /* degrees */; double longitude /* degrees */; 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 Test mutableTransfer Test transfer method import junit.framework.*; public class TestStudent extends TestCase { … public void testMutableTransfer(){ Student aStudent1 = new Student("st2", "Wilson", "Fillip", "Harper"); Student aStudent2 = new Student("st3", "Woops", "Helen", "Flatt"); aStudent1.muatbleTransfer("Lee"); assertEquals("Lee“,aStudent1.getTeacher()); aStudent2.muatbleTransfer("Flister"); assertEquals("Flister“,aStudent2.getTeacher()); } } 45 Class diagram Class Student + + + + String String String String id firstName lastName teacher + + + + + String check(String thatTeacher) Student transfer(String thatTeacher) void mutableTransfer(String thatTeacher) boolean equals(Student that) String getTeacher() Property or field Data type Method 46 Exercise 5.1.4 (HTDP) • Develop whatKind The function consumes the coefficients a, b, and c of a quadratic equation It then determines whether the equation is degenerate and, if not, how many solutions the equation has The function produces one of four symbols: 'degenerate, 'two, 'one, or 'none Solution 47 Exercise 4.4.1 and 4.4.3 (HTDP) • • Information about the transaction in bank includes customer name, and deposit amount and maturity(computed in year) Develop the function interest It consumes a deposit amount and produces the actual amount of interest that the money earns in a year The bank pays a flat 4% for deposits of up to $1,000, a flat 4.5% per year for deposits of up to $5,000, and a flat 5% for deposits of more than $5,000 • Some credit card companies pay back a small portion of the charges a customer makes over a year One company returns 25% for the first $500 of charges, Solution 50% for the next $1000 (that is, the portion between $500 and $1500), 75% for the next $1000 (that is, the portion between $1500 and $2500), and 1.0% for everything above $2500 Define the function payback, which consumes a charge amount and computes the corresponding pay-back amount Solution 48 Prepare for next week How to design class hierarchy • I  The Varieties of Data  Compound Data: Class Containment • III  Fun Methods     4  Methods and Containment Arrows 49 Relax… & Do Exercise 50 Solution 2.3.3 (HTDP) • Class definition public class MovieTheatre { private double ticketPrice; private double costForPerformance; private double costPerAttendee; public MovieTheatre(double ticketPrice, double costForPerformance, double costPerAttendee) { this.ticketPrice = ticketPrice; this.costForPerformance = costForPerformance; this.costPerAttendee = costPerAttendee; } public double cost(int numAttendee){ return this.costForPerformance + this.costPerAttendee * numAttendee; } public double revenue(int numAttendee){ return this.ticketPrice * numAttendee; } public double totalProfit(int numAttendee){ return this.revenue(numAttendee)- this.cost(numAttendee); } } 51 Solution 2.3.3 (HTDP) (cont) • Using test public void testTotalProfit(){ MovieTheatre aMovie1 = new MovieTheatre(5.0, 20.0, 0.15); MovieTheatre aMovie2 = new MovieTheatre(6.0, 40.0, 0.1); MovieTheatre aMovie3 = new MovieTheatre(7.0, 50.0, 0.2); assertEquals(465.0, aMovie1.totalProfit(100), 0.001); assertEquals(550.0, aMovie2.totalProfit(100), 0.001); assertEquals(630.0, aMovie3.totalProfit(100), 0.001); } Back 52 Solution 3.3.5 (HTDP) • class definition public class Rocket { private String model; private String manufacturer; public Rocket(String model, String manufacturer) { this.model = model; this.manufacturer = manufacturer; } public double speed(double time){ return 10 * time; } public double height(double time){ return 0.5 * this.speed(time) * time; } } • Using test public void testHeight(){ Rocket aRocket1 = new Rocket("Columbia", "America"); Rocket aRocket2 = new Rocket("Victory", "England"); Rocket aRocket3 = new Rocket("Win", "Vietnam"); Back 53 assertEquals(500.0 , aRocket1.height(10), 0.001); Solution 5.1.4 (HTDP) public class Quadratic { private double a; private double b; private double c; public Quadratic(double a, double b, double c) { this.a = a; this.b = b; this.c =c; } public double computeDelta(){ return this.b * this.b - * this.a * this.c; } public String whatKind(){ double delta = this.computeDelta(); if (this.a == 0) return "degenerate"; if (delta < 0) return "none"; if (delta == 0) return "one solution"; return "two solution"; 54 Solution 5.1.4 (HTDP) (cont) • Using test public void testWhatKind(){ Quadratic q1= new Quadratic (0.0, 1.0, 2.0); Quadratic q2= new Quadratic (2.0, 1.0, 2.0); Quadratic q3= new Quadratic (1.0, 2.0, 1.0); Quadratic q4= new Quadratic (2.0, 3.0, 1.0); assertEquals("degenerate”, q1.whatKind()); assertEquals(“none”, q2.whatKind()); assertEquals(“one solution”, q3.whatKind()); assertEquals("two solution“, q4.whatKind()); } Back 55 Solution 4.4.1 (HTDP) • Class definition public class Transaction { private String customerName; private double depositeAmount; private int maturity; public Transaction(String customerName, double depositeAmount, int maturity) { this.customerName = customerName; this.depositeAmount = depositeAmount; this maturity = maturity; } public double interest(){ if (this.depositeAmount

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

Mục lục

    Define Class, Constructor and test Constructor

    Define Class, Constructor and test Constructor

    The public or private modifiers for attribute and method

    Discuss more: equals method

    Discuss more: getSales method

    Discuss more: equals method

    Discuss more: getTeacher method

    Prepare for next week

Tài liệu cùng người dùng

Tài liệu liên quan