In Java . You will be implementing the Comparable and Cloneable interfaces in th
ID: 3680136 • Letter: I
Question
In Java.
You will be implementing the Comparable and Cloneable interfaces in this lab Card class: First, create a class named Card with the following properties: color:Color, value:String. The card would be used in the game of Uno. Add two constructors (no-arg constructor and a constructor that includes both properties), toString mutator and accessor methods. The toString method should use the to separate properties Your Card class should implement the Comparable interface as discussed in class Implement the compare To method that sorts by color then by value. So, if the cards have the same color, you need to look at the value to determine whether one card is less than the other. Use alphabetical order to compare color Deck Class: Create a Deck class consisting of 108 Cards represented in an ArrayListExplanation / Answer
package com.he.capillary;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JOptionPane;
public class Deck implements Cloneable {
List<Card> cards = new ArrayList<Card>(108);
public Deck() {
this.cards.add(new Card(Color.green, "0"));
this.cards.add(new Card(Color.red, "0"));
this.cards.add(new Card(Color.blue, "0"));
this.cards.add(new Card(Color.yellow, "0"));
for (int i = 1; i <= 76; i++) {
this.cards.add(new Card(Color.green, i + ""));
this.cards.add(new Card(Color.red, i + ""));
this.cards.add(new Card(Color.blue, i + ""));
this.cards.add(new Card(Color.yellow, i + ""));
}
}
public void shuffle() {
for (int j = 0; j < 7; j++) {
for (int i = 0; i < 76; i++) {
int randomNum = (int) (Math.random() * 75);
Card temp = this.cards.get(randomNum);
this.cards.set(randomNum, this.cards.get(i));
this.cards.set(i, temp);
}
}
}
@Override
public Object clone() {
Deck deck = new Deck();
deck.cards = new ArrayList<Card>(this.cards);
return deck;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 76; i++) {
sb.append(this.cards.get(i).toString() + "|");
if (((i + 1) % 15) == 0) {
sb.append(" ");
}
}
return sb.toString();
}
public void sort() {
Collections.sort(this.cards);
}
public static void main(String[] args) {
Deck deck = new Deck();
deck.shuffle();
JOptionPane.showMessageDialog(null, deck.toString(), null, JOptionPane.INFORMATION_MESSAGE);
deck.sort();
JOptionPane.showMessageDialog(null, deck.toString(), null, JOptionPane.INFORMATION_MESSAGE);
Deck clonned = (Deck) deck.clone();
clonned.sort();
JOptionPane.showMessageDialog(null, deck.toString(), null, JOptionPane.INFORMATION_MESSAGE);
}
}
package com.he.capillary;
import java.awt.Color;
public class Card implements Comparable<Card> {
private Color color;
private String value;
public Card(Color color, String value) {
super();
this.color = color;
this.value = value;
}
public Card() {
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "Card [color=" + getColorName() + ", value=" + value + "]";
}
@Override
public int compareTo(Card o) {
int result = 0;
if (o.getValue() != null && this.getValue() != null) {
result = ((Integer)Integer.parseInt(o.getValue())).compareTo(Integer.parseInt(this.getValue()));
if(result == 0){
return o.getColorName().compareTo(this.getColorName());
}
}
return result;
}
public String getColorName(){
if(Color.green == this.color){
return "green";
}else if(Color.yellow == this.color){
return "yellow";
}else if(Color.blue == this.color){
return "blue";
}else{
return "red";
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.