程式語言結構 Final Project 老師: Gwan-Hwan Hwang. Goal The students can gain the knowledge of the object operation for the Java programming language. –Class –Interface.

Slides:



Advertisements
Similar presentations
Java Review Interface, Casting, Generics, Iterator.
Advertisements

Real food for all™ Monday TuesdayWednesdayThursdayFriday September Lunch - PSN No School Labor Day Hot Meatball.
Feb Ron McFadyen1 Iterator Pattern Recall Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate.
Real food for all™ Monday TuesdayWednesdayThursdayFriday May Lunch K-12 Mac & Cheeseburger Pasta Sausage Pizza.
Fall 2007ACS Ron McFadyen1 Composite Pattern (see pages ) A composite is a group of objects in which some objects contain others; one object.
Feb Ron McFadyen1 Iterator Pattern Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate interface.
Winter 2015ACS Ron McFadyen1 Composite Pattern A composite is a group of objects in which some objects contain others; one object may represent.
The healthy diet of European. Monday: The breakfast: The scrambled eggs from two eggs kcal A glass of milk (2% the fat)- 115 kcal Jam - 32 kcal.
Food and Restaurants. What kinds of American Food do you know? Try to think of 20 different foods that people eat in America.
Real food for all™ Monday TuesdayWednesdayThursdayFriday September Hot Breakfast No School Labor Day RF Honey.
Real food for all™ Monday TuesdayWednesdayThursdayFriday Lunch: choice of 1% or fat free milk, fresh fruit available.
FOOD Project work from English Lenka Káčerová,3.A.
Real food for all™ Monday TuesdayWednesdayThursdayFriday August Hot Breakfast RF Honey Buttons Cereal (DF) Zac.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Goals for Today  implement a Deck of Cards  composition  Iterator interface  Iterable interface 1.
Chapter 9: The Iterator Pattern
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
程式語言結構 Final Project. Goal The student can gain the knowledge of the object operation for the Java programming language. –Class –Interface.
Real food for all™ Monday TuesdayWednesdayThursdayFriday September Unitized Breakfast HOT Buenos Dias Breakfast.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 Program exercise -Yacc Use Lex and Yacc to generate a compiler for Micro/Ex Micro/Ex is an extension of Micro. %the beginning of an test data for Micro/Ex.
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
(62 AND OVER) PLEASE INDICATE TO YOUR SERVER WHEN ORDERING OFF THE SENIOR MENU. ALL OF OUR SENIOR MENU COMBINATION PLATTERS ARE PROPORTIONED SMALLER THAN.
CS 210 Iterator Pattern October 31 st, Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Iterator Pattern. Traversing two different collections  When bringing two previously developed objects together, it can be difficult to change an implementation.
Iteration Abstraction SWE Software Construction Fall 2009.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Real food for all™ Monday TuesdayWednesdayThursdayFriday March Breakfast New!! Jumpstart Breakfast: Mini Dipperdoodle.
Real food for all™ Monday TuesdayWednesdayThursdayFriday August Unitized Breakfast Kashi Berry Blossoms Cereal.
1 Software Project n Design and implement an assembly for SIC/XE The test data look like Figure 2.5. and are generated by MS Notepad. The possible OPCODE.
Java Collection Classes Com379PT
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Lesson 2 The Healthy Eating Manual, Nutrition Resource Centre, 2011 Healthy Eating the Lower Sodium Way Lesson 2.
1 Iterator Pattern (A Behavioral Pattern) Prepared by: Neha Tomar.
Vegetables, Salads, Condiments & Dressings
February 2017 Lunch Menu District 31
Vegetables, Salads, Condiments & Dressings
PHED 1111: Physical Education Spring 2012 Section 201
November Lunch Menu District 31
Vegetables, Salads, Condiments & Dressings
Kings Head Lunches Baked potatoes Sandwiches Wraps
Sixth Lecture ArrayList Abstract Class and Interface
Breakfast Served Fresh Daily 7 – 10am. Breakfast Tacos 3. 49
Breakfast Menu What food groups can you see in this meal?
What's New? HOT/COLD BREAKFAST
Vegetables, Salads, Condiments & Dressings
Introduction to Computer Science / Procedural – 67130
Vegetables, Salads, Condiments & Dressings
Vegetables, Salads, Condiments & Dressings
Iterator Design Pattern
Jumpstart breakfast : Mini Dipperdoodle and string cheese
September 2016 Elementary Lunch Confluence Academy
Fall/Winter Menu – Week 1
November 2018 Lunch Calendar
Uncle Nick’s Fresh Bagels $1.75
Menu item at a restaurant
Summer Break! College Achieve NSLP BREAKFAST K-8 What's New?
Iteration Abstraction
Build Your Own Yoghurt Station
Selection of Fresh Fruit and Vegetables Offered Daily
January Meal Calendar 2019 Breakfast: Hot Choice (Menu) Cold Cereal Option Daily Seconds: $1.00 Milk: $ .50 Salad Bar, Fruit, Vegetable and Milk will be.
December Menu 2018 MMS, MHS All meals are served with 1% or fat free flavored milk Monday Tuesday Wednesday Thursday Friday 3 Cheese Burger Beef 3oz WG.
Build Your Own Yoghurt Station
Software Design Lecture : 39.
March Lunch Calendar 2019 Mon Tue Wed Thu Fri   1 B: Breakfast L: Lunch
Build Your Own Yoghurt Station
Presentation transcript:

