CardSort.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import java.util.Arrays; import java.util.Collections; import java.util.List; class PlayingCard implements Comparable<PlayingCard>{ public enum Suit {CLUBS, DIAMONDS, HEARTS, SPADES}; public enum FaceValue {ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING}; Suit suit; FaceValue value; public PlayingCard(Suit suit, FaceValue value){ this.suit = suit; this.value = value; } @Override public String toString(){ return value + " of " + suit; } @Override public int compareTo(PlayingCard o){ // First compare by suit. int suitComparison = this.suit.compareTo(o.suit); if (suitComparison != 0) return suitComparison; // Then compare by value. return this.value.compareTo(o.value); } } public class CardSort { public static void printCardList(List<PlayingCard> cards){ System.out.println("--- Card List ---"); for (PlayingCard c : cards) System.out.println(c); } public static void main(String[] args){ PlayingCard[] cardArray = {new PlayingCard(PlayingCard.Suit.DIAMONDS, PlayingCard.FaceValue.FIVE), new PlayingCard(PlayingCard.Suit.DIAMONDS, PlayingCard.FaceValue.FOUR), new PlayingCard(PlayingCard.Suit.CLUBS, PlayingCard.FaceValue.ACE), new PlayingCard(PlayingCard.Suit.CLUBS, PlayingCard.FaceValue.KING), new PlayingCard(PlayingCard.Suit.SPADES, PlayingCard.FaceValue.ACE)}; List<PlayingCard> cards = Arrays.asList(cardArray); printCardList(cards); Collections.sort(cards); printCardList(cards); } } |
PolyEx.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
abstract class GraphicObject { protected int x, y; public GraphicObject(int x, int y){ this.x = x; this.y = y; } public void moveTo(int newX, int newY){ x = newX; y = newY; } abstract String shapeName(); abstract void resize(double multiplier); public void printDetails() { System.out.print(shapeName() + " at (" + x + ", " + y + ")"); } } class Rectangle extends GraphicObject { private double side1, side2; public Rectangle(int x, int y, double side1, double side2){ super(x, y); this.side1 = side1; this.side2 = side2; } public void resize(double multiplier){ side1 *= multiplier; side2 *= multiplier; } public String shapeName() { return "Rectangle"; } public void printDetails(){ super.printDetails(); System.out.println(" with sides: " + side1 + ", " + side2); } } class Circle extends GraphicObject { private double radius; public Circle(int x, int y, double radius){ super(x, y); this.radius = radius; } public void resize(double multiplier){ radius *= multiplier; } public String shapeName() { return "Circle"; } public void printDetails(){ super.printDetails(); System.out.println(" with radius: " + radius); } } public class PolyEx{ public static void main(String[] args){ GraphicObject r = new Rectangle(0, 0, 2.0, 3.0); r.printDetails(); r.moveTo(5,5); r.resize(1.5); r.printDetails(); GraphicObject c = new Circle(0, 0, 1.5); c.printDetails(); c.moveTo(10, 10); c.resize(Math.PI); c.printDetails(); } } |