1992-2007 pearson education, inc. all rights reserved. 1 cs01 object-oriented programming: case...

149
1 1992-2007 Pearson Education, Inc. All rights rese CS01 Object-Oriented Programming: Case Studies 01

Upload: charlene-norman

Post on 28-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

1

1992-2007 Pearson Education, Inc. All rights reserved.

CS01

CS01Object-Oriented

Programming:Case Studies 01

Page 2: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

2

1992-2007 Pearson Education, Inc. All rights reserved.

Cases Studies

• A sales store

• Restaurant

• Registration System

• Course Scheduling System

• Reservation System

Page 3: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

3

1992-2007 Pearson Education, Inc. All rights reserved.

Other Cases

• ATM• Lone payment follow up

– A financial problem

• Simple edditor• Simple calculator• Simple games

– Tic-tac-too– Memory cards

• Agent-based simulation studies– e-auctions, service, production, markets

Page 4: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

4

1992-2007 Pearson Education, Inc. All rights reserved.

Relationship between Classes

• has a – composition

• E.g.:– A student has a(n)

• name, address, phone number

– A book has a• title, publisher,...• author or authors

– A bank has• accounts• customers

Page 5: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

5

1992-2007 Pearson Education, Inc. All rights reserved.

Relationship between Classes

• is a – inheritance

• E.g.:– A student is a person

• name, address, phone number

– A book is a document• author or authors, title, publisher,... From document

• Specific variables to books

– A dog is an animal• move – every animal moves

Page 6: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

6

1992-2007 Pearson Education, Inc. All rights reserved.

one to one Relationship

• A student has – one name,– one ID number

• A book has a• one title, one publisher,...

• A bank account• one code

• Imlementation:– instance variable – primitive or class– initialize with constructors– set methods for chaning– get methods for obtaining the values– Part of toString methods - String representation

Page 7: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

7

1992-2007 Pearson Education, Inc. All rights reserved.

one to many Relationship

• A store has many– products– employees– customers– branchs

• These are properties of each store object• 1 to many relationship

– one store has many customers, products.

• Implementation– instance variable – array of simple types or objcects– Constructor – initialise the array or get the array as a parameter

Page 8: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

8

1992-2007 Pearson Education, Inc. All rights reserved.

one to many Relationship

– Methods for– Adding new elemnents

• E.g.: adding new customers to a store• Adding new bank account to a bank• Adding new products

– Removing elements• Deleting customers• Closing accounts

– Searching elements• Find a particular customer, or account• How – search key

– index of the array – direct method (not practical)– Key – e.g.: customer ID, account ID.

Page 9: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

9

1992-2007 Pearson Education, Inc. All rights reserved.

one to many Relationship

– iterate over the elements• Searching,

• calculating totals

Page 10: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

10

1992-2007 Pearson Education, Inc. All rights reserved.

many to many Relationship

• student - course– each student can take many courses

– each course are taken by many students

• book - author– Each book may have many authors

– each authorr may wtite many books

• Emloyees – departments– each may work in more then one department

– each department has many employees

Page 11: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

11

1992-2007 Pearson Education, Inc. All rights reserved.

Composition and Inheritnece

• Person – Student: is a relation– Student is inherited from Person

• Person– attributes:

• name – String

• personID – String

– Constructors and methods• get, set methods

• toString method

Page 12: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

12

1992-2007 Pearson Education, Inc. All rights reserved.

Composition and Inheritnece

• Student– attributes:

• department – String

• schoolID – String

• others

– Constructors and methods• get, set methods

• toString method

– overrides toStirng of Person

• can reach: name, personID invoking get set methods of Person

Page 13: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

13

1992-2007 Pearson Education, Inc. All rights reserved.

Another design - Composition

• Student– attributes:

• person – Person– a person object is an instance variable– set name, personID on this object

• department – String• schoolID – String• others

– Constructors and methods• get, set methods• toString method

– overrides toStirng of Person• can reach: name, personID invoking get set methods over the person

instance variable in the studnet object

Page 14: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

14

1992-2007 Pearson Education, Inc. All rights reserved.

The ArrayList Class

• Array – store objects but – Once an Array is created its size is fixed

• Java ArrayList class to store unlimited number of objects

Page 15: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

15

1992-2007 Pearson Education, Inc. All rights reserved.

Some Methods of the ArrayList Class

• ArrayList()– Create an empty list

• void add(Object o)– Append a new element o at the end of the list

• void add(int index, Object o)– Add a new element o at the specifed index to the list

• void clear()– Removes all the elements from the list

• boolean contains(Object o)– Returns true if the lsit contains Object o

Page 16: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

16

1992-2007 Pearson Education, Inc. All rights reserved.

Some Methods of the ArrayList Class (cont.)

• Object get(int index)– Returns the element from the list at the specified index

• int indexOf(Object o)– Retuns the index of the first matching element in the list

• boolean isEmpty()– Returns true if the list is empty – contains no elements

• int lastIndexOf(Object o)– Returns the index of the last matching element

• boolean remove(Object o)– Removes the element from the list

Page 17: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

17

1992-2007 Pearson Education, Inc. All rights reserved.

Some Methods of the ArrayList Class (cont.)

• int size()– Returns the number of elements in the list

• boolean remove(int index)– Removes the element at the specifed index from the list

• Object set(int index, Object o)– Sets the element at the specified index

Page 18: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

18

1992-2007 Pearson Education, Inc. All rights reserved.

Comparing Array and ArrayList

• Creatng an Array/ArrayList– Object[] a = new Object[10];

– ArrayList list = new ArrayList();

• Accesing an element– a[index]

– list.get(index);

• Updating an element– a[index] = “London”;

– list.set(index,”London”);

Page 19: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

19

1992-2007 Pearson Education, Inc. All rights reserved.

Comparing Array and ArrayList (cont.)

• Returning size– a.length;

– list.size();

• Adding a new element– -

– list.add(“London”);

• Inserting a new element– -

– list. add(index,“London”);

Page 20: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

20

1992-2007 Pearson Education, Inc. All rights reserved.

Comparing Array and ArrayList (cont.)

• Removing an element– -

– list.remove(index);

– list.remove ( “London”);

• Removing all elements– -

– list.clear();

Page 21: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

21

1992-2007 Pearson Education, Inc. All rights reserved.

An Example Program

