css446 spring 2014 nan wang. to learn how to choose appropriate classes for a given problem to...

27
CSS446 Spring 2014 Nan Wang

Upload: lizbeth-jackson

Post on 25-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

CSS446Spring 2014

Nan Wang

Page 2: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

To learn how to choose appropriate classes for a given problem

To understand the concept of cohesion To minimize dependencies and side effects To learn how to find a data representation

for a class To understand static methods and variables To learn about packages

2

Page 3: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

A class should represent a single concept from a problem domain.

E.g. Point Rectangle Ellipse

◦ Other classes are abstractions of real-life entities: BankAccount CashRegister

Card Game

3

Page 4: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

The name for such a class should be a noun that describes the concept. In fact, a simple rule of thumb for getting started with class design is to look for nouns in the problem description.

Card DeckOfCards

4

Page 5: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

Objects of an actor class carry out certain tasks for you

A Scanner Object: scans a stream for numbers and strings.

A Random object: generates random numbers. It is a good idea to choose class names for actors that end in “-er” or “-or”. (A better name for the Random class might be RandomNumberGenerator.)

E.g. Random myRandomNumberGenerator = new Random();

5

Page 6: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

Utility class has no objects, but it contains a collection of related static methods and constants.

E.g. ◦ Math Class◦ Arrays Class

import java.util.Arrays

6

Page 7: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

Their sole purpose is to start a program.

DeckOfCardsTest.java

7

Page 8: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

What is a simple rule of thumb for finding classes?

Your job is to write a program that plays chess. Might ChessBoard be an appropriate class? How about MovePiece?

What classes will be in your card game?

8

Page 9: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

Providing a Cohesive public interface A class should represent a single concept. All interface

features should be closely related to the single concept that the class represents.

Such a public interface is said to be cohesive.

9

The members of a cohesive team have a common goal.

Page 10: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

What methods are needed in Card Class What are in DeckOfCards?

10

Page 11: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

11

Page 12: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

12

Page 13: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

A mutator method changes the state of an object. ◦ setFace()

An accessor method asks an object to compute a result, without changing the state. ◦ getFace()

13

Page 14: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

Designed to have only accessor methods and no mutator methods at all.

E.g String Class◦ String name = "John Q. Public";◦ String uppercased = name.toUpperCase(); //

name is not changed An immutable class has a major advantage:

It is safe to give out references to its objects freely. If no method can change the object’s value, then no code can modify the object at an unexpected time.

14

Page 15: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

15

Page 16: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

16

In Java, a method can never change the contents of a variable that is passed to a method.

Page 17: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

A value properly belongs to a class, not to any object of the class.

We want to assign bank account numbers sequentially. That is, we want the bank account constructor to construct the first account with number 1001, the next with number 1002, and so on. To solve this problem, we need to have a single value of lastAssigned Number that is a property of the class, not any object of the class.

17

Page 18: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

Every BankAccount object has its own balance and accountNumber instance variables, but all objects share but all objects share a single copy of the lastAssignedNumber variablea single copy of the lastAssignedNumber variable.

18

Page 19: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

19

private static final String[] faces = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; private static final String[] suits = { "Hearts", "Diamonds", "Clubs", "Spades" };

Page 20: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

20

private static final int ONEPAIR = 2; private static final int TWOPAIR = 4; private static final int THREEKIND = 6; private static final int STRAIGHT = 8; private static final int FULLHOUSE = 10; private static final int FLUSH = 12; private static final int FOURKIND = 14; private static final int STRAIGHTFLUSH = 16;

Page 21: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

A class defines methods that are not invoked on an object.

the sqrt method in the Math class. Because numbers aren’t objects, you can’t invoke methods on them. For example, if x is a number, then the call x.sqrt() is not legal in Java. Therefore, the Math class provides a static method that is invoked as Math.sqrt(x). No object of the Math class is constructed. The Math qualifier simply tells the compiler where to find the sqrt method.

21

Page 22: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

22

Page 23: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

23

Page 24: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

24

public static void main( String[] args ) { DeckOfCards myDeckOfCards = new DeckOfCards(); myDeckOfCards.shuffle(); // place Cards in random order Card[] hand1 = new Card[ 5 ]; // store first hand Card[] hand2 = new Card[ 5 ]; // store second hand…}

Page 25: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

25

Page 26: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

26

Due Date: 1/23/14 (Next Thursday) Submit your zipped package to

[email protected] Grading:

◦ 100 points for implementation of simple rules E.g. if there are same pairs in both hands, they tie

with out continuing comparison of next card.◦ 50 bonus points for implementation of complete

rules. Do not tell me you do not know how to play

card game, I do not believe it

Page 27: CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies

27