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
This class contains the main method. In this class you should: Ask the user for the...
This class contains the main method. In this class you should: Ask the user for the name, job tilte, and salary. Instantiate a Employee object with those parameters. Use the accessor methods to check the value of all three instance variables and print them. Ask the user to enter a percentage to raise the salary by. Use the raise method to increase the salary. Use the salary accessor check the new value of salary and print it.
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),...
Java Class Create a class with a main method. Write code including a loop that will...
Java Class Create a class with a main method. Write code including a loop that will display the first n positive odd integers and compute and display their sum. Read the value for n from the user and display the result to the screen.
You are to write a class StringPlay that has only a main method. This class contains...
You are to write a class StringPlay that has only a main method. This class contains all interaction with the user. The main method Uses a Scanner to accept user input Maintains a “current” String Uses a sentinel-controlled loop to accept multiple user commands, or exit The program displays a message asking the user to enter one of the following commands a – enter a new value for current b – padLeft asks the user for the number of characters...
Requirement: To create three classes, Account, Transaction, and the main class (with the main method). Please...
Requirement: To create three classes, Account, Transaction, and the main class (with the main method). Please closely follow the requirements below. I only need to know how to create the menu and be able to use it through out the code. Requirements for the Transaction class: A private Date data field that stores the date of the transaction  A private char data field for the type of the transaction, such as “W” for withdrawal and “D” for deposit ...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT