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

Methods and containment in unions (lập TRÌNH cơ bản SLIDE)

176 116 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

Cấu trúc

  • Slide 1

  • Slide 2

  • Managing Inventory

  • Data definition

  • Class diagram

  • Define classes an constructors (cont)

  • Define classes and constructors

  • Test Constructor

  • toString() method

  • Getter in classes Toy

  • Employee problem (again)

  • Data definition

  • Class diagram

  • Define classes an constructors (cont)

  • Define classes and constructors

  • toString() method

  • toString () in Employee

  • Test Constructor

  • Slide 19

  • Managing a Runner’s Logs Example

  • Data definition

  • Class diagram

  • Define classes an constructors (cont)

  • Define classes and constructors

  • toString() method

  • toString () in Entry

  • toString() in Date

  • Test Constructor

  • Recall restaurant example

  • Class diagram

  • Data definition

  • Define classes and constructors

  • Define classes and constructors (cont)

  • toString() method

  • toString() in Restaurant

  • toString () in Intersection

  • Test Constructor

  • Exercises

  • Prepare for next week

  • Slide 40

  • Recall Inventory problem

  • Class diagram

  • Add methods to the Class Diagram

  • Java template for Toy

  • Java template for Inventory

  • Java template for MTInventory

  • Java template for Cons

  • Slide 48

  • Q

  • howMany for Inventory

  • howMany() for MTInventory and Cons

  • Test howMany()

  • More requirement

  • contains for Inventory

  • Contains for MTInventory and Cons

  • Test contains()

  • More requirement

  • isBellow for Inventory

  • isBellow() for MTInventory and Cons

  • Test isBellow()

  • Slide 61

  • New requiremqnt

  • raisePrice for Inventory- Immutable version

  • raisePrice() for MTInventory and Cons

  • New version

  • Test raisePrice()

  • raisePrice - mutable version

  • raisePrice for Inventory- mutable version

  • raisePrice() for MTInventory and Cons

  • setPrice in Toy class

  • Test raisePrice()

  • Review “Overriding” and “Overloading”

  • Slide 73

  • Recall employee problem

  • Class diagram

  • Add methods Class Diagram

  • Java template for Employee

  • Java template for ALoEmployee

  • Java template for MTLoEmployee

  • Java template for ConsEmployee

  • Q

  • wages for ALoEmployee

  • Examples to test wages()

  • Define classes an constructors

  • Define classes an constructors (cont)

  • wages() in MTLoEmployee

  • wages() in ConsEmployee

  • wage() for Employee

  • Test wages()

  • Recall the problem of tracking a runner’s workouts

  • Class diagram for a runner’s log

  • Add methods to the runner’s log Class Diagram

  • Java template for Date

  • Java template for Entry

  • Java template for ALog

  • Java template for MTLog

  • Java template for ConsLog

  • Examples for a runner’s log

  • Q

  • miles() for ALog

  • Examples to test miles()

  • miles() in MTLog

  • miles() in ConsLog

  • getDistance() in Entry

  • Extension of the runner’s log problem

  • getLog() for ALog

  • Examples to test getLog()

  • getLog() for MTLog

  • getLog() for ConsLog

  • sameMonthInAYear() in Entry

  • sameMonthInAYear() in Date

  • ALog class diagram

  • Exercise 6.0.5

  • Exercise 6.0.6

  • Exercise 6.0.7

  • Exercise

  • Slide 117

  • Slide 118

  • Problem Statement

  • Sheme version

  • Modification of ALog

  • sortByDistance() in ALog

  • Examples

  • sortByDistance() in MTLog

  • sortByDistance() in ConsLog

  • Q

  • Modification of ALog

  • insertInDistanceOrder() in ALog

  • Examples for insertInDistanceOrder()

  • insertInDistanceOrder() in MTLog

  • insertInDistanceOrder() in ConsLog

  • hasDistanceShorterThan() in Entry

  • Exercise 6.0.8

  • Exercise (cont)

  • Slide 135

  • Overlaying shape example

  • Recall the current design

  • Design 1

  • Design 2

  • Design 3

  • Question

  • Define classes and constructors

  • Define classes and constructors

  • Define classes and constructors

  • Test constructor

  • Slide 146

  • Modified Class Diagram

  • distanceToO() in CompositeShape

  • Slide 149

  • Slide 150

  • Modified Class Diagram

  • contains() in CompositeShape

  • Slide 153

  • Bounding box for Composite shape

  • boundingBox() in CompositeShape

  • Slide 156

  • Slide 157

  • River Systems Example

  • Slide 159

  • Class Diagram (design 1)

  • Design 2

  • Design 3

  • Discussion

  • Define classes and constructors

  • Define classes and constructors (cont)

  • Test Constructor

  • Slide 167

  • Slide 168

  • Slide 169

  • Slide 170

  • Exercise 6.5.1

  • Exercise 6.5.1 Class Diagram

  • Exercise 6.5.2

  • Exercise 6.5.2 Class Diagram

  • Exercise 6.5.3

  • Slide 176

Nội dung

A2 Programming Course Methods and Containment in Unions Week 10,11,12,13 List Managing Inventory • A sales clerk in a toy store needs to know not only the name of the toy, but also its price, warehouse availability • The representation of an inventory as a list of toys Data definition In Scheme: • In Scheme, we would use a list to represent a complete inventory In Java: Java doesn't provide built-in empty and cons According to the Scheme-based data definition, the class of Inventory is a union: Inventory, which is the abstract class of all kind of inventories; ;; Inventory is one of: ;; - a empty MTInventory, which represents an empty inventory; and ;; - a (cons Toy Inventory) Cons, which represents the construction of a new • (define-struct Toy (name price available )) inventory from a Toy and an existing Inventory ;; A toy is: (make-Toy String Number Number) Class diagram • Following Scheme, we also know that an MTInventory isn't really a structure, so that we don't need any fields for it • A Cons though consists of two things, and therefore the Cons class requires two field definitions: one for the first Toy and one for the rest of the Inventory Inventory self-referential Toy MTInventory Cons -Toy first -Inventory rest -String name -double price -int avilable Define classes an constructors (cont) // class Inventory public abstract class Inventory { public Inventory() { } } //class MTInventory public class MTInventory extends Inventory{ public MTInventory() { } } //class Cons public class Cons extends Inventory{ private Toy first; private Inventory rest; public Cons(Toy first, Inventory rest ) { this.first = first; this.rest= rest; } } Define classes and constructors //class Toy public class Toy { private String name; private double price; private int available; public Toy(String name, double price, int available) { this.name = name; this.price = price; this.available = available; } } Test Constructor public class TestInventory extends TestCase { public void testConstructor(){ Toy doll = new Toy("doll", 17.95, 5); Toy robot = new Toy("robot", 22.05, 3); Toy gun = new Toy ("gun", 15.0,4); Inventory i1= new MTInventory(); Inventory i2= new Cons(doll, i1); Inventory i3= new Cons(robot, i2); Inventory i4= new Cons(gun, i3); System.out.println(i4); } } toString() method • • • • Q: How can we print the contain of an object A:overriding toString() method of class Object Q: Do we need to add toString() in Inventory class? Why? A: No ! public String toString(){ return "name: " + this.first.getName() + ", price: " + this.first.getPrice()+ “,available: “ + this.first.getAvailable()+ “ \n” + this.rest; } // class Cons public String toString(){ return ""; Deligation? } // class MTInventory Getter in classes Toy public class Toy { private String name; private int wage; … public String getName() { return this.name; } public double getPrice() { return this.price; } public int getAvailable() { return this.available; } } 10 Design ARiverSytem -Mouth mouth -ARiver aRiver Location Mouth -Location location -int x -Int y -String name ARiver Location location self-referential Source Confluence -ARiver left -ARiver right 162 Discussion • Q: Which version you prefer, design or design or design 3? Why? 163 Define classes and constructors // class Location public class Location { private int x; private int y; String name; public Location(int x, int y, String name) { this.x=x; this.y=y; this.name=name; } } // class Mouth public class Mouth { private Location location; private ARiver river; public Mouth(Location location, ARiver river) { this.location=location; this.river=river; 164 Define classes and constructors (cont) public abstract class ARiver { protected Location location; protected double length; public ARiver(Location location, double length) { this.location=location; this.length=length; } } //class ARiver public class Source extends ARiver { public Source(Location location, double length) { super(location,length); } } //class Source public class Confluence extends ARiver { private ARiver left; private ARiver right; public Confluence(Location location, double length, ARiver left, ARiver right) { super(location,length); this.left=left; this.right=right; 165 Test Constructor public class TestARiver extends TestCase { public void test(){ ARiver s = new Source (new Location(1, 1,"s"),120.0); ARiver t = new Source (new Location(1, 5,"t"),50.0); ARiver u = new Source (new Location(3, 8, "u"),100.0); ARiver b = new Confluence(new Location(3, 3, "b"),60.0, ` ARiver a = new Confluence(new Location(5, 5,"a"), 30.0, s, t); b, u); Mouth m = new Mouth(new Location(7, 5, "m"), a); System.out.println(m); } } 166 Problem The EPA must represent river systems and monitor them…An EPA officer may wish to query a computer about the number of sources that feed a river system… 167 Problem An EPA officer may wish to find out whether some location is a part of a river system, regardless of whether it is a source, a confluence, or the river mouth 168 Problem An EPA officer may request the number of miles of a river system, either starting from the river's mouth or any of its confluence points 169 Exercises 170 Exercise 6.5.1 Design a data representation for shopping lists Start from the class of grocery items developed in exercise 5.1.2 part I Add the following methods: • • • howMany, which computes the number of items on the shopping list; brandList, which produces the list of all brand names; and highestPrice, which determines the highest unit price among all items in the shopping list 171 Exercise 6.5.1 Class Diagram AnItem # String branchName # double weight # double price + double unitPrice() + boolean lowerPrice(double amount) + boolean cheaperThan(AnItem that)() Ice Cream - String flavor - String package Coffee - String label Juice - String flavor - String package 172 Exercise 6.5.2 Develop a program for managing discount bookstores (see exercise 5.1.4 part I): • • design a representation for lists of books; write down (in English) three examples of book lists and their corresponding data representations; • develop the method thisAuthor, which produces the list of books that this author has authored 173 Exercise 6.5.2 Class Diagram ABook # String title # String author # double price # int publicationYear + double salePrice() + boolean cheaperThan(ABook that) + boolean sameAuthor(ABook that) Hardcover Sale Paperback + double salePrice() + double salePrice() + double salePrice() 174 Exercise 6.5.3 Extend the following methods to classes that represent river systems with the following methods: • maxlength, which computes the length of the longest river segment; • confluences, which counts the number of confluences in the river system; and • locations, which produces a list of all locations on this river the sources, the mouths, and the confluences 175 Relax & …Do Exercises … Too much hard exercises now Try again, never stop practicing! GOOD JOB! 176 ... double distance, int durationInMinutes, String postRunFeeling) { this.date = date; this.distance = distance; this.durationInMinutes = durationInMinutes; this.postRunFeeling = postRunFeeling; } } //class... Data definition In Scheme: • In Scheme, we would use a list to represent a complete inventory In Java: Java doesn't provide built -in empty and cons According to the Scheme-based data definition,... String toString(){ return ""; } // class MTLog 25 toString () in Entry public class Entry { private Date date; private double distance; private int durationInMinutes; private String postRunFeeling;

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

TỪ KHÓA LIÊN QUAN