程式語言結構 Final Project 老師: Gwan-Hwan Hwang

Goal The students can gain the knowledge of the object operation for the Java programming language. –Class –Interface 2

What you can learn from this project? There are lots of ways to stuff objects into a collection. Put them in an Array, Stack, List, or Hash table, take your pick. –Each has its own advantages and tradeoffs. But at some point your client is going to want to iterate over those objects, and when he does, are you going to show him your implementation? We certainly hope not! –That just wouldn’t be professional. Well, you don’t have to risk your career; you’re going to see how you can allow your clients to iterate through your objects without ever getting a peek at how you store your objects. 3

Scenario Breaking News: Objectville Diner and Objectville Pancake House Merge That’s great news! Now we can get those delicious pancake breakfasts at the Pancake House and those yummy lunches at the Diner all in one place. But, there seems to be a slight problem … 4

They want to use my Pancake House menu as the breakfast menu and the Diner’s menu as the lunch menu. We’ve agreed on an implementation for the menu items… … but we can’t agree on how to implement our menus. That joker over there used an ArrayList to hold his menu items, and I used an Array. Neither one of us is willing to change our implementations… We just have too much code written that depends on them. Scenario (Cont’d) 5

Step 1 Understand and compile the listed Java program –The MenuItem class is used to save the properties of a menu item. Then, write a Java program with a public static void main(String[] args) method to test the class. public class MenuItem { private String name; private String description; private boolean vegetarian; private double price; public MenuItem(String name, String description, boolean vegetarian, double price) { this.name = name; this.description = description; this.vegetarian = vegetarian; this.price = price; } public String getName(){ return name; } public String getDescription(){ return description; } public boolean isVegetarian(){ return vegetarian; } public double getPrice(){ return price; } public void print() { System.out.print(" " + name); if (vegetarian) { System.out.print("(v)"); } System.out.println(", " + price); System.out.println(" -- " + decsription); }

Step 2 Implement PancakeHouseMenu class and DinerMenu class There is a problem. –Diner and Pancake House have different menu implementations. 7

import java.util.ArrayList; public class PancakeHouseMenu { private ArrayList menuItems; //... other fields public PancakeHouseMenu() { menuItems = new ArrayList(); addItem("K&B's Pancake Breakfast", "Pancakes with scrambled eggs, and toast", true, 2.99); addItem("Regular Pancake Breakfast", "Pancakes with fried eggs, sausage", false, 2.99); addItem("Blueberry Pancakes", "Pancakes made with fresh blueberries", true, 3.49); addItem("Waffles", "Waffles with your choice of blueberries or strawberries", true, 3.59); } public void addItem(String name, String description, boolean vegetarian, double price) { //... } public ArrayList getMenuItems() { return menuItems; } //... other methods } Then, write a Java program with a public static void main(String[] args) method to test the class. The Pancake House menu use a list to contain their menu items: (You can use the java.util.ArrayList class or other linked-list-like structure written by yourself) 8

And the Dinner menu use an array to contain their menu items: public class DinerMenu { public static final int MAX_ITEMS = 6; private MenuItem[] menuItems; //... other fields public DinerMenu() { menuItems = new MenuItem[MAX_ITEMS]; addItem("Vegetarian BLT", "(Fakin') Bacon with lettuce & tomato on whole wheat", true, 2.99); addItem("BLT", "Bacon with lettuce & tomato on whole wheat", false, 2.99); addItem("Soup of the day", "Soup of the day, with a side of potato salad", false, 3.29); addItem("Hotdog", "A hot dog, with saurkraut, relish, onions, topped with cheese", false, 3.05); addItem("Steamed Veggies and Brown Rice", "Steamed vegetables over brown rice", true, 3.99); addItem("Pasta", "Spaghetti with Marinara Sauce, and a slice of sourdough bread", false, 3.89); } public void addItem(String name, String description, boolean vegetarian, double price) { //... } public MenuItem[] getMenuItems() { return menuItems; } //... other methods } Then, write a Java program with a public static void main(String[] args) method to test the class. 9

Step 3 Now the menus are ok. It's time to provide a waitress for listing menu items. The waitress must implements our Waitress interface to be a real waitress: 10

Step 3(Cont’d) Using the following interface Waitress to implement the WaitressVersion1 class public interface Waitress { /** prints every item on the menu */ public void printMenu(); /** prints just breakfast items */ public void printBreakfastMenu(); /** prints just lunch items */ public void printLunchMenu(); /** prints all vegetarian menu items */ public void printVegetarianMenu(); /** * given the name of an item, returns true if the items is vegetarian, * otherwise, returns false */ public boolean isItemVegetarian(String name); } 11

The first waitress you hire will look like: public class WaitressVersion1 implements Waitress { private PancakeHouseMenu pancakeHouseMenu; private DinerMenu dinerMenu; //... other fields public WaitressVersion1(PancakeHouseMenu pancakeHouseMenu, DinerMenu dinerMenu) { this.pancakeHouseMenu = pancakeHouseMenu; this.dinerMenu = dinerMenu; } public void printMenu() { // use getMenuItems() in each menu for printing } public void printBreakfastMenu() { //... } public void printLunchMenu() { //... } public void printVegetarianMenu() { //... } public boolean isItemVegetarian(String name) { //... } //... other methods }

Test your first waitress You can use our MenuTestDrive to test your waitress: And the output should be: public class MenuTestDrive { public static void main(String[] args) { PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu(); DinerMenu dinerMenu = new DinerMenu(); Waitress waitress = new WaitressVersion1(pancakeHouseMenu, dinerMenu); waitress.printMenu(); // other testing } MENU ---- BREAKFAST K&B's Pancake Breakfast(v), Pancakes with scrambled eggs, and toast Regular Pancake Breakfast, Pancakes with fried eggs, sausage Blueberry Pancakes(v), Pancakes made with fresh blueberries Waffles(v), Waffles, with your choice of blueberries or strawberries LUNCH Vegetarian BLT(v), (Fakin') Bacon with lettuce & tomato on whole wheat BLT, Bacon with lettuce & tomato on whole wheat Soup of the day, Soup of the day, with a side of potato salad Hotdog, A hot dog, with saurkraut, relish, onions, topped with cheese Steamed Veggies and Brown Rice(v), Steamed vegetables over brown rice Pasta, Spaghetti with Marinara Sauce, and a slice of sourdough bread

Step 4 The implementation of WaitressVersion1 have a disadvantage. –That is you must know the underlying data structure (array or list) with each menu to access the menu items. public class MenuTestDrive { public static void main(String[] args) { PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu(); DinerMenu dinerMenu = new DinerMenu(); Waitress waitress = new WaitressVersion1(pancakeHouseMenu, dinerMenu); waitress.printMenu(); // other testing } 14

Review the WaitressVersion1 class : public class WaitressVersion1 implements Waitress { private PancakeHouseMenu pancakeHouseMenu; private DinerMenu dinerMenu; //... other fields public WaitressVersion1(PancakeHouseMenu pancakeHouseMenu, DinerMenu dinerMenu) { this.pancakeHouseMenu = pancakeHouseMenu; this.dinerMenu = dinerMenu; } public void printMenu() { // use getMenuItems() in each menu for printing } public void printBreakfastMenu() { //... } public void printLunchMenu() { //... } public void printVegetarianMenu() { //... } public boolean isItemVegetarian(String name) { //... } //... other methods } Access the menu items from two different menus.

Step 4 (Cont’d) Now, we can provides a way to access the menu items of each menu sequentially without exposing its underlying representation. –The solution is create an iterator for each menu, which implements the Iterator interface: public interface Iterator { /** tells us if there are more elements in the aggregate to iterate through */ public boolean hasNext(); /** returns the next object in the aggregate */ Object next(); } 16

Add iterators for each menu Write the iterators for PancakeHouseMenu and DinerMenu class: public class PancakeHouseMenuIterator implements Iterator { //... fields public boolean hasNext() { //... } public Object next() { //... } public class DinerMenuIterator implements Iterator { //... fields public boolean hasNext() { //... } public Object next() { //... } 17

And add the createIterator() method to PancakeHouseMenu and DinerMenu class: public class PancakeHouseMenu { //... public Iterator createIterator() { //... } //... } public class DinerMenu { //... public Iterator createIterator() { //... } //... } 18

When you implement the iterators for each menu You can hire a smarter waitress called WaitressVersion2 : public class WaitressVersion2 implements Waitress { private PancakeHouseMenu pancakeHouseMenu; private DinerMenu dinerMenu; //... other fields public WaitressVersion2(PancakeHouseMenu pancakeHouseMenu, DinerMenu dinerMenu) { this.pancakeHouseMenu = pancakeHouseMenu; this.dinerMenu = dinerMenu; } public void printMenu() { Iterator pancakeIterator = pancakeHouseMenu.createIterator(); Iterator dinerIterator = dinerMenu.createIterator(); System.out.println("MENU\n----\nBREAKFAST"); printMenu(pancakeIterator); System.out.println("\nLUNCH"); printMenu(dinerIterator); } // implement printBreakfastMenu() and printLunchMenu() public void printMenu(Iterator iterator) { while (iterator.hasNext()) { MenuItem menuItem = (MenuItem) iterator.next(); //object down casting to MenuItem menuItem.print(); } //implement other methods : isItemVegetarian & printVegetarianMenu }

Review the interface Waitress : public interface Waitress { /** prints every item on the menu */ public void printMenu(); /** prints just breakfast items */ public void printBreakfastMenu(); /** prints just lunch items */ public void printLunchMenu(); /** prints all vegetarian menu items */ public void printVegetarianMenu(); /** * given the name of an item, returns true if the items is vegetarian, * otherwise, returns false */ public boolean isItemVegetarian(String name); } 20

Test your second waitress Modify our MenuTestDrive to test your waitress: And you will see the same output. public class MenuTestDrive { public static void main(String[] args) { PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu(); DinerMenu dinerMenu = new DinerMenu(); Waitress waitress = new WaitressVersion2(pancakeHouseMenu, dinerMenu); waitress.printMenu(); // other testing } 21

P.S. If you have any question about Java –You can refer the java.sun.com –Ask the class TA at R107 of our department The execution results in each step: 1.Test MenuItem class (page.6) 2.Test PancakeHouseMenu class (page.8) 3.Test DinerMenu class (page.9) 4.Execute the MenuTestDrive class (page. 13, 21) 5.Complete all waitress and iterator classes and interfaces 22

Project Report The student should prepare a report which contains at least the follows: –The source codes and execution results in each step. If you have your own test data, you can show it. –What you have learned and experienced during the implementation.. E.g. You could show your daily record of the implementation. –In case you implement more than the required specification, please itemize it. –Copyright Claim Do you make the implementation yourself? –Any thing you would like to let G.H.Hwang know. E.g. Suggestion, … Who will be reading the report? –Not TAs but G. H. Hwang 23

How to hand in your report? Please send a mail to TA with a zip or rar file –Mail title: Programming Language final project + your student id –Attached filename: your_student_id.zip(.rar) –It should have the at least the following items: Electronic files of your report –It must be a pdf. Source codes(.java and.class) Your test data and the corresponding execution results. 24