Classes/Object and Annotations Flashcards
Consider the following class:
public class IdentifyMyParts { public static int x = 7; public int y = 3; }
- What are the class variables?
x
Consider the following class:
public class IdentifyMyParts { public static int x = 7; public int y = 3; }
What are the instance variables?
y
Consider the following class:
public class IdentifyMyParts { public static int x = 7; public int y = 3; }
//What is the output from the following code: IdentifyMyParts a = new IdentifyMyParts(); IdentifyMyParts b = new IdentifyMyParts(); a.y = 5; b.y = 6; a.x = 1; b.x = 2; System.out.println("a.y = " + a.y); System.out.println("b.y = " + b.y); System.out.println("a.x = " + a.x); System.out.println("b.x = " + b.x); System.out.println("IdentifyMyParts.x = " + IdentifyMyParts.x);
Here is the output:
a.y = 5 b.y = 6 a.x = 2 b.x = 2 IdentifyMyParts.x = 2
Because x
is defined as a public static int
in the class IdentifyMyParts
, every reference to x
will have the value that was last assigned because x
is a static variable (and therefore a class variable) shared across all instances of the class. That is, there is only one x
: when the value of x
changes in any instance it affects the value of x
for all instances of IdentifyMyParts
.
This is covered in the Class Variables section of Understanding Instance and Class Members.
Write a class whose instances represent a single playing card from a deck of cards. Playing cards have two distinguishing properties: rank and suit. Be sure to keep your solution as you will be asked to rewrite it in Enum Types.
Card.java
public class Card { private final int rank; private final int suit; // Kinds of suits public final static int DIAMONDS = 1; public final static int CLUBS = 2; public final static int HEARTS = 3; public final static int SPADES = 4; // Kinds of ranks public final static int ACE = 1; public final static int DEUCE = 2; public final static int THREE = 3; public final static int FOUR = 4; public final static int FIVE = 5; public final static int SIX = 6; public final static int SEVEN = 7; public final static int EIGHT = 8; public final static int NINE = 9; public final static int TEN = 10; public final static int JACK = 11; public final static int QUEEN = 12; public final static int KING = 13; public Card(int rank, int suit) { assert isValidRank(rank); assert isValidSuit(suit); this.rank = rank; this.suit = suit; } public int getSuit() { return suit; } public int getRank() { return rank; } public static boolean isValidRank(int rank) { return ACE \<= rank && rank \<= KING; } public static boolean isValidSuit(int suit) { return DIAMONDS \<= suit && suit \<= SPADES; } public static String rankToString(int rank) { switch (rank) { case ACE: return "Ace"; case DEUCE: return "Deuce"; case THREE: return "Three"; case FOUR: return "Four"; case FIVE: return "Five"; case SIX: return "Six"; case SEVEN: return "Seven"; case EIGHT: return "Eight"; case NINE: return "Nine"; case TEN: return "Ten"; case JACK: return "Jack"; case QUEEN: return "Queen"; case KING: return "King"; default: //Handle an illegal argument. There are generally two //ways to handle invalid arguments, throwing an exception //(see the section on Handling Exceptions) or return null return null; } } public static String suitToString(int suit) { switch (suit) { case DIAMONDS: return "Diamonds"; case CLUBS: return "Clubs"; case HEARTS: return "Hearts"; case SPADES: return "Spades"; default: return null; } } public static void main(String[] args) { // must run program with -ea flag (java -ea ..) to // use assert statements assert rankToString(ACE) == "Ace"; assert rankToString(DEUCE) == "Deuce"; assert rankToString(THREE) == "Three"; assert rankToString(FOUR) == "Four"; assert rankToString(FIVE) == "Five"; assert rankToString(SIX) == "Six"; assert rankToString(SEVEN) == "Seven"; assert rankToString(EIGHT) == "Eight"; assert rankToString(NINE) == "Nine"; assert rankToString(TEN) == "Ten"; assert rankToString(JACK) == "Jack"; assert rankToString(QUEEN) == "Queen"; assert rankToString(KING) == "King"; assert suitToString(DIAMONDS) == "Diamonds"; assert suitToString(CLUBS) == "Clubs"; assert suitToString(HEARTS) == "Hearts"; assert suitToString(SPADES) == "Spades"; } }
Write a class whose instances represents a full deck of cards. You should also keep this solution.
You can use the assert
statement to check your assignments. You write:
assert (boolean expression to test);
If the boolean expression is false, you will get an error message. For example,
assert toString(ACE) == "Ace";
should return true
, so there will be no error message.
If you use the assert
statement, you must run your program with the ea
flag:
java -ea YourProgram.class
Deck.java.
import java.util.\*; public class Deck { public static int numSuits = 4; public static int numRanks = 13; public static int numCards = numSuits \* numRanks; private Card[][] cards; public Deck() { cards = new Card[numSuits][numRanks]; for (int suit = Card.DIAMONDS; suit \<= Card.SPADES; suit++) { for (int rank = Card.ACE; rank \<= Card.KING; rank++) { cards[suit-1][rank-1] = new Card(rank, suit); } } } public Card getCard(int suit, int rank) { return cards[suit-1][rank-1]; } }
Write a small program to test your deck and card classes. The program can be as simple as creating a deck of cards and displaying its cards.
DisplayDeck.java.
import java.util.\*; public class DisplayDeck { public static void main(String[] args) { Deck deck = new Deck(); for (int suit = Card.DIAMONDS; suit \<= Card.SPADES; suit++) { for (int rank = Card.ACE; rank \<= Card.KING; rank++) { Card card = deck.getCard(suit, rank); System.out.format("%s of %s%n", card.rankToString(card.getRank()), card.suitToString(card.getSuit())); } } } }
What’s wrong with the following program?
public class SomethingIsWrong { public static void main(String[] args) { Rectangle myRect; myRect.width = 40; myRect.height = 50; System.out.println("myRect's area is " + myRect.area()); } }
The code never creates a Rectangle
object. With this simple program, the compiler generates an error. However, in a more realistic situation, myRect
might be initialized to null
in one place, say in a constructor, and used later. In that case, the program will compile just fine, but will generate a NullPointerException
at runtime.
The following code creates one array and one string object. How many references to those objects exist after the code executes? Is either object eligible for garbage collection?
... String[] students = new String[10]; String studentName = "Peter Smith"; students[0] = studentName; studentName = null; ...
There is one reference to the students
array and that array has one reference to the string Peter Smith
. Neither object is eligible for garbage collection. The array students
is not eligible for garbage collection because it has one reference to the object studentName
even though that object has been assigned the value null
. The object studentName
is not eligible either because students[0]
still refers to it.
How does a program destroy an object that it creates?
A program does not explicitly destroy objects. A program can set all references to an object to null
so that it becomes eligible for garbage collection. But the program does not actually destroy objects.
Fix the program called SomethingIsWrong shown in Question 1.
public class SomethingIsWrong { public static void main(String[] args) { Rectangle myRect; myRect.width = 40; myRect.height = 50; System.out.println("myRect's area is " + myRect.area()); } }
public class SomethingIsRight { public static void main(String[] args) { Rectangle myRect = new Rectangle(); myRect.width = 40; myRect.height = 50; System.out.println("myRect's area is " + myRect.area()); } }
Given the following class, called NumberHolder
, write some code that creates an instance of the class, initializes its two member variables, and then displays the value of each member variable.
public class NumberHolder { public int anInt; public float aFloat; }
public class NumberHolderDisplay { public static void main(String[] args) { NumberHolder aNumberHolder = new NumberHolder(); aNumberHolder.anInt = 1; aNumberHolder.aFloat = 2.3f; System.out.println(aNumberHolder.anInt); System.out.println(aNumberHolder.aFloat); } }
The program Problem.java doesn’t compile. What do you need to do to make it compile? Why?
public
class
Problem {
String s;
static
class
Inner {
void
testMethod() {
s =
"Set from Inner"
;
}
}
}
Delete static
in front of the declaration of the Inner
class. An static inner class does not have access to the instance fields of the outer class. See ProblemSolved.java
.
Use the Java API documentation for the Box class (in the javax.swing package) to help you answer the following questions.
Question: What static nested class does Box define?
https://docs.oracle.com/javase/8/docs/api/javax/swing/Box.html
Box.Filler
What inner class does Box define? https://docs.oracle.com/javase/8/docs/api/javax/swing/Box.html
Box.AccessibleBox
What is the superclass of Box's inner class? https://docs.oracle.com/javase/8/docs/api/javax/swing/Box.html
[java.awt.]Container.AccessibleAWTContainer