import java.util.ArrayList;public class ArrayListTest {

public static void main(String[] args) { ArrayList list = new ArrayList(); list.add("Ankara"); list.add("İstanbul"); list.add("İzmir"); System.out.println("size:"+list.size()); int i = list.indexOf("Ankara"); System.out.println("index of Ankara"+i); System.out.println("index of Ankara"+list.indexOf("kjk"));

Page 22: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

22

1992-2007 Pearson Education, Inc. All rights reserved.

An Example Program (cont.)

Integer j = new Integer(2); list.add(j); System.out.println("size:"+list.size());

int k = 2; list.add(k); System.out.println("size:"+list.size());

Page 23: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

23

1992-2007 Pearson Education, Inc. All rights reserved.

An Example Program (cont.)

X x = new X(); list.add(x); System.out.println("index of x"+list.indexOf(x));

X y = (X)list.get(5);// downcasting is needed// returns Object- assigned to X-subclass System.out.println("value of y.a"+y.a); } // end main } // end ArrayListTst

Page 24: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

24

1992-2007 Pearson Education, Inc. All rights reserved.

An Example Program (cont.)

class X {

public double a = 10;

}

Page 25: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

25

1992-2007 Pearson Education, Inc. All rights reserved.

AutoBoxing unboxing

• Boxing

• Integer a = Integer(2);

• a = 3; // possible a simple variable or constant is set as the value of a

• a.setValue(3);

• Unboxing

• int i;

• i = a; // unboxing the value eccapsuated in a is obtained and assigned to i

• i = a.getvalue();

Page 26: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

26

1992-2007 Pearson Education, Inc. All rights reserved.

Another Example – Simple Store

• A sale consits of products and how many are purchased from each

– Calculate total payment for a sale

• Classes:– Product

• attribute: price• methods: getPrice

– Sales• attributes: list of products - ArrayList• list of counts – ArrayList• methods: addProduct, cancleProduct,getTotal

Page 27: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

27

1992-2007 Pearson Education, Inc. All rights reserved.

Another Example – Simple Store

• Test Class: SaleTestAL

• create products: p1,p2,p3

• create sales: s

• add transactions to sale (products and their sold amounts)

• cancle transactions from sale (products and their sold amounts)

• list total payment of the sale

Page 28: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

28

1992-2007 Pearson Education, Inc. All rights reserved.

Sale class with ArrayList

class Sales{

private ArrayList productList = new ArrayList();private ArrayList count = new ArrayList();

public void addTransaction(Product p,int c){ productList.add(p); count.add(c);

} // end addTransaction

public void cancleTransaction(Product p){ int i = productList.indexOf(p); productList.remove(i); count.remove(i);

} // end cancleProduct

Page 29: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

29

1992-2007 Pearson Education, Inc. All rights reserved.

Sale class with ArrayList

public double getTotal(){

Product prd;int quant;double total=0.0;for(int i=0; i < productList.size();i++) {

prd = (Product)productList.get(i); quant = (Integer)count.get(i); total += prd.getPrice()*quant;

} // end for return total;

} // emd getTptal

} // end class Sales

Page 30: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

30

1992-2007 Pearson Education, Inc. All rights reserved.

Product class

class Product{private double price;

public Product(double p){ price = p;} // end constructor

public double getPrice(){ return price;} // end get price

} // end class Product

Page 31: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

31

1992-2007 Pearson Education, Inc. All rights reserved.

StoreTestAL class

import java.util.*;

public class StoreTestAL { public static void main(String[] agrs) { Sales s = new Sales(); Product p1 = new Product(5); Product p2 = new Product(10); Product p3 = new Product(20);

Page 32: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

32

1992-2007 Pearson Education, Inc. All rights reserved.

s.addTramsaction(p1,1);

s.addTramsaction(p2,2);

s.addTramsaction(p3,2);

s.cancleTramsaction(p3);

System.out.println("total:"+s.getTotal());

}// end main

} // end StoreTestAL

Page 33: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

33

1992-2007 Pearson Education, Inc. All rights reserved.

Sale class with Collection version

class Sales{ private ArrayList<Product> productList = new ArrayList<Product>(); private ArrayList <Integer>count = new ArrayList<Integer>();

public void addTransaction(Product p,int c){ productList.add(p); count.add(c);

} // end addProduct

public void cancleTransaction(Product p){ int i = productList.indexOf(p); productList.remove(i); count.remove(i);

} // end cancleProduct

Page 34: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

34

1992-2007 Pearson Education, Inc. All rights reserved.

Sale class with Collection version

public double getTotal(){

double total=0.0;for(int i=0; i < productList.size();i++) {

total += productList.get(i).getPrice()

*count.get(i);} // end for return total;

}

} // end class Sales

Page 35: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

35

1992-2007 Pearson Education, Inc. All rights reserved.

ArrayList Collections

ArrayList<T> name = new ArrayList<T >();

T ploaceholder for a nonprimitive type

ArrayList<String> name = new ArrayList<String>();

ArrayList<Integer> name = new ArrayList<Integer>();

ArrayList<Product> name = new ArrayList<Product>();

Page 36: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

36

1992-2007 Pearson Education, Inc. All rights reserved.

Store Problem extended

• Extension of the simple store• Sell products to customers with different promotion types

by sale persons with different premiums from these sales• What are classes

– Product– Person– Employee– Customer– Promotion

• These are nouns in the requirement statement so definetly classes. Others?

– What about sales?

Page 37: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

37

1992-2007 Pearson Education, Inc. All rights reserved.

Product Class

• products– product hierarchy

– abstract product class• instance variables - name,code,price

• - stock

• methods – increase or decrease stock

– subclasses• food, cleaning, electironic, textile

• Food:

– Bevarages, meat..

Page 38: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

38

1992-2007 Pearson Education, Inc. All rights reserved.

Product Class

• products

• here for simplisity no hierarchy

• products are defined in Store class

• What about promotion?– When sold apply a prootion

Page 39: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

39

1992-2007 Pearson Education, Inc. All rights reserved.

Product class – instance variables

class Product {

private String name ;

private int stock;

private double price;

private Promotion prm;

;

Page 40: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

40

1992-2007 Pearson Education, Inc. All rights reserved.

Product class - constructors

public Product(String productName,int productStock, double price,Promotion promotion) { this(productName,productStoc, price); this.prm = promotion; } // end of constructor

public Product(String productName,int productStock, double price) { name = productName; stock = productStock;//can be set with verification by set methods

this.price = price; } // end of constructor

Page 41: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

41

1992-2007 Pearson Education, Inc. All rights reserved.

Product cass - methods

public int dropStock(int amount) { if(amount <= stock) { stock -= amount; return amount; } return 0; } // end dropStock public int getstock() { return stock;} // end getStock

Page 42: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

42

1992-2007 Pearson Education, Inc. All rights reserved.

Product get methods

public Promotion getPromotion () { return prm;} public double getPrice() { return price;}

Page 43: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

43

1992-2007 Pearson Education, Inc. All rights reserved.

Product set methods

public void setPromotion (Promotion p) { prm = p;} public double setPrice(double p) { price = p;} }// end class Product

Page 44: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

44

1992-2007 Pearson Education, Inc. All rights reserved.

Person Class

• Personal characteristics

• Super class of customers and employees

Page 45: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

45

1992-2007 Pearson Education, Inc. All rights reserved.

Person Classes Code

class Person { protected String name;

// may have other attributes

public Person(String name) {

this.name = name;

} // end of constructor

// may have other methods

} // end class Person

Page 46: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

46

1992-2007 Pearson Education, Inc. All rights reserved.

Cusomer

• Customer– general customer – abstract class– loyal – non loyal customers– loyal: having a kind of purchaseing card– loyal customers has subclasses as well

• gıolden , silver, ...card customers

– different point accumulation methods– Sale promotions may apply differently

• rules of organization

– may extend from a person class• inharits name address, birthDate ... from a Person class

Page 47: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

47

1992-2007 Pearson Education, Inc. All rights reserved.

Customer Classes

abstract class Customer extends Person { protected double point; public Customer(String name) { super(name); } public double getpoint() { return point; } abstract protected double payment(double x); } // end Customer class

Page 48: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

48

1992-2007 Pearson Education, Inc. All rights reserved.

Customer Classes

class Gold extends Customer {

private static double goldPointRate = 0.1; public Gold(String name) {

super(name); }

public double payment(double amount) { point += amount*goldPointRate;

double pointsUsed =Sales.askPoints(point); point -= pointsUsed;

return amount - pointsUsed;

} // end payment} // end class Gold

Page 49: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

49

1992-2007 Pearson Education, Inc. All rights reserved.

Customer Classes

abstract Silver extends Customer{

private static double silverPointRate = 0.05; private static double limit = 20;

public Silver(String name) {

super(name); }

public double payment(double amount) {

if (amount > limit)point += (amount-limit)*silverPointRate;

double pointsUsed =Sales.askPoints(point);

point -= pointsUsed;return amount - pointsUsed;

} // end payment } // end class Silver

Page 50: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

50

1992-2007 Pearson Education, Inc. All rights reserved.

Employee

• Employee – employee class – abstract class

– employees get preimium from the sales they made

– premium depends on the amount of sales and type of employee along the hierarchy

– Gets the amount of sales as parameters, calculates the premium depending on the type of employee and returns the premium

• As both customers and employees are persons they may extend from a Person class

Page 51: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

51

1992-2007 Pearson Education, Inc. All rights reserved.

Employee

• Emloyee

• three types of employees– type1: commission is proportional to the sales they made

(similar to the commissionEmployee class we covered in Chapter 9-10)

– type2: get premium if the amount of sales exceeds a lower limit• make the limit 0 then convert to type1

– type3: get premium if total sales they made in a say month exceeds a prespecifed limit• hold total sales they made not from a single transaction

Page 52: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

52

1992-2007 Pearson Education, Inc. All rights reserved.

Employee classes

abstract class Employee extends Person {

public Employee (String eName) { super (eName); }

public abstract double premium(double amount);

} // end class Employee

Page 53: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

53

1992-2007 Pearson Education, Inc. All rights reserved.

Employee classes

class Salaried extends Employee { private static double commissionRateSalaried = 0.05; private static double limitSalaried = 100.00; private double totalSales;

public Salaried (String eName) { super (eName); } // end constructor public double premium(double amount) { totalSales += amount; if(totalSales < limitSalaried)

return 0.0; else return amount*commissionRateSalaried;

} // end premium} // end class Salaried

Page 54: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

54

1992-2007 Pearson Education, Inc. All rights reserved.

Employee classes

class Commissioned extends Employee { private static double commissionRate = 0.10;

public Commissioned (String eName) { super (eName); } // end constructor public double premium(double amount) { return amount*commissionRate; }} // end class Salaried

Page 55: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

55

1992-2007 Pearson Education, Inc. All rights reserved.

Promotion

• promotion– different promotion types defined

• How to define promotion – a product has a promotion

• instance variable of product• default value may be no promotion – most products• may change over time – assign different promotions to a

product by the setPromotion method• promotion may interract with customer type

– certain promotions may be applied to different customers

• depends on how much purhased as well

Page 56: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

56

1992-2007 Pearson Education, Inc. All rights reserved.

Promotion class

• abstract class

• Each broad category of prootion is a subclass of the abstradt promotion

• Are associated with products

Page 57: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

57

1992-2007 Pearson Education, Inc. All rights reserved.

Promotion class

public abstract class Promotion {

protected String proName;

public Promotion(String n){

proName = n;

}

public abstract double payAfterProm(int q, Product p);

} // end of class Promotion

Page 58: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

58

1992-2007 Pearson Education, Inc. All rights reserved.

Promotion class

public class Discount extends Promotion { private double discountRate; public Discount(String n,double dr){ super(n); setDiscountRate(dr); } public double payAfterProm(int q, Product p) {

double gross = q*p.getPrice(); return gross*(1-discountRate); }} // end of class Discount

Page 59: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

59

1992-2007 Pearson Education, Inc. All rights reserved.

Promotion class

public class GetMore extends Promotion { public Discount(String n){ super(n); } public double payAfterProm (int q, Product p) {

p.setStocks(q+q/2); double gross = q*p.getPrice(); return gross; }} // end of class GetMore

Page 60: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

60

1992-2007 Pearson Education, Inc. All rights reserved.

Sales

• In general Sales – transaction• involves

– a customer (null or a customer type for layal customers)– a sales person who made the sale (obtional)– list of products each optionally are sold with a different

promotion type

– other variables • a branch of a store (optional)• time of sales for return or service information or other bookkeeping

activities such as accounting• how the payment is made – in cash or credit card..• variables related to security

Page 61: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

61

1992-2007 Pearson Education, Inc. All rights reserved.

Sales

• A concept like sales• seems to be an action but involves many objects

(single – customer or list - products)• Examine the sentences:

– customer makes a purchase buying• bread, apple....• on 04.07.2009:17:25

– shoud it be a method of customer – a behavior of customer– or products are sold

• apple is purchased by customer X on a day

– should it be a method of product

Page 62: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

62

1992-2007 Pearson Education, Inc. All rights reserved.

Sales

• Define it as a new class although is is an action or behavior of customers or products

• As a sales action involves many objects

• Treat as a moun rather than worb selling

Page 63: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

63

1992-2007 Pearson Education, Inc. All rights reserved.

Sales class – variables

Customer customer; // a Customer object

Emloyee emp;

List of products purchased and their quantities are hold in arrays or ArrayLists

Other variables holding total revenue, total premium paid to employees

These variables should be static

They are classwide , not specific to a single sale

Page 64: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

64

1992-2007 Pearson Education, Inc. All rights reserved.

Sales class - constructors

Constructors can be overloaded

Chain of constructor calls with this

İf a loyal customer exists or the empleyee responsible from the sale is known they are recorded

Products and quantites are initilized

Page 65: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

65

1992-2007 Pearson Education, Inc. All rights reserved.

Sales class – adding or canceling products

• add

• Adds products and their amounts to the basket– Add products and quantities to relevant ArrayLists or arrays

• Cancel

• Takes a product reference removes it from the basket of products

Page 66: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

66

1992-2007 Pearson Education, Inc. All rights reserved.

Sales – calculating invoice

• As the last operation

• calcuate the payment of the customer– how much to pay considering

• promotion of indivdual products

• promotions specific to customer types

• decrease stock of products

• accumulate points or customers may use some points

– calculate premium for the employee

• Update total premium, total payment

Page 67: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

67

1992-2007 Pearson Education, Inc. All rights reserved.

Sales class –variables

class Sales {

private Customer customer;

private Employee saleEmployee;

private ArrayList<Integer> quantities;

private ArrayList<Product> basket;

private static double totalnetRevenue = 0.00;

private static double totalPremium = 0.00;

Page 68: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

68

1992-2007 Pearson Education, Inc. All rights reserved.

Sales class - constructors

public Sales(Customer customer,Employee saleEmployee)

{

this(saleEmployee);

this.customer = customer;

}

public Sales(Employee saleEmployee)

{

this();

this.saleEmployee = saleEmployee;

}

Page 69: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

69

1992-2007 Pearson Education, Inc. All rights reserved.

Sales class - constructors

public Sales(Customer customer) {

this();

this.customer = customer;

}

public Sales() {

quantities = new ArrayList<Integer>();

basket = new ArrayList< Product >();

}

Page 70: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

70

1992-2007 Pearson Education, Inc. All rights reserved.

Sales class - methods

public void add(Product product,int howMany) {

basket.add(product); quantities.add(howMany);}

public void cancel(Product p) { int i basket.indexOf(p);

basket.remove(p); if (i >=0) quantities.remove(i);}

Page 71: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

71

1992-2007 Pearson Education, Inc. All rights reserved.

Sales class - methods

public void invoice() { double premium = 0.0; double netRevenue = 0.0; double revenue = payment();

if(saleEmployee != null) { premium = saleEmployee.premium(revenue); totalPremium += premium; }

if(customer != null) netRevenue = customer.payment(revenue); else netRevenue = revenue; totalNetRevenue += netRevenue; System.out.println("Revenue: "+revenue); System.out.println(“net Revenue: "+netRevenue); System.out.println("Premium: "+premium);}

Page 72: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

72

1992-2007 Pearson Education, Inc. All rights reserved.

Sales class - methods

private double payment() { double total = 0.0; for(int i=0; i<basket.size(); i++) {

Product pr = basket.get(i); int q = quantities.get(i); Promotion p = pr.getPromotion(); total += p. payAfterProm(q,pr);

} return total;} // end method payment

Page 73: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

73

1992-2007 Pearson Education, Inc. All rights reserved.

Sales class - methods

public static double askPoints(double point)

{

Scanner input = new Scanner(System.in);

System.out.println("Your points:"+point);

System.out.print("How many points to use:");

return input.nextDouble();

}

} // end class Transaction

Page 74: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

74

1992-2007 Pearson Education, Inc. All rights reserved.

TestStrore class

import java.util.Scanner;public class TestStore { public static void main(String[] args) { Store myStore = new Store(); myStore.operate();

} // end of main} // end of TestStore

can be done even without creating a Store reference like that public static void main(String[] args) { new Store().operate();

// a Store object is created and its operate() method is invoked } // end of main } // end of TestStor

Page 75: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

75

1992-2007 Pearson Education, Inc. All rights reserved.

Store class code

class Store { private Product[] products; private Customer[] customers; private Employee[] employees; private Promotion[] promotions;

public Store() { products = new Product[10]; customers = new Customer[10]; employees = new Employee[10]; promotions = new Promotion[10]; }

Page 76: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

76

1992-2007 Pearson Education, Inc. All rights reserved.

Store class

public void operate() {

// create customers

customers[0] = new Gold("ali");

customers[1] = new Silver("ahmet");

// create promotions

promotions[0] = new Discount(“todaysdis",0.10);

promotions[1] = new Discount(“weeklydis",0.05);

promotions[2] = new GetMore(“one more for two");

Page 77: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

77

1992-2007 Pearson Education, Inc. All rights reserved.

Store class

// create products

products[0] = new Product("apple",30,6.0,promotions[0]);

products[1] = new Product("chocklate",40,5.0);

products[2] = new Product("milk",40,2.5,promotions[2]);

products[3] = new Product("ball",20,2.0);

Page 78: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

78

1992-2007 Pearson Education, Inc. All rights reserved.

Store class

// create employees employees[0] = new Salaried("hasan");

employees[1] = new Commissioned("ayşe");

Sales s1 = new Sales(customers[0]);s1.add(products[1], 2);s1.add(products[2], 3);s1.invoice();

Sales s2 = new Sales(customers[0],employees[1]);s2.add(products[1], 2);s2.add(products[2], 3);

s2.cancel(1);s2.invoice();

} // end of opertate method } // endof Store class

Page 79: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

79

1992-2007 Pearson Education, Inc. All rights reserved.

Restaurant

• meals – products– each meal has ingredients

• array of items – defined in the products

– all of its ingredients’ stoks dimenishes

– weiters should be promoted for the table they serve

– orders are made tablewise• customers are tables

• The restorant has many tables

– other ordering types – home services

Page 80: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

80

1992-2007 Pearson Education, Inc. All rights reserved.

Restaurant

• Similar to strore sales in many asects but has some differences

– Produces meals from raw materials– Do not consider kitchen scheduling or supply chain yet

• When a meal is ordered– its ingredients stoks dimenishes

• Ready products – Bevarages– Not have ingredients

• A product hierarchy. – Products:

• Meals – produced in the restoran• Others – purchased

Page 81: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

81

1992-2007 Pearson Education, Inc. All rights reserved.

Restaurant

– Premium policy

– Weiters get premium from the orders

– Head weiters get some small commissins as well

– There are couple of cooks – for each type of meal they are also paid a commission

• Emloyee hierarchy– Employee – abstract

• Cooks - concrete

• Weither - abstract

– Ordinary weiter - concrete

– Weither - concrete

Page 82: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

82

1992-2007 Pearson Education, Inc. All rights reserved.

Restaurant - Products

• Meal extends from product• Bevarages extends from product• Product class – abstract class

– instance variables• name – String• price – double

– Constructors• Set neme and price when creating a product

– Methods:• Order a product by customer - abstract• toString

Page 83: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

83

1992-2007 Pearson Education, Inc. All rights reserved.

Restorant - Products- Meals

• Each meal has – ingredient list

– Customer may customize some of them • How to imlement such a customization in java

– Has a cook to produce

– Type information – discrete broad categories

• inherits from products

• implements the order method– Decrease stocks of the desired ingredients

• toString method – overrides the toString in Product

Page 84: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

84

1992-2007 Pearson Education, Inc. All rights reserved.

Restaurant - Products- Bevarages

• Each bevarages has – A type information

• Constructor

• Order mehtod – implemnt the abstact method

• toString method– Override the Products

Page 85: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

85

1992-2007 Pearson Education, Inc. All rights reserved.

Restorant - Tables

• The restaurant has many tables– List of array– Each table is assigned a weiter – Either:

• Each table has a head weither• Or each weiter has a head weiter

• Methods– addOrder

• Add a product (meal or bevarages) and haw many are ordered• When spefifying a meal a customer may exclude some of its ingredients

– Cancel a product – Total payment

• Premium weiters and cooks– ClearTable

• After payments ready for new customers

Page 86: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

86

1992-2007 Pearson Education, Inc. All rights reserved.

Restorant - Emloyee

• Variables– Name, ssn,

• Constructors

• Methods– get, set methods

– premium – abstract

– toString

Page 87: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

87

1992-2007 Pearson Education, Inc. All rights reserved.

Restorant - Cook

• Variables– Type of meals

– Commission rate – static double

• Constructors

• Methods– get, set methods

– premium – amount*commissionRate

– toString

Page 88: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

88

1992-2007 Pearson Education, Inc. All rights reserved.

Restorant - weiter

• Variables• Constructors• Methods

– get, set methods– premium – abstract– toString

• ordinaryWeiter and headWeiter classes– Extends from weiter– Static commissionRate different– Premium method – amount*commissionRate

Page 89: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

89

1992-2007 Pearson Education, Inc. All rights reserved.

Restorant - Test class

• creates – list of tables

• order of meals or bevargages

• bill for payng the bill

– list of meals• ingredients

– list of cooks• assing to meals

– list of weithers• assing to tables

Page 90: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

90

1992-2007 Pearson Education, Inc. All rights reserved.

University Registration System

• students take courses– can or can not taken some courses

• prerequisites

• cotas

• other restrictions

• students – takes courses, withdraw a course

– general or semester point averages

• instructors– give courses, give grades to students

Page 91: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

91

1992-2007 Pearson Education, Inc. All rights reserved.

Requirements

• Students should take, drop courses

• Can display their transcripts

• Instructors can give grades see lsit of students

• Registration

Page 92: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

92

1992-2007 Pearson Education, Inc. All rights reserved.

Two design alternatives

• Taking a course, droping a course, displaying transcripts are actions of students so they are methods of the student class

• Giving grades displaying course lists are actions of instructors so should be methods of instructors

Page 93: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

93

1992-2007 Pearson Education, Inc. All rights reserved.

classes

• person class – abstract

• Concrete subclasses – Student

– instructor

Page 94: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

94

1992-2007 Pearson Education, Inc. All rights reserved.

class Student

• Student class extends from Person• ınstance variables

– name – String from Person– birthDate - Date: object from Person– advisor - Instructor: object – transcript – list of courses– program – list of courses

• Constructor(s)• Methods

– add a course– drop a course– display personal information– display transcript– other get and set methods

Page 95: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

95

1992-2007 Pearson Education, Inc. All rights reserved.

class Instructor

• Instructor extends from Person

• Instance variables– name from Person

– Title

– department

• Constructor(s)

• Methods– Give grades

– See student list in a course

Page 96: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

96

1992-2007 Pearson Education, Inc. All rights reserved.

class Course

• Course class• Instance variables

– code - String– name of the course – String– credit – integer– description – String– prerequisites – list of courses

• array or arraylist of courses– Cotas – for the time being cota is a variable of course

• İn an extended version cota is a varble of a sctionTerm • MIS 233.01 in FAll 2009 is different frım• MIS 233.01 in Spring 2008 is different frım or• MIS 234.01 in Spring 2009 is different frım • MIS 234.02 in Spring 2009 is different frım

Page 97: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

97

1992-2007 Pearson Education, Inc. All rights reserved.

class Course

• Course class

• Instance variables– code - String

– name of the course – String

– credit – integer

– description – String

– prerequisites – list of courses

Page 98: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

98

1992-2007 Pearson Education, Inc. All rights reserved.

classes

• list of students

• list of courses

• many to many relationship

Page 99: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

99

1992-2007 Pearson Education, Inc. All rights reserved.

Class TestClass

• Test class• Registration : objects represents terms

– Open method for carrying opperations

• Person– instance variables

• Name

• Instructor– instance variables

• coursesGiven: array of Courses given by this instructo

– Actions by methods• Give a course• Give grades in a term for a particular student in a particular course

Page 100: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

100

1992-2007 Pearson Education, Inc. All rights reserved.

Classes

• Course– Variables

• courseName• Credit

– Methods• get and set methods and toString

• Student– instance variables

• inherits name from Person• Courses taken grades if given

– Behavior by methods• Add a course• Set grade• Calculate gpa• Display transcript

Page 101: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

101

1992-2007 Pearson Education, Inc. All rights reserved.

Class - RegistrationTest

• public class RegistrationTest {

• public static void main(String[] args) {•

• Registration registration = new Registration();• registration.open();•

• } // end method main• } // end

Page 102: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

102

1992-2007 Pearson Education, Inc. All rights reserved.

Class - Registration

class Registration {

public Registration() {

} // end constructor

// starts registration opperationspublic void open() {

// create studentsStudent s1 = new Student("ali");Student s2 = new Student("ayse");

// create coursesCourse c1 = new Course("MIS 236",4);Course c2 = new Course("MIS 224",3);

// add courses to studentss1.addCourse(c1);s1.addCourse(c2);s2.addCourse(c1);s2.addCourse(c2);

Page 103: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

103

1992-2007 Pearson Education, Inc. All rights reserved.

Class – Registration (cont.)

// print transcripts of studentss1.transcript();s2.transcript();// greate instructorsInstructor i1 = new Instructor("bertan");Instructor i2 = new Instructor("hande");

i1.giveCourse(c1);i2.giveCourse(c2);

i1.giveGrade(c1, s1,2.0);i2.giveGrade(c2, s2,4.0);

// reprint transcripts of studentss1.transcript();s2.transcript();

} // end method open} // end class Registration

Page 104: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

104

1992-2007 Pearson Education, Inc. All rights reserved.

Class - Person

class Person {

private String name;

public Person(String name) {this.name = name;

} // end constructor

// display personpublic String toString() {

return String.format("name: \t%s",name);}

} // end class Person

Page 105: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

105

1992-2007 Pearson Education, Inc. All rights reserved.

Class - Student

class Student extends Person {

private Course[] courses; // course arrayprivate double[] grades; // array of gradesprivate boolean[] gradeEntered; // true if grades are enterredprivate int count; // number of courses taken

public Student(String name) {super(name);courses = new Course[10];grades = new double[10];gradeEntered = new boolean[10];

} // end constructor

Page 106: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

106

1992-2007 Pearson Education, Inc. All rights reserved.

Class – Student (cont.)

// add a coursepublic void addCourse(Course c) {

count++;courses[count] = c;

}

// get index of courseprivate int getIndex(Course c) {

for(int i = 1; i <= count; i++) if(courses[i].equals(c)) return i; return -1;

}

Page 107: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

107

1992-2007 Pearson Education, Inc. All rights reserved.

Class – Student (cont.)

// compute gpa of studentpublic double gpa() {

int points = 0;double total = 0.0;

for(int i = 1; i <= count ; i++)if (gradeEntered[i]) {

total += courses[i].getCredit()*grades[i];points += courses[i].getCredit();

} // end ifreturn total / points;

} // end method gpa

Page 108: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

108

1992-2007 Pearson Education, Inc. All rights reserved.

Class – Student (cont.)

// seting grade of a coursepublic boolean setGrade(Course c,double grade) {

int i = getIndex(c); if(i>0) { grades[i] = grade; gradeEntered[i] = true; return true; } return false;

}

Page 109: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

109

1992-2007 Pearson Education, Inc. All rights reserved.

Class – Student (cont.)

public void transcript() {System.out.printf("%s \n %s \n",

"Trascript of ", super.toString());

for(int i = 1; i <= count; i++) System.out.printf("%s\t%s\n",courses[i],

(gradeEntered[i]) ? String.format("%3.1f",grades[i]):"");

} // end method transcript

} // end class Student

Page 110: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

110

1992-2007 Pearson Education, Inc. All rights reserved.

Class – Student (with ArrayList)

class Student extends Person {

private ArrayList<Course> courses; // ArrayList of Course

private ArrayList<Double> grades; // arrayList of Double

public Student(String name) {

super(name);

courses = new ArrayList<Course>();

grades = new ArrayList<double>();

} // end constructor

Page 111: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

111

1992-2007 Pearson Education, Inc. All rights reserved.

Class – Student (with ArrayList)(cont.)

// add a coursepublic void addCourse(Course c) {

courses.add©; grades.add(null);

}

// seting grade of a coursepublic boolean setGrade(Course c,double grade) {

int i = courses.indexOf(c); if(i >=0) { grades.set(i,grade); return true;

} else return false;

}

Page 112: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

112

1992-2007 Pearson Education, Inc. All rights reserved.

Class – Student (with ArrayList)(cont.)

// compute gpa of studentpublic double gpa() {

int points = 0;double total = 0.0;for(int i = 0; i < courses.size() ; i++)

if (grades.get(i)!=null) { total +=

courses.get(i).getCredit()*grades.get(i);points += courses.get(i).getCredit();

} // end ifreturn total / points;

} // end method gpa

Page 113: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

113

1992-2007 Pearson Education, Inc. All rights reserved.

Class – Student (with ArrayList)(cont.)

public void transcript() { System.out.printf("%s \n %s \n",

"Trascript of ", super.toString());

for(int i = 0; i <courses.size(); i++) System.out.printf("%s\t%s\n",courses.get(i),

(grades.get(i) != null) ?

String.format("%3.1f",grades.get(i)):"");} // end method transcript

} // end class Student

Page 114: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

114

1992-2007 Pearson Education, Inc. All rights reserved.

Class –Instructor

class Instructor extends Person {

private Course[] givenCourses;private int count;

public Instructor(String name) {

super(name); givenCourses = new Course[3]; } // end constructor

Page 115: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

115

1992-2007 Pearson Education, Inc. All rights reserved.

Class –Instructor (cont.)

public void giveCourse(Course c) {

count++;

givenCourses[count] = c;

}

public void giveGrade(Course c, Student s, double g) {

s.setGrade(c,g);

}

} // end class Instructor

Page 116: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

116

1992-2007 Pearson Education, Inc. All rights reserved.

Class –Instructor (with ArrayList)

class Instructor extends Person {

private ArrayList<Course> givenCourses;

public Instructor(String name) { super(name);

givenCourses = new ArrayList<Course>(); } // end constructor

Page 117: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

117

1992-2007 Pearson Education, Inc. All rights reserved.

Class –Instructor (with ArrayList) (cont.)

public void giveCourse(Course c) {

givenCourses.add(c);

}

public void giveGrade(Course c, Student s, double g) {

s.setGrade(c,g);

}

} // end class Instructor

Page 118: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

118

1992-2007 Pearson Education, Inc. All rights reserved.

Class - Course

class Course {

private String courseName;private int credit;

public Course(String cName, int cCredit) {courseName = cName;credit = cCredit;

} // end constructor

public int getCredit() {return credit;

}

// display coursepublic String toString() {

return String.format("name: \t%s\t%2d",courseName,credit);}

} // end class Course

Page 119: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

119

1992-2007 Pearson Education, Inc. All rights reserved.

Second Design Option

• Course – student – Many to many relation

• Relational approach• Create two arrays representing a table• One holding students the other courses• i th index of the arrays hold

– Students[i] = s1;– Courses[i] = c1;– Student s1 takes course c1 – Hold in index i

Page 120: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

120

1992-2007 Pearson Education, Inc. All rights reserved.

Second Design Option

• Greate two more arrays for holding grades information

– grades[i] = 4.0– At i th index for student s1, course c1 grade is 4.0

• When a student takes a course– A new element is added to the arrays– Just like adding another rew to a table in a relational

database

• Then, list courses , transcript, gpa – like methods opertate over the class containing the arrays

Page 121: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

121

1992-2007 Pearson Education, Inc. All rights reserved.

Class – Registration2Test

public class Registration2Test {

public static void main(String[] args) {

Registration2 registration = new Registration2();registration.open();

} // end method main} // end

Page 122: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

122

1992-2007 Pearson Education, Inc. All rights reserved.

Class – Registration2

class Registration2 {

// starts registration opperationspublic void open() {

// create studentsStudent s1 = new Student("ali");Student s2 = new Student("ayse");

// create coursesCourse c1 = new Course("MIS 236",4);Course c2 = new Course("MIS 224",3);

// create a termCourseTaken term = new CourseTaken();

Page 123: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

123

1992-2007 Pearson Education, Inc. All rights reserved.

Class – Registration2 (cont.)

// students take courses in that termterm.takeCourse(c1,s1);term.takeCourse(c2,s1);term.takeCourse(c1,s2);term.takeCourse(c2,s2);

// student list of coursesterm.courseList(c1);term.courseList(c2);

// print transcripts of studentsterm.transcript(s1);term.transcript(s2);

// greate instructorsInstructor i1 = new Instructor("bertan");Instructor i2 = new Instructor("hande");

Page 124: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

124

1992-2007 Pearson Education, Inc. All rights reserved.

Class – Registration2 (cont.)

i1.giveCourse(c1);

i2. giveCourse(c2);

i1.giveGrade(term,c1,s1,2.0);

i2.giveGrade(term,c2,s2,4.0);

// reprint transcripts of students

term.transcript(s1);

term.transcript(s2);

} // end method open

} // end class Registration

Page 125: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

125

1992-2007 Pearson Education, Inc. All rights reserved.

Class - Person

class Person {

private String name;

public Person(String name) {

this.name = name;

} // end constructor

// display personpublic String toString() {

return String.format("name: \t%s",name);}

} // end class Person

Page 126: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

126

1992-2007 Pearson Education, Inc. All rights reserved.

Class - Student

class Student extends Person {

public Student(String name) {

super(name);

} // end constructor

} // end class Student

Page 127: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

127

1992-2007 Pearson Education, Inc. All rights reserved.

Class – Instructor (cont.)

class Instructor extends Person {

private Course[] givenCourses;private int count;

public Instructor(String name) {

super(name); givenCourses = new Course[3]; } // end constructor

Page 128: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

128

1992-2007 Pearson Education, Inc. All rights reserved.

Class – Instructor (cont.)

public void giveCourse(Course c) {

count++;

givenCourses[count] = c;

c.setInstructor(this);

}

public void giveGrade(CourseTaken t, Course c, Student s, double g) {

t.setGrade(s,c,g);

}

} // end class Instructor

Page 129: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

129

1992-2007 Pearson Education, Inc. All rights reserved.

Class – Instructor (with ArrayList)

class Instructor extends Person {private ArrayList<Course> givenCourses;public Instructor(String name) { super(name); givenCourses = new ArrayList<Course>(); } // end constructor

public void giveCourse(Course c) { givenCourses.add(c); c.setInstructor(this);}public void giveGrade(CourseTaken t, Course c, Student s, double g) { t.setGrade(s,c,g);}

} // end class Instructor

Page 130: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

130

1992-2007 Pearson Education, Inc. All rights reserved.

Class –Course

class Course {

private String courseName;private int credit;private Instructor courseInst;

public Course(String cName, int cCredit) {

courseName = cName;credit = cCredit;

} // end constructor

public int getCredit() {

return credit;}

Page 131: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

131

1992-2007 Pearson Education, Inc. All rights reserved.

Class – Course (cont.)

public void setInstructor(Instructor inst) {

courseInst = inst;

} // end method setInstructor

// return instructor of the coursepublic Instructor getInstructor() {

return courseInst;

} // end method getInstructor

// display coursepublic String toString() {

return String.format("name: \t%s\t%2d",courseName,credit);}

} // end class Course

Page 132: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

132

1992-2007 Pearson Education, Inc. All rights reserved.

Class –CourseTaken

class CourseTaken {

private Course[] courses;private Student[] students;private double[] grades;private boolean[] gradeEntered;private int count;

public CourseTaken() {

courses = new Course[20];students = new Student[20];grades = new double[20];gradeEntered = new boolean[20];

} // end contructor

Page 133: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

133

1992-2007 Pearson Education, Inc. All rights reserved.

Class –CourseTaken (cont.)

public void takeCourse(Course c,Student s) {

count++;courses[count] = c;students[count] = s;

} // end courseTaken method

// course listspublic void courseList(Course c) {

System.out.println("Course List");System.out.printf("%s\n",c.toString());for(int i = 1 ; i <= count ; i++ )

if(courses[i].equals(c))System.out.printf("%s\n",students[i].toString());

} // end method courseList

Page 134: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

134

1992-2007 Pearson Education, Inc. All rights reserved.

Class –CourseTaken (cont.)

// transcript of studentspublic void transcript(Student s) {

System.out.println("Transcript");System.out.printf("%s\n",s.toString());for(int i = 1 ; i <= count ; i++ )

if(students[i].equals(s))System.out.printf("%s\t%s\n",courses[i],

(gradeEntered[i]) ? String.format("%3.1f",grades[i]):"");

} // end method transcript

Page 135: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

135

1992-2007 Pearson Education, Inc. All rights reserved.

Class –CourseTaken (cont.)

// get index of course by student

private int getIndex(Course c, Student s) {

for(int i = 1; i <= count; i++)

if(courses[i].equals(c) && students[i].equals(s) )

return i;

return -1;

} // end method getIndex

Page 136: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

136

1992-2007 Pearson Education, Inc. All rights reserved.

Class –CourseTaken (cont.)

// compute gpa of studentpublic double gpa(Student s) {

int points = 0;double total = 0.0;

for(int i = 1; i <= count ; i++)if (gradeEntered[i] && students[i].equals(s)) {

total += courses[i].getCredit()*grades[i];points += courses[i].getCredit();

} // end ifreturn total / points;

} // end method gpa

Page 137: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

137

1992-2007 Pearson Education, Inc. All rights reserved.

Class –CourseTaken (cont.)

// seting grade of of a student at index ipublic boolean setGrade(Student s, Course c,double grade) {

int i = getIndex(c,s); if(i >=1) { grades[i] = grade;

gradeEntered[i] = true; return true; }

else return false;

} // end method setGrade

} // end class CourseTaken

Page 138: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

138

1992-2007 Pearson Education, Inc. All rights reserved.

Class –CourseTaken(with ArrayList)

class CourseTaken {

private ArrayList<Course> courses = new ArrayList<Course>();

private ArrayList<Student> students = new ArrayList<Student>();

private ArrayList<Double> grades = new ArrayList<Double>();

// default constructor

public void takeCourse(Course c,Student s) {

courses.add(c);

students.add(s);

grades.add(-1.0);

} // end courseTaken method

Page 139: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

139

1992-2007 Pearson Education, Inc. All rights reserved.

Class –CourseTaken(with ArrayList) (cont.)

// course lists

public void courseList(Course c) {

System.out.println("Course List");

System.out.printf("%s\n",c.toString());

for(int i = 0 ; i < courses.size() ; i++ )

if(courses.get(i).equals(c))

System.out.printf("%s\n",students.get(i).toString();

} // end method courseList

Page 140: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

140

1992-2007 Pearson Education, Inc. All rights reserved.

Class –CourseTaken(with ArrayList) (cont.)

// transcript of students

public void transcript(Student s) {

System.out.println("Transcript");

System.out.printf("%s\n",s.toString());

for(int i = 0 ; i < students.size() ; i++ )

if(students.get(i).equals(s))

System.out.printf("%s\t%s\n",courses.get(i),

(grades.get(i) > -0.5) ?

String.format("%3.1f",grades.get(i)):"");

} // end method transcript

Page 141: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

141

1992-2007 Pearson Education, Inc. All rights reserved.

Class –CourseTaken(with ArrayList) (cont.)

// get index of course by student

private int getIndex(Course c, Student s) {

for(int i = 0; i < students.size(); i++)

if(courses.get(i).equals(c) && students.get(i).equals(s) )

return i;

return -1;

} // end method getIndex

Page 142: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

142

1992-2007 Pearson Education, Inc. All rights reserved.

Class –CourseTaken(with ArrayList) (cont.)

// compute gpa of studentpublic double gpa(Student s) {

int points = 0;double total = 0.0;

for(int i = 0; i < students.size() ; i++)if (grades.get(i) > -0.5 && students.get(i).equals(s)) {

total += courses.get(i).getCredit()*grades.get(i);points += courses.get(i).getCredit();

} // end ifreturn total / points;

} // end method gpa

Page 143: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

143

1992-2007 Pearson Education, Inc. All rights reserved.

Class –CourseTaken(with ArrayList) (cont.)

// seting grade of of a student at index ipublic boolean setGrade(Student s, Course c,double grade) {

int i = getIndex(c,s);if(i>=0) { grades.set(i,grade); return true;} else return false;

} // end method setGrade

} // end class CourseTaken

Page 144: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

144

1992-2007 Pearson Education, Inc. All rights reserved.

Course Scheduling Problem

• for a given semester prepare a timetable for the courese offered in that term:in what room, when, considering also other constraints such as

– at a given time no two course can be scheduled in one room

– any instructor can not give two course at the same tim

– two courses of the same program year can not be at the same time• Teo frashmen courses, senior courses can not be confilicted

• Externsions– Capacity of rooms and number of students in each course

– Requirements of instructors – data show availiability in rooms

Page 145: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

145

1992-2007 Pearson Education, Inc. All rights reserved.

Reservation Problem

Page 146: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

146

1992-2007 Pearson Education, Inc. All rights reserved.

Quiz 3 Solution

public interface Asset { double taxPayment();}public abstract class Vehical implements Asset {}public class Building implements Asset { private double area; public Building(double a) { area = a: } // end constructor public double taxPayment() { return area*10; } // end method taxPayment} // end class Building

Page 147: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

147

1992-2007 Pearson Education, Inc. All rights reserved.

Class - Car

public class Car extends Vehical { private int age; private double motorVolume; public Car(int age, double volume) { this.age = age; motorVolume = volume; } // end constructor double taxPayment() { if (age > 15) return 0.0; if(motorVolume ) return ; return ; } // end method taxPayment} // end class Car

Page 148: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

148

1992-2007 Pearson Education, Inc. All rights reserved.

Class - Truck

public class Truck extends Vehical { private double ton; public Truck(double ton) { this.ton = ton; } // end constructor double taxPayment() { if (ton > 10) return 100.0; return ton*10;} // end method taxPayment} // end class Truck

Page 149: 1992-2007 Pearson Education, Inc. All rights reserved. 1 CS01 Object-Oriented Programming: Case Studies 01

149

1992-2007 Pearson Education, Inc. All rights reserved.

Class - TaxPaymentTest

public class TaxPaymentTest {

public static void main(String[] args) {

Asset[] assets = new Asset[3];//declare and instantiate teh assets array

Asset[0] = new Building(100.0);

Asset[1] = new Car(10,1200.0);

Asset[2] = new Truck(12.0);

double totalTax = 0.0;

for(Asset currentAsset: assets) // calculate total tax due

totalTax += currentAsset.taxPayment();

System.out.printf(“total tax due: %,.2f\n”,totalTax),

} // end main

} // end class TaxPaymentTest