Contents
- ./AutoboxFixed.java
- ./AutoboxInt.java
- ./Autobox.java
- ./Card.java
- ./Deck.java
- ./IntegerListExample.java
./AutoboxFixed.java 1/6
[top][prev][next]
/**
* This class demonstrates fixing the autoboxing inefficiency.
*
* @author Sara Sprenkle
*/
public class AutoboxFixed {
/**
* Called when user runs java Autobox
*/
public static void main(String[] args) {
// Fixed: Find the inefficiency in the code below.
long startTime = System.currentTimeMillis();
long sum = 0L;
for (long i = 0; i < Integer.MAX_VALUE; i++) {
sum += i;
}
System.out.println(sum);
long finishTime = System.currentTimeMillis();
double seconds = (finishTime - startTime)/1000.0;
System.out.println("Elapsed time: " + seconds + " s");
}
}
./AutoboxInt.java 2/6
[top][prev][next]
/**
* This class demonstrates inefficiencies with unnecessary autoboxing.
* Note that changing the loop's counter variable to an int does *not*
* help with respect to improving runtime because the autoboxing is
* still occuring.
*
* @author Sara Sprenkle
*/
public class AutoboxInt {
public static void main(String[] args) {
// Find the inefficiency in the code below.
long startTime = System.currentTimeMillis();
Long sum = 0L;
for (int i = 0; i < Integer.MAX_VALUE; i++) {
sum += i;
}
System.out.println(sum);
long finishTime = System.currentTimeMillis();
double seconds = (finishTime - startTime)/1000.0;
System.out.println("Elapsed time: " + seconds + " s");
}
}
./Autobox.java 3/6
[top][prev][next]
/**
* This class demonstrates inefficiencies with unnecessary autoboxing
*
* @author Sara Sprenkle
*/
public class Autobox {
public static void main(String[] args) {
// Find the inefficiency in the code below.
long startTime = System.currentTimeMillis();
Long sum = 0L;
for (long i = 0; i < Integer.MAX_VALUE; i++) {
sum += i;
}
System.out.println(sum);
long finishTime = System.currentTimeMillis();
double seconds = (finishTime - startTime)/1000.0;
System.out.println("Elapsed time: " + seconds + " s");
}
}
./Card.java 4/6
[top][prev][next]
/**
* Represents a playing card. Demonstrates use of enumerated types, enum.
* enums are not covered in class. This class is mostly here for use in the
* Deck class and if you're curious about enums.
*
* @author CSCI209
*/
public class Card {
/**
* Represents the ranks in a deck of playing cards
*/
public enum Rank {
DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE;
}
/**
* Represents the suits in a deck of playing cards
*/
public enum Suit {
CLUBS, DIAMONDS, HEARTS, SPADES;
}
// a card won't change its rank or suit after it is created
private final Rank rank;
private final Suit suit;
/**
* Creates a new card object, with the given rank and suit
*
* @param rank
* @param suit
*/
public Card(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
}
/**
* Returns this card's rank
*
* @return card's rank
*/
public Rank getRank() {
return rank;
}
/**
* Returns this card's suit
*
* @return card's suit
*/
public Suit getSuit() {
return suit;
}
/**
* 10, J, Q, K: 10 points A: 15 points all others: 5 points
*
* @return the value of the card in the game of Rummy
*/
public int getRummyValue() {
switch( rank ) {
case ACE:
return 15;
case TEN:
case JACK:
case QUEEN:
case KING:
return 10;
default:
return 5;
}
}
/**
* Determines if this card and another card have the same suit. Returns true
* if they do.
*
* @param c
* another Card to compare
* @return true iff the cards have the same suit
*/
public boolean sameSuit(Card c) {
return this.suit.equals(c.suit);
// return this.suit == c.suit;
// return this.suit().equals(c.suit());
}
/**
* Returns a string representation of this card.
*
* @return string representation of a Card in the form <rank> of
* <suit>
*/
@Override
public String toString() {
// leverages toString() methods of Rank and Suit
return rank + " of " + suit;
}
public static void main(String args[]) {
Card jackOfDiamonds = new Card(Rank.JACK, Suit.DIAMONDS);
Card aceOfDiamonds = new Card(Rank.ACE, Suit.DIAMONDS);
System.out.println(jackOfDiamonds);
if (jackOfDiamonds.sameSuit(aceOfDiamonds)) {
System.out.println(jackOfDiamonds + " and " + aceOfDiamonds
+ " are the same suit.");
}
System.out.println("The rummyValue of " + jackOfDiamonds + " is "
+ jackOfDiamonds.getRummyValue());
}
}
./Deck.java 5/6
[top][prev][next]
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Represents a deck of playing cards
*
* @author Sara Sprenkle and CS209
*
*/
public class Deck {
// define our instance variable as an interface variable, List, not the
// concrete implementation class.
// Note that only Card objects can be put into the list or taken
// out of the list.
/** a list of Card objects */
private List<Card> deck;
/**
* Creates a new, shuffled deck of cards
*/
public Deck() {
this(true);
}
/**
* Creates a new deck of cards
*
* @param shuffle
* - true if the cards should be shuffled
*/
public Deck(boolean shuffle) {
deck = new ArrayList<>();
refresh(shuffle);
}
/**
* Refresh the deck from the beginning -- as in, it contains all the cards again
*
* @param shuffle
* - true if the cards should be shuffled
*/
public void refresh(boolean shuffle) {
// removes all the Cards from the deck
deck.clear();
// Use the enums defined in the Card class
for (Card.Suit suit : Card.Suit.values()) {
for (Card.Rank rank : Card.Rank.values()) {
Card c = new Card(rank, suit);
deck.add(c);
}
}
if (shuffle) {
shuffle();
}
}
/**
* Display the contents of the deck.
*/
public void display() {
for (Card c : deck) {
System.out.println(c);
}
}
/**
* Shuffles the deck of cards
*/
public void shuffle() {
Collections.shuffle(deck);
}
/**
* Draws the first/"top" card from the deck, removes it from the deck,
* and returns the chosen card
*
* @return the top card from the deck, which is removed
*/
public Card draw() {
return deck.remove(0);
}
/**
* Returns a list of cards of size numCards that are drawn
* from the top of the deck.
*
* @param numCards
* @return a list of cards (of the specified size)
*/
public List<Card> deal(int numCards) {
/* Note that this method returns a *List*, not an
ArrayList. This adds flexibility to our code.
We can change the list implementation (maybe a
LinkedList would be better?) and the calling code
is not affected.
*/
List<Card> hand = new ArrayList<>();
for (int i = 0; i < numCards; i++) {
hand.add(draw());
}
return hand;
}
/**
* Evaluates the strength of a hand in Poker, poorly.
* Returns an integer representing the strength of the hand.
* @param pokerHand a poker hand
* @return the strength of the hand in poker
*/
public static int calcStrengthOfHand(List<Card> pokerHand) {
int strength = 0;
for( Card c : pokerHand ) {
if( c.getRank() == Card.Rank.ACE ) {
strength += 5;
} else {
strength += 1;
}
}
return strength;
}
/**
* @param args
*/
public static void main(String[] args) {
Deck d = new Deck();
d.display();
d.shuffle();
/* deal returns a List<Card>. I don't know which
List implementation isn't used, and I don't *need* to know.
If the implementation changes, my code doesn't need to change!
Horray!!
*/
List<Card> myHand = d.deal(5);
System.out.println("\nMy hand contains:");
for( Card c : myHand ) {
System.out.println(c);
}
System.out.println("My hand's strength in Poker is " + calcStrengthOfHand(myHand));
}
}
./IntegerListExample.java 6/6
[top][prev][next]
import java.util.*;
/**
* Making a List of random numbers.
*
* @author Sara Sprenkle
*/
public class IntegerListExample {
public static void main(String[] args) {
List<Integer> myList = new ArrayList<>();
Random rand = new Random();
for( int i=0; i < rand.nextInt(1,100); i++ ) {
myList.add(rand.nextInt(100));
}
System.out.println("My list of random numbers of length " + myList.size() + ":");
for( int i:myList ) {
System.out.println(i);
}
myList.clear();
// Doing it again to show the benefit of using a List rather than an array
System.out.println("Doing it again!");
for( int i=0; i < rand.nextInt(1,100); i++ ) {
myList.add(rand.nextInt(100));
}
System.out.println("My list of random numbers of length " + myList.size() + ":");
for( int i:myList ) {
System.out.println(i);
}
}
}
Generated by GNU Enscript 1.6.5.90.