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

Thông tin cơ bản

Định dạng
Số trang 176
Dung lượng 651,42 KB

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