1 of 8

CS149 – Arrays of Objects

"A computer is like a mischievous genie. It will give you exactly what you ask for, but not always what you want." - Joe Sondow

2 of 8

Review classes and objects

Classes generally include the following kinds of methods (in addition to others):

• constructor methods that initialize new objects

• accessor methods (getters) that return attributes

• mutator methods (setters) that modify attributes

• object methods such as equals and toString

3 of 8

Review

  1. Identify the constructors for the Color class. What is the difference between them? What arguments do they take?
  2. What kind of constructor does the Point class have that the Color class does not? What is the purpose of such a constructor?

3) Identify an accessor(getter) method in the Point clas

    • Which instance variable does it get?
    • What arguments does the method take?
    • What does the method return?

4) Identify a mutator(setter) method in the Point class.

    • Which instance variable does it set?
    • What arguments does the method take?
    • What does the method return?

5) The Color class does not have accessors or mutators, but it provides methods that return lighter or darker Color objects. Why do you think the class was designed this way?

4 of 8

Arrays of points

This code will test to see if a test point is in an array of points.

public static boolean found(Point[] pts, Point test) {

boolean located = false;

for(Point p: pts) {

if (p.equals(test)) {

return true;

}

}

return located;

}

import java.awt.Point;

public class TestPoints {

public static void main(String[] args) {

Point a = new Point(0,0);

Point[] points = {a, new Point(1,2), new Point(3,5)};

Point[] points2 = {new Point(3,5), new Point(1,1), new Point(2,2)};

System.out.println("Found test point in array1: " + found(points, a));

System.out.println("Found test point in array2 " + found(points2, a));

}

}

Java Tutor version

5 of 8

Model 2 Playing Cards

  • Now let’s play a different game. In this section, we’ll create an array of face cards. (Note: The array is one-dimensional, but the cards are displayed on four lines for convenience.)

6 of 8

Card Questions

9. Implement the following constructor. The class has two attributes: rank and suit. Don’t think too hard about it.

/**

* Constructs a face card given its rank and suit.

* @param rank face value (1 = ace, 11 = jack, 12 = queen, 13 = king)

* @param suit category ("clubs", "diamonds", "hearts", or "spades")

*/

public Card(int rank, int suit) { }

10. In one line of code,declare an array of strings named suits and initialize its contents to the four possible suits as shown above.

7 of 8

Card Questions Contd.

11. Write several lines of code to declare and create an array of 52 Card objects. Use nested for loops to construct Card objects in the order of the Model. Make use of your suits array from the previous question. How you will keep track of the array index?

12. Describe what the following code does and how it works.(Note:You’ve come a long way this semester, to be able to understand this example!)

public static Card[] sort(Card[] deck) {

if (deck == null) {

System.err.println("Missing deck!");

return null;

}

Card[] sorted = new Card[deck.length];

for (Card card : deck) {

int index = card.position();

sorted[index] = card;

}

return sorted;

}

13. Write a static method named inDeck that takes a Card[] representing a deck of cards and a Card object representing a single card, and that returns true if the card is somewhere in the deck of cards. Be sure to use the equals method of the Card object to make comparisons.

8 of 8

  • Acknowledgements

Parts of this activity are based on materials developed by Chris Mayfield and Nathan Sprague.

</end>