Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

how do I get this to compile? Currently there is an issue on class Module02 outputting random colors for the uno cards and random suit

how do I get this to compile? Currently there is an issue on class Module02 outputting random colors for the uno cards and random suit for the poker card

the expected output should be

Sample Output (Due to Randomness your output will not be the same though the format will be the same):

[UnoCard: 7 YELLOW, PokerCard: 1, PokerCard: 11, PokerCard: 1, PokerCard: 3, UnoCard: 9 RED, UnoCard: 6 BLUE, UnoCard: 6 BLUE, UnoCard: 2 GREEN, PokerCard: 1, UnoCard: 12 YELLOW, PokerCard: 10, UnoCard: 5 BLUE, PokerCard: 6, UnoCard: 8 RED, PokerCard: 3, UnoCard: 2 GREEN, UnoCard: 9 RED, PokerCard: 12, PokerCard: 5] I just played PokerCard: 10 I just played PokerCard: 11 AND THE WINNER IS....PokerCard: 12 AND THE WINNER IS....PokerCard: 12 IT'S A TIE!!! I just played PokerCard: 1 AND THE WINNER IS....PokerCard: 3 IT'S A TIE!!! AND THE WINNER IS....PokerCard: 5 AND THE WINNER IS....PokerCard: 6 AND THE WINNER IS....UnoCard: 12 YELLOW Drawing 1 Playing UnoCard: 2 GREEN Drawing 1 Playing UnoCard: 6 BLUE Playing UnoCard: 6 BLUE Drawing 1 Drawing 1 Playing UnoCard: 9 RED Playing UnoCard: 9 RED

the code I have so far is as follows

please see what I am missing

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class Module02 { public static void main(String[] args) { ArrayList cards = new ArrayList(); Random rand = new Random(); for (int i = 0; i < 10; i++) { cards.add(new UnoCard(rand.nextInt(12) + 1, getRandomColor())); cards.add(new PokerCard(rand.nextInt(12) + 1, getRandomSuit())); } for (int i = 0; i < 5; i++) { Collections.shuffle(cards); } System.out.println(cards); Collections.sort(cards); for (int i = 0; i < cards.size(); i++) { System.out.println("Card #" + i); if (i == 0) { cards.get(i).play(cards.get(i)); } else { cards.get(i).play(cards.get(i - 1)); } } } private static String getRandomColor() { String

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public abstract class Card implements Comparable { protected int value; public Card(int value) { this.value = value; } @Override public int compareTo(Card other) { return this.toString().compareTo(other.toString()); } @Override public boolean equals(Object other) { if (other instanceof Card) { return this.toString().equals(other.toString()); } return false; } @Override public String toString() { return this.getClass().getSimpleName() + ": " + value; } public void play(Card topCard) { if (topCard.equals(this)) { System.out.println("I just played a " + this.toString()); } else { int comparison = this.compareTo(topCard); if (comparison > 0) { System.out.println("AND THE WINNER IS..... " + this.toString()); } else if (comparison < 0) { System.out.println("AND THE WINNER IS..... " + topCard.toString()); } else { System.out.println("IT'S A TIE!!"); } } } public int getValue() { return value; } }

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class PokerCard extends Card {

public enum Suit { SPADES("\u2660"), HEARTS("\u2665"), CLUBS("\u2663"), DIAMONDS("\u2666"); private final String symbol;

Suit(String s) { symbol = s; }

@Override public String toString() { return symbol; } }

private Suit suit;

public PokerCard(int v, Suit s) { super(v); suit = s; }

public Suit getSuit() { return suit; }

@Override public String toString() { return "PokerCard: " + super.getValue() + suit.toString(); } }

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class UnoCard extends Card { private String color; public UnoCard(int value, String color) { super(value); this.color = color; } @Override public void play(Card topCard) { if (!(topCard instanceof UnoCard)) { super.play(topCard); } else { UnoCard topUnoCard = (UnoCard) topCard; if (this.color.equals(topUnoCard.color) || this.value == topUnoCard.value) { System.out.println("Playing " + this.toString()); } else { System.out.println("Drawing 1"); } } } public String getColor() { return color; } @Override public String toString() { return super.toString() + " " + color; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 2 Lncs 13427

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124251, 978-3031124259

More Books

Students also viewed these Databases questions