clients and suppliers

22
Clients and Suppliers The business of computer science

Upload: joseph-rosario

Post on 02-Jan-2016

19 views

Category:

Documents


1 download

DESCRIPTION

Clients and Suppliers. The business of computer science. Clients and Suppliers. Two classes: May be related as client – supplier. May not be related as client – supplier. Client : The object that uses the services of the supplier to accomplish a complex task Supplier : - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Clients and Suppliers

Clients and Suppliers

The business of computer science

Page 2: Clients and Suppliers

2

Clients and Suppliers Two classes:

May be related as client – supplier. May not be related as client – supplier.

Client: The object that uses the services of the supplier to

accomplish a complex task

Supplier: The object that provides data/services to the other

object

Page 3: Clients and Suppliers

3

Supplier Example

Design a class that keeps statistical information about a bunch of numbers average standard deviation min max others…

We’ll start with just ‘average’

Averager - int count - int total

«constructor» + Averager()

«update» + void clear() + void include( int )

«query» + double getAverage() + int getIncludeCount()

Page 4: Clients and Suppliers

4

Supplier Example

// code fragment that uses the Average classAverager sum1, sum2;sum1 = new Averager();sum1.include( 10 );sum1.include( 10 );sum1.include( 3 );sum1.include( 7 );System.out.println(“Count: “+sum1.getIncludeCount());System.out.println(“Average: “+sum1.getAverage());

sum2 = new Averager();sum2.include( (int) Math.random()*10 );sum2.include( (int) Math.random()*10 );System.out.println(“Count: “+sum2.getIncludeCount());System.out.println(“Average: “+sum2.getAverage());

Averager - int count - int total

«constructor» + Averager()

«update» + void clear() + void include( int )

«query» + double getAverage() + int getIncludeCount()

Page 5: Clients and Suppliers

5

How to Implement?

public class Averager { private int count; private int total; public Averager() { clear(); }

public void clear() {count = 0;total = 0;

}

public void include( int d ) {total = total + d;count++;

}

public double getAverage() {return ((double)total) / count;

}

public int getIncludeCount() {return count;

}}

Averager - int count - int total

«constructor» + Averager()

«update» + void clear() + void include( int )

«query» + double getAverage() + int getIncludeCount()

Page 6: Clients and Suppliers

6

public class Die { private int value;

public Die() { value = roll(); }

public int roll() { value = (int)(1 + Math.random() * 6); return value; }

public int getValue() { return value; }}

Consider writing a Die class. The class should not be concerned with the way a Die ‘looks’ but with what a die ‘is’ and how it is used.

1) Generate a class diagram2) Generate a class specification3) Implement the class

Supplier Example

Page 7: Clients and Suppliers

Supplier Examplepublic class Die { private int value; private Averager averager;

public Die() { averager = new Averager(); value = roll(); }

public int roll() { value = 1 + Math.random() * 6; averager.include(value); return value; }

public int getValue() { return value; }

public double getAverage() { return averager.getAverage(); }

public int getNumberOfTimesRolled() { return average.getIncludeCount(); }}

Consider writing a more feature-laden Die class.

A Die can report on the average of every roll that IT has ever made and the total number of times it has been rolled.

7

Page 8: Clients and Suppliers

Client Supplier in UML Class Diagram A client supplier relationship can be represented

in a UML class diagram A line connecting two boxes (classes) represents a

relationship A black diamond is connected to the client No annotation is given on the suppliers end Can be understood as ‘aggregation’

Die AveragerYahtzee

Page 9: Clients and Suppliers

9

The String Class

The String class is an example of a supplier A String represents a sequence of chars

(text!) A char is one of the eight primitive types A char represent a single keyboard

character a single textual symbol

G o R sma

Single char Single String

0 1 2 3 4 5 6Indices

Page 10: Clients and Suppliers

10

Strings

Strings are objects! A sequence of characters (i.e. text) Built-in class Immutable

Can’t be altered or changed once created Syntax: zero or more characters enclosed in double

quotes

Image from: http://www.fabricattic.com/Buckaroo%20Bears%20blue%20rope.jpg

“a” “Hello!” “3<true” “” Hi! “12”

Page 11: Clients and Suppliers

11

String Behavior

What kind of operations should be supported by Strings? length of string “adding” two strings comparing two strings finding a char in the string many others…

Image from: http://www.fonsecatim.com/Vlume1d.JPG

Page 12: Clients and Suppliers

12

Strings

String “concatenation” Indicated by a “+” Computes a new string that joins two strings

public int length() returns the number of characters in the sequence

public char charAt(int index) returns the number of character at the specified index

public boolean equals(Object other) returns true if the two strings are equals an false otherwise

public boolean startsWith(String other) returns true if the calling string begins with the specified string

public String toLowerCase() returns a new string that is an all lower-case representation of the calling

public String toUpperCase() returns a new string that is an all upper-case representation of the calling

public String substring(int start, int end) returns a new string that is a substring of original. Starting at start through end-1.

Page 13: Clients and Suppliers

13

Strings

public int indexOf(char c) returns the index of the first occurrence of c or -1 if c does not occur

public boolean isEmpty() returns true if the string has no characters (i.e. if the length is 0)

public char charAt(int index) returns the number of character at the specified index

public boolean endsWith(String other) returns true if the calling string ends with the specified string

public boolean equalsIgnoreCase(String other) returns true if the calling string is equal to the other and case is not considered

public String replace(char oldChar, char newChar) returns a new string where every oldChar has been replaced by newChar

public String trim() returns a copy of the string with leading and trailing whitespace omitted

Page 14: Clients and Suppliers

14

String Concatenation

String concatenation is indicated by ‘+’ binary infix operator operands must be strings if either operand is NOT a string, the operand is

converted to a string and then the operation is performed

String msg = “Hello World”;String msg2 = msg + 13;String msg3 = “V” + 8;String msg4 = 12 + ‘a’;

Page 15: Clients and Suppliers

15

String

String msg = “Hello World”;int x = msg.length();char c = msg.charAt(4);boolean b1 = msg.startsWith(“He”);String s1 = msg.toLowerCase();String s2 = msg.toUpperCase();String s3 = msg + “ again “ + ‘!’.boolean b2 = msg.equals(“hello world”);

Page 16: Clients and Suppliers

String Problems Write a function that accepts an email address and returns

the username. Write another function that returns the domain. Note that emails are all of the form <username>@<domain>

Write a function that accepts a date in the format MM/DD/YYYY and returns it in a European format of DD/MM/YYYY.

Write a function that accepts an integer-number string and converts it to an int.

Write a function that repeats a string "n" times. The function accepts the string and n.

Write a function that counts the number of times a particular character occurs in a string.

Write a function that counts the number of vowels in a string.

Page 17: Clients and Suppliers

17

String Example

DNA are typically represented as strings Composed of characters ‘C’, ‘A’, ‘G’, ‘T’

C: Cytosine A: Adenine G: Guanine T: Thymine

ExampleCAGGGGGTAC

Image from: http://members.aol.com/wayneheim/dna-w.jpg

Page 18: Clients and Suppliers

18

String Processing

Problem: Write a function that accepts a string and returns “true” if the string is a DNA string and false otherwise. The method should be case insensitive.

public boolean isDNA(String s) { for(int i=0; i<s.length() ; i++) { if(!isDNAChar(s.charAt(i))) return false; } return true;}

public boolean isDNAChar(char c) { return c == ‘C’ || c == ‘A’ || c == ‘G’ || c == ‘T’;}

Page 19: Clients and Suppliers

19

String Processing

Problem: Write a function that accepts a string and returns true if the string is a palindrome and false otherwise.

public boolean isPalindrome(String s) { for(int i=0; i<s.length()/2; i++) { int j=s.length()-i; if(s.chartAt(i) != s.chartAt(j)) return false; } return true;}

Example: given BACAB the method should return true (a mayan diety). Also try NAMAN.

Page 20: Clients and Suppliers

Access Control Modifiers A class is a container for data and methods.

The class is able to control access to these things via "access control modifiers" public: any code anywhere has access private: only code in the containing class has

access protected: we’ll talk about this later

Use public when the item is for external use Use private when the item is only for internal

use

Page 21: Clients and Suppliers

21

Scope Design

RULE: Hide as much information (from the client) as possible!

Variables: Use LOCAL whenever possible Use PRIVATE only when data needs to be kept between

method calls Use PUBLIC – Almost Never!

Methods Use PRIVATE whenever possible Use PUBLIC if the client is meant to use it

Image from: http://www.stecf.org/~rfosbury/home/natural_colour/spectroscopes/Browning_s-scope.gif

Page 22: Clients and Suppliers

Access to instance variables Instance variables are almost always ‘private’

How can clients know those values? How can clients change those values?

Instance variables are made readable through accessors. A method that returns the value. Conventionally named “getX” or "isX" for booleans

Instance variables are made changeable through settors. A method that changes the value. Conventionally named “setX” of "isX" for booleans22