Designing Classes

Download Report

Transcript Designing Classes

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 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


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



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

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


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

Their sole purpose is to start a program.

DeckOfCardsTest.java
7

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



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.
The members of a cohesive team have a common goal.
9


What methods are needed in Card Class
What are in DeckOfCards?
10
11
12

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


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
15
In Java, a
method can
never change the
contents of a
variable that is
passed to a
method.
16


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 lastAssignedNumber that is a property of the class, not
any object of the class.
17

Every BankAccount object has its own balance and
accountNumber instance variables, but all objects share a
single copy of the lastAssignedNumber variable.
18
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" };
19
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;
20


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
22
23
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
…
}
24
25



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
26
27