Contents
- ./Autobox.java
- ./Card.java
- ./Chicken.java
- ./Deck.java
- ./FindDuplicates2.java
- ./FindDuplicates3.java
- ./FindDuplicates.java
./Autobox.java 1/7
[top][prev][next]
/**
* This class demonstrates inefficiencies with unnecessary autoboxing
*
* @author Sara Sprenkle
*/
public class Autobox {
/**
* Called when user runs java AutoboxO
*/
public static void main(String[] args) {
// Find the inefficiency in the code below.
long startTime = System.currentTimeMillis();
Long sum = 0;
for (long i = 0; i < Integer.MAX_VALUE; i++) {
sum += i;
}
System.out.println(sum);
long finishTime = System.currentTimeMillis();
System.out.println("Elapsed time: " + (finishTime - startTime));
}
}
./Card.java 2/7
[top][prev][next]
package cards;
/**
* Represents a playing card. Demonstrates use of enumerated types, enum.
*/
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 rank() {
return rank;
}
/**
* Returns this card's suit
*
* @return card's suit
*/
public Suit suit() {
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());
}
}
./Chicken.java 3/7
[top][prev][next]
import java.util.Arrays;
/**
* A simple Java class that models a Chicken. The state of the chicken is its
* name, height, weight, and gender.
*
* @author CSCI209
*/
public class Chicken implements Comparable<Chicken> {
// ------------ INSTANCE VARIABLES -------------------
/** the chicken's name */
protected String name;
/** the height of the chicken in centimeters */
protected int height;
/** the weight of the chicken in pounds */
protected double weight;
/** true iff the chicken is a female */
protected boolean isFemale;
/** default name of a Chicken */
public static final String DEFAULT_NAME = "BUBBA";
/**
* Creates a fully-specified Chicken object
*
* @param name
* the name of the Chicken
* @param height
* the Chicken's height, in cm
* @param weight
* the Chicken's weight, in pounds
* @param isFemale
* if the Chicken is female (true/false)
*/
public Chicken(String name, int height, double weight, boolean isFemale) {
this.name = name;
this.height = height;
this.weight = weight;
this.isFemale = isFemale;
}
/**
* Assumes Chickens are females
*
* @param name
* the name of the Chicken
* @param height
* the Chicken's height, in cm
* @param weight
* the Chicken's weight, in pounds
*/
public Chicken(String name, int height, double weight) {
this(name, height, weight, true);
}
/**
* Default name is Bubba; Assumes Chickens are female
*
* @param height
* the Chicken's height, in cm
* @param weight
* the Chicken's weight, in pounds
*/
public Chicken(int height, double weight) {
this(DEFAULT_NAME, height, weight, true);
}
/**
* Returns a string representation of the chicken.
* Format:
* <br/>Chicken name: <name>
* <br/>weight: <weight>
* <br/>height: <height>
* <br/>female/male
*/
@Override
public String toString() {
StringBuffer rep = new StringBuffer("Chicken name: ");
rep.append(name);
rep.append("\nweight: ");
rep.append(weight);
rep.append("\nheight: ");
rep.append(height);
rep.append("\n");
rep.append(isFemale ? "female" : "male"); // Java ternary operator; also
// available in C
return rep.toString();
}
/**
* Determines if two Chickens are equivalent, based on their name, height,
* weight, and gender.
*/
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (! (o instanceof Chicken) ) {
return false;
}
Chicken other = (Chicken) o;
if (!other.getName().equals(this.getName())) {
return false;
}
if (other.getHeight() != this.getHeight()) {
return false;
}
if (other.weight != this.weight) {
return false;
}
return this.isFemale == other.isFemale;
}
/*
* (non-Javadoc)
*
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
/**
* Compares the chickens by their height
*/
@Override
public int compareTo(Chicken o) {
if (o.getHeight() > this.getHeight()) {
return -1;
} else if (o.getHeight() < this.getHeight()) {
return 1;
}
return 0;
// simpler: return height-o.getHeight()
}
//
// ----------- GETTER METHODS ------------
// (also Accessor methods)
/**
* @return the height of the chicken, in centimeters
*/
public int getHeight() {
return height;
}
/**
*
* @return the weight of the chicken, in pounds
*/
public double getWeight() {
return weight;
}
/**
*
* @return
*/
public String getName() {
return name;
}
/**
*
* @return whether this chicken is a female
*/
public boolean isFemale() {
return isFemale;
}
//
// ------------- MUTATORS -----------
//
/**
* Feed the chicken. Side effects: the chicken grows in weight and height.
*/
public void feed() {
weight += .2;
height += 1;
}
//
// ------------- SETTERS ----------
//
/**
* Changes the name of the chicken
*
* @param n
* the name of the chicken
*/
public void setName(String n) {
name = n;
}
/**
* Sets the height of the chicken
*
* @param h
* the height of the chicken, in cm
*/
public void setHeight(int h) {
height = h;
}
/**
* Sets the weight of the chicken
*
* @param w
* the weight of the chicken, in pounds
*/
public void setWeight(double w) {
weight = w;
}
/**
* @param args
* the command-line arguments
*/
public static void main(String args[]) {
Chicken one = new Chicken("Fred", 38, 2.0, false);
Chicken two = new Chicken("Sallie Mae", 45, 3.0);
Chicken female = new Chicken("Lucille", 38, 2.0, true);
System.out.println(one.getName() + " weighs " + one.getWeight());
one.feed();
System.out.println(one.getName() + " ate, so he now weighs "
+ one.getWeight());
System.out.print(two.getName() + " is now ");
two.setName("The Divine Miss Sallie Mae");
System.out.println(two.getName());
// test toString method
System.out.println(one);
System.out.println(one + " equal to " + two + "? " + one.equals(two));
System.out.println(one + " equal to " + female + "? "
+ one.equals(female));
System.out.println(two + " equal to " + two + "? " + two.equals(two));
Chicken[] chickens = new Chicken[3];
chickens[0] = one;
chickens[1] = two;
chickens[2] = female;
System.out.println("\nSorted chickens...");
// sort the chickens
Arrays.sort(chickens);
// print the chickens in sorted order (by height)
for (Chicken c : chickens) {
System.out.println(c);
}
}
}
./Deck.java 4/7
[top][prev][next]
package cards;
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 variable as an interface variable, List.
// 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() {
// Assign the List variable to the ArrayList implementation.
deck = new ArrayList<>();
restart(true);
}
/**
* Creates a new deck of cards
*
* @param shuffle
* - true if the cards should be shuffled
*/
public Deck(boolean shuffle) {
deck = new ArrayList<>();
restart(shuffle);
}
/**
* Restart the deck from the beginning.
*
* @param shuffle
* - true if the cards should be shuffled
*/
public void restart(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 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 that are drawn (thus from the deck.
*
* @param numCards
* @return a list of cards (of the specified size)
*/
public List<Card> deal(int numCards) {
List<Card> hand = new ArrayList<>();
for (int i = 0; i < numCards; i++) {
hand.add(draw());
}
return hand;
}
/**
* @param args
*/
public static void main(String[] args) {
Deck d = new Deck();
d.display();
}
}
./FindDuplicates2.java 5/7
[top][prev][next]
import java.util.HashSet;
import java.util.Set;
/**
* From the array of command-line arguments, identify the duplicates
*
* @author CSCI209 Class
*
*/
public class FindDuplicates2 {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("This program finds the duplicates in the command-line arguments.");
Set<String> nonDuplicates = new HashSet<>();
for( String string : args ) {
if( nonDuplicates.contains(string) ) {
System.out.println("found duplicate " + string);
} else{
nonDuplicates.add(string);
}
}
}
}
./FindDuplicates3.java 6/7
[top][prev][next]
import java.util.HashSet;
import java.util.Set;
/**
* From the array of command-line arguments, identify the duplicates
*
* @author CSCI209 Class
*
*/
public class FindDuplicates3 {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("This program finds the duplicates in the command-line arguments.");
Set<String> nonDuplicates = new HashSet<>();
for( String string : args ) {
if( ! nonDuplicates.add(string) ) {
System.out.println("found duplicate " + string);
}
}
}
}
./FindDuplicates.java 7/7
[top][prev][next]
import java.util.HashSet;
import java.util.Set;
/**
* From the array of command-line arguments, identify the duplicates
*
* @author CSCI209 Class
*
*/
public class FindDuplicates {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("This program finds the duplicates in the command-line arguments.");
}
}
Generated by GNU Enscript 1.6.6.