Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 205 Java Programming II

Similar presentations


Presentation on theme: "CSC 205 Java Programming II"— Presentation transcript:

1 CSC 205 Java Programming II
Lecture 27 March 20, 2002

2 Instance Variable VeryLongInt CLASS
THE ONLY FIELD WILL BE: ArrayList digits; // holds all of the digits // in the VeryLongInteger

3 Default Constructor public VeryLongInt( ) {
final int INITIAL_CAPACITY = 500; digits= new ArrayList (INITIAL_CAPACITY); } // default constructor

4 Constructor With String Param.
FOR THE CONSTRUCTOR WITH A STRING PARAMETER: FOR EACH CHARACTER c IN s, MAKE SURE ‘0’ <= c <= ‘9’ IF SO, APPEND (c – ‘0’) TO digits.

5 Constructor With String Param.
public VeryLongInt (String s) { // for each character, if the character is between // ‘0’ and ‘9’, convert the character to a digit and // append the digit to digits. } // constructor with string parameter Suppose s = “3x79”. What will digits contain?

6 The toString Method public String toString() {
final String EMPTY_STRING = ""; String s = EMPTY_STRING; for (int i = 0; i < digits.size(); i++) s += digits.get (i); return s; } // method toString

7 The add Method VeryLongInt a1, a2; // a1 = {3, 8, 7, 4, 9, 7}
FOR THE add METHOD: VeryLongInt a1, a2; // a1 = {3, 8, 7, 4, 9, 7} // a2 = {5, 3, 8, 4} a1.add (a2);

8 The add Method public void add (VeryLongInt otherVeryLong) {
  final int BASE = 10;   int largerSize, partialSum, carry = 0; VeryLongInt sum = new VeryLongInt(); if (digits.size() > otherVeryLong.digits.size()) largerSize = digits.size(); else largerSize = otherVeryLong.digits.size();

9 The add Method partialSum = least (i) +
for (int i = 0; i < largerSize; i++) {   partialSum = least (i) + otherVeryLong.least (i) + carry; carry = partialSum / BASE; sum.digits.add (new Integer ( partialSum % BASE)); } // for   if (carry == 1) sum.digits.add (new Integer (carry)); Collections.reverse (sum.digits); digits = sum.digits; } // method add

10 The least Method Suppose the VeryLongInt object has the value 13579
then least (0) RETURNS 9 least (1) RETURNS 7 least (2) RETURNS 5 least (3) RETURNS 3 least (4) RETURNS 1 least (5) RETURNS 0 least (6) RETURNS 0 ...

11 The Deck Class public class Deck {
final static public int MAX_NUMBER = Card.getNumberOfSuits()*Card.getNumberOfRanks(); private ArrayList cards; public Deck() { cards = new ArrayList(MAX_NUMBER); for (int i = 0; i < 4; i++) for (int j = 0; j < 13; j++) cards.add(new Card(j, i)); }

12 The Hand Class public class Hand { private int cardsRemained;
ArrayList[] suits; public Hand() { suits = new ArrayList[4]; for (int i=0; i<SUITS; i++) suits[i] = new ArrayList(); cardsRemained = 0; }


Download ppt "CSC 205 Java Programming II"

Similar presentations


Ads by Google