Question

In: Computer Science

● Change the Deck class into a Singleton class. ● Change the main method to get...

● Change the Deck class into a Singleton class.

● Change the main method to get the Deck instance rather than creating a new object.

● The program should print all 52 cards in random order

import java.util.*;

enum Suit {
SPADES,
HEARTS,
CLUBS,
DIAMONDS
}

class Card {
public Card(Suit s, int n) {
suit = s;
if((n < 2) || (n > 14)) {
throw new IllegalArgumentException( );
}
number = n;
}

public void print( ) {
switch(number) {
case 11:
System.out.print("Jack");
break;
case 12:
System.out.print("Queen");
break;
case 13:
System.out.print("King");
break;
case 14:
System.out.print("Ace");
break;
default:
System.out.print(number);
break;
}
System.out.print(" of ");
switch(suit) {
case SPADES:
System.out.println("spades.");
break;
case HEARTS:
System.out.println("hearts.");
break;
case CLUBS:
System.out.println("clubs.");
break;
case DIAMONDS:
System.out.println("diamonds.");
break;
}
}

private Suit suit;
private int number;
}


class Deck {
public Deck( ) {
cards = new ArrayList<Card>( );

// build the deck
Suit[] suits = {Suit.SPADES, Suit.HEARTS, Suit.CLUBS, Suit.DIAMONDS};
for(Suit suit: suits) {
for(int i = 2; i <= 14; i++) {
cards.add(new Card(suit, i));
}
}

// shuffle it!
Collections.shuffle(cards, new Random( ));
}

public void print( ) {
for(Card card: cards) {
card.print( );
}
}

private List<Card> cards;
}


public class SingletonExercise {
public static void main(String args[]) {
Deck deck = new Deck( );
deck.print( );
}
}

Solutions

Expert Solution

import java.util.*;

enum Suit {
   SPADES,
   HEARTS,
   CLUBS,
   DIAMONDS
}

class Card {
   public Card(Suit s, int n) {
       suit = s;
       if((n < 2) || (n > 14)) {
           throw new IllegalArgumentException( );
       }
       number = n;
   }

   public void print( ) {
       switch(number) {
           case 11:
           System.out.print("Jack");
           break;
           case 12:
           System.out.print("Queen");
           break;
           case 13:
           System.out.print("King");
           break;
           case 14:
           System.out.print("Ace");
           break;
           default:
           System.out.print(number);
           break;
       }
       System.out.print(" of ");
       switch(suit) {
           case SPADES:
           System.out.println("spades.");
           break;
           case HEARTS:
           System.out.println("hearts.");
           break;
           case CLUBS:
           System.out.println("clubs.");
           break;
           case DIAMONDS:
           System.out.println("diamonds.");
           break;
       }
   }

   private Suit suit;
   private int number;
}


class Deck {

   private static Deck instance = null;

   private Deck( ) {
       cards = new ArrayList<Card>( );

// build the deck
       Suit[] suits = {Suit.SPADES, Suit.HEARTS, Suit.CLUBS, Suit.DIAMONDS};
       for(Suit suit: suits) {
           for(int i = 2; i <= 14; i++) {
               cards.add(new Card(suit, i));
           }
       }

// shuffle it!
       Collections.shuffle(cards, new Random( ));
   }

   public static synchronized Deck getInstance()
   {
       if(instance == null)
           instance = new Deck();
       return instance;
   }

   public Object clone() throws CloneNotSupportedException {
       throw new CloneNotSupportedException("Clone is not allowed.");
}

   public void print( ) {
       for(Card card: cards) {
           card.print( );
       }
   }

   private List<Card> cards;
}


public class SingletonExercise {
   public static void main(String args[]) {
       Deck deck = Deck.getInstance();
       deck.print( );
   }
}


Related Solutions

Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of Poem objects, read in the information from PoemInfo.txt and create Poem objects to populate the ArrayList. After all data from the file is read in and the Poem objects added to the ArrayList- print the contents of the ArrayList. Paste your PoemDriver.java text (CtrlC to copy, CtrlV to paste) into the open space before. You should not change Poem.java or PoemInfo.txt. Watch your time...
Error: Main method is not static in class ArrayReview, please define the main method as: public...
Error: Main method is not static in class ArrayReview, please define the main method as: public static void main(String[] args) please help me fast: import java.util. Random; import java.util.Scanner; //ArrayReview class class ArrayReview { int array[];    //constructor ArrayReview (int n) { array = new int[n]; //populating array Random r = new Random(); for (int i=0;i<n; i++) array[i] = r.nextInt (); } //getter method return integer at given index int getElement (int i) { return array[i]; }    //method to...
create a public class and a main class to deletebyStudentID number with get ID and getStudentByName...
create a public class and a main class to deletebyStudentID number with get ID and getStudentByName in public class. when the user is prompted to search for the student and delete them by their. ID has seven digits and 6 students each. Ex. Delete student by their ID         Enter students name and their ID         Vanessa,         1111-111         Vanessa is now deleted
Required components: A controller class with Java main() method and a PaymentCalculator class to hold the...
Required components: A controller class with Java main() method and a PaymentCalculator class to hold the data and methods for the application. Scenario/Information: If you carry a balance on a credit card, it can be nice to see how long it would take to payoff the card. For this scenario, you should design a Java application the prints a credit card payment schedule. Inputs for your program should include: Customer’s name (first and last), the account number (as an integer),...
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the...
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the main method), define and implement the following methods: public static void printArray. This method accepts a two-dimensional double array as its argument and prints all the values of the array, separated by a comma, each row in a separate line. public static double getAverage. This method accepts a two-dimensional double array as its argument and returns the average of all the values in the...
Given the main method of a driver class, write a Fraction class. Include the following instance...
Given the main method of a driver class, write a Fraction class. Include the following instance methods: add, multiply, print, printAsDouble, and a separate accessor method for each instance variable. Write a Fraction class that implements these methods: add ─ This method receives a Fraction parameter and adds the parameter fraction to the calling object fraction. multiply ─ This method receives a Fraction parameter and multiplies the parameter fraction by the calling object fraction. print ─ This method prints the...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class,...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. -Add a method addToCollection. In this method, add one to collected...
How would I get this java code to work and with a main class that would...
How would I get this java code to work and with a main class that would demo the rat class? class rat { private String name; private String specialAbility; private int TotalHealth; private int shieldedHealth; private int cooldown; public rat() { } public rat(String n,String SA,int TH,int SH,int cd) { name=n; specialAbility=SA; TotalHealth=TH; shieldedHealth=SH; cooldown=cd; } public void setname(String n) { name=n; } public String getname() { return name; } public void setability(String SA) { specialAbility=SA; } public String getability()...
Name the project pa3 [Method 1] In the Main class, write a static void method to...
Name the project pa3 [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. [Method 2]...
3. [Method 1] In the Main class, write a static void method to print the following...
3. [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. 4. [Method 2] In the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT