the problem is A standard pack of cards can be represented as an array of 52 integers with each number representing a
standard card. Thus:
0 1 2 3 4 5 6 7 8 9 10 11 12.....39 40 41 42 43 44 45 46 47 48 49 50 51
We can shuffle this pack of cards by randomly generating two indexes and swapping those two cards.
For example if we randomly generate the indexes 8 and 50 and swap these two "cards" we would get
(assuming this is the first swap):
0 1 2 3 4 5 6 7 50 9 10 11 12.....39 40 41 42 43 44 45 46 47 48 49 8 51
After a suitable number of swaps have been made the pack can be said to be "shuffled".
Design and implement a Java programme which takes as input the number of desired swaps, outputs an
ordered set of cards which are before the shuffling and output the set of cards after the shuffling. The
number of swaps must be at least 1 (otherwise we would not be doing any "shuffling" ) and not more
than 1000 (as any more than this would be ridiculous).
i want to use four class to solve this problem. below is my code. can any one help me to change the wrong code into the right one. thank you. -
public class Card{
-
private String value_;
-
private String suit_;
-
public Card(String value, String suit){
-
value_=value;
-
suit_=suit;
-
}
-
public String toSting()
-
{
-
return value_ + "-" + suit_;
-
}
-
}
-
-
public class CardApp{
-
public static void main (String[] args){
-
System.out.print(card);
-
newManager = new IOManager(numberOfSwaps);
-
newManager.shuffle();
-
}
-
}
-
-
import java.util.*;
-
public class IOManager{
-
private static Scanner input= new Scanner(System.in);
-
private int numberOfSwaps;
-
private int index1;
-
private int index2;
-
public void shuffle(){
-
numberOfSwaps = input.nextInt();
-
int index1 = 0;
-
int index2 = 0;
-
for(int i=0;i<numberOfSwaps;i++){
-
index1=(int)(Math.random()*52);
-
index2=(int)(Math.random()*52);
-
newPack = new Pack();
-
newPack.swap(index1,index2);
-
}
-
}
-
}
-
-
public class Pack{
-
-
private Card[] cardArray_;
-
public Pack(){
-
cardArray_=newCard[52];
-
}
-
public void initialise(){
-
String value,suit;
-
for(int i=0;i<52;i++){
-
suit=i/13;
-
value=i%13;
-
Card card=new Card(value,suit);
-
cardArray_[i]=card;
-
}
-
public void swap(int index1, int index2){
-
Card card=cards[index1];
-
cards[index1]=cards[index2];
-
cards[index2]=card;
-
}
-
}
-
}
-
2 3349
the problem is A standard pack of cards can be represented as an array of 52 integers with each number representing a
standard card. Thus:
0 1 2 3 4 5 6 7 8 9 10 11 12.....39 40 41 42 43 44 45 46 47 48 49 50 51
We can shuffle this pack of cards by randomly generating two indexes and swapping those two cards.
For example if we randomly generate the indexes 8 and 50 and swap these two "cards" we would get
(assuming this is the first swap):
0 1 2 3 4 5 6 7 50 9 10 11 12.....39 40 41 42 43 44 45 46 47 48 49 8 51
After a suitable number of swaps have been made the pack can be said to be "shuffled".
Design and implement a Java programme which takes as input the number of desired swaps, outputs an
ordered set of cards which are before the shuffling and output the set of cards after the shuffling. The
number of swaps must be at least 1 (otherwise we would not be doing any "shuffling" ) and not more
than 1000 (as any more than this would be ridiculous).
i want to use four class to solve this problem. below is my code. can any one help me to change the wrong code into the right one. thank you. -
public class Card{
-
private String value_;
-
private String suit_;
-
public Card(String value, String suit){
-
value_=value;
-
suit_=suit;
-
}
-
public String toSting()
-
{
-
return value_ + "-" + suit_;
-
}
-
}
-
-
public class CardApp{
-
public static void main (String[] args){
-
System.out.print(card);
-
newManager = new IOManager(numberOfSwaps);
-
newManager.shuffle();
-
}
-
}
-
-
import java.util.*;
-
public class IOManager{
-
private static Scanner input= new Scanner(System.in);
-
private int numberOfSwaps;
-
private int index1;
-
private int index2;
-
public void shuffle(){
-
numberOfSwaps = input.nextInt();
-
int index1 = 0;
-
int index2 = 0;
-
for(int i=0;i<numberOfSwaps;i++){
-
index1=(int)(Math.random()*52);
-
index2=(int)(Math.random()*52);
-
newPack = new Pack();
-
newPack.swap(index1,index2);
-
}
-
}
-
}
-
-
public class Pack{
-
-
private Card[] cardArray_;
-
public Pack(){
-
cardArray_=newCard[52];
-
}
-
public void initialise(){
-
String value,suit;
-
for(int i=0;i<52;i++){
-
suit=i/13;
-
value=i%13;
-
Card card=new Card(value,suit);
-
cardArray_[i]=card;
-
}
-
public void swap(int index1, int index2){
-
Card card=cards[index1];
-
cards[index1]=cards[index2];
-
cards[index2]=card;
-
}
-
}
-
}
-
Why don't you sort the cards?Here is a sample code for sorting cards
public void sort() {
for (int i = 0; i < N; i++) {
for (int j = i; j > 0; j--) {
if (cards[j-1].less(cards[j])) {
Card swap = cards[j];
cards[j] = cards[j-1];
cards[j-1] = swap;
}
}
}
}
BE CAREFUL THAT when you compare 2 cards you have to design a method to compare the two corresponding cards.
BigDaddyLH 1,216
Recognized Expert Top Contributor
> can any one help me to change the wrong code into the right one.
You shouldn't just toss assignments into this forum and expect members to do them for you. What do you think is wrong?
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Geoff Berrow |
last post by:
I may have mentioned that I run an Introduction to PHP course at a local
college (very basic - I'm no PHP expert). Well, one of my students was
doing really well so I set him some extension work. The task was to use
PHP to generate a random bingo card. The standard UK card has nine rows
and three columns. Each row has five numbers. All numbers are
different, out of a pool of 90.
I asked my student to design one card. He came back a...
|
by: JC |
last post by:
I am very new to programming and learning on my own.
Why do I keep getting duplicate values using this code? I want to shuffle a
deck of 52 cards. The logic seems right to me.
Randomize
For C = 0 To 1000
C1 = Cards(Int(Rnd * 52)) ' returns a number from 0 to 51
|
by: J. Campbell |
last post by:
I'm trying to make a class that represents a deck of cards and a
dealer for use in a card game. I'm fairly novice. The code compiles
(Dev c++(gcc 3.3.1)) without complaint, however it acts strange when I
run it. (In windows), when I double-click or run the prog from the
IDE, it displays the deck, then exits. When I run it from a console
window, if I type the filename eg "cards.exe", it displays the cards
as expected but doesn't pause...
|
by: Ken Smith |
last post by:
I have a little video poker game I created in Javascript.
It uses Tables and inner html stuff - for example: (psudo-code)
imagePicked=random card image (2h.gif);
getElementById(cellNum).innerHTML="<img src='imagePicked'>";
I use small 1k.gif images for the cards.
Sometimes on the net the 1k.gifs take a while to load & kills bandwith
traffic.
|
by: ericvw |
last post by:
How would I shuffle a static array of 52 cards that you input an
integer, n, into a function and it takes the first n cards as the left
segment and the remaining as the right. Then it shuffles this deck
starting from the first of the right segment, then first of the left,
second of the right, second of the left. Once one side is exhausted it
fills in the rest of the shuffle with the other remaining segment. Any
suggestions?
| |
by: nabeel.girgis |
last post by:
I'm creating a deck class which uses a card class. The card class is
correct and works perfectly. One fo my member functions is to draw a
card, but I'm having trouble removing the top card after it has been
dealt. I have tested this in a seperate program to see if it work and
it does. But I get this error when I try to compile my deck class:
'std::_Vector_iterator<_Ty,_Alloc>...
|
by: Limpor |
last post by:
Hello, I am new to learning java, and i am trying to build the class for a calculation card game, unfortunately i can't get the public Card top() and Card takeTop() method in the Stock class. Can someone helps me.
Thanks!!
-----------------------------------------------------------------------
The code for the Stock class:
-----------------------------------------------------------------------
public class Stock
{
private Deck deck = new...
|
by: Arun Nair |
last post by:
Can any one help me with this im not getting it even after reading
books because there is not much of discussion anywhere
a>
Implement a calss that represents a playing card. The class should
implement the following methods:
_ _ init _ _ (self, rank, suit) Creates a card.
rank is an integer in range of 1 to 13 (Ace:1, King 13), suit is a
character in set {'h','d','c','s'}
getRank(self) Returns the rank of the card
|
by: JNeko |
last post by:
Hello all, awesome site! I guess I am technically not a beginner in JAVA, but from my code you would not realize it! I don’t expect anyone to help me with this, but I figure I might as well as try and ask. Any help is really appreciated; this should be a piece of cake…driving me crazy.
I am writing a simple program (from a book for fun) that creates a deck of cards, shuffles them, gives the user a card, and asks if the next card will be...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |