Can anyone please explain to me the following Java code? [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
The following code shuffles playing cards in a game. I have to explain what the code does in a presentation. I am a newbie in Java coding, so can anyone please explain to me the following code line by line:
// Deck shuffling method
public void shuffleDeck() {
//Seed the Random instance with nanoTime
Random random = new Random(System.nanoTime());
for(int i = 0; i < 52; i++) {
int swapIndex = random.nextInt(52);
if (swapIndex != i) {
PlayingCard temp = cardDeckArray[i];
cardDeckArray[i] = cardDeckArray[swapIndex];
cardDeckArray[swapIndex] = temp;
}
}
cardIndex = 0; //Next card to be pulled off the deck
}

This is a shuffle of a deck, at each iteration you swap two cards at random, and it will result in some random permutation of the deck.
However, this algorithm is flawed and biased, there are some permutations that are more likely to be generated than others, to use an unbiased shuffle, you should use fisher-yates shuffle (which is basically the same idea, but generate a random number between i to 52, instead between 0 to 52)
The reason and results of this bias is discussed thoroughly in the thread: What distribution do you get from this broken random shuffle?

Related

sort integer array in java class fairly basic from 2 to 18 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 years ago.
Improve this question
i need to sort an array of the type intgere in a basic way (nothing too complex) for a school computer science project. It would be nice if sb could give me not only a one sentence answer waht to consider but also some code i can work with.
thanks a lot.
You could've either googled it but since you didn't, use this bubblesort method:
boolean swapped; // to notice swaps during a pass
do {
swapped = false;
for (int i=1; i<a.length; i++)
if (a[i-1] > a[i]) {
// Swap!
int swap = a[i];
a[i] = a[i-1];
a[i-1] = swap;
swapped = true;
}
} while (swapped); // another pass if swaps happened
It swaps the ints next to each other until you have them sorted from the smallest to the greatest number. If you want it the other way around simply swap the ">" with a "<".
I hope that will help you.

JAVA Project Euler P. #8, Find greatest product from 1000 digit number [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
https://projecteuler.net/problem=8 link to problem
Find the thirteen adjacent digits in the 1000-digit number that have the greatest product.(number is in the code as String num)
The answer i am getting is "9205903071867879424" (wrong answer)
Please point out the mistakes in my code and also suggest your solution to solve the problem efficiently as possible.
I read the other threads about this problem but couldn't understand them and also there weren't enough efficient solutions.
public static void main(String[] args){
String num = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
long product=1;
long greatestp=0;
long limit=13;
for (int i=0; i<num.length()-13; i++){
product = 1;
for (int j=0; j<limit; j++ ){
product = (long) (product * (int) (num.charAt(j+i)));
}
if (greatestp<product){
greatestp = product;
}
}
System.out.println(greatestp);
}
P.S. I'm a beginner at JAVA, I would appreciate if you explain your solution in detail.
charAt gives you the ascii charcode, not the actual digit. Therefore the fastest way is to subtract the value of '0' from it:
product = product * (num.charAt(i+j) - '0');

How do I insert data of a for loop in a specific index? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a "for" loop that is creating a variable each time through the loop. I am attempting to insert the results into an empty array at the index of the "i" in the loop. From the best I can tell it seems I need to create a ArrayList vs. an Array to make this happen.
int varNum = 10;
Array someArr = new Array ();
for (int i = 0; i < 10; i++){
varNum = varNum +i;
someArr[i] = varNum;
}
On the first loop I want the 10 to be inserted in my array at the "0 index", 11 inserted at "1 index", 12 at the "2 Index".
**The important part is that the Array is not a set size, because I do not know how many indexes I will need in the array, so I want to add them as I needed.
If you use an ArrayList you can call add like this:
int varNum = 10;
ArrayList<Integer> someArr = new ArrayList<Integer>();
for (int i = 0; i < 10; i++)
{
varNum = varNum + i;
someArr.add(varNum);
}
This will allow you to dynamically fill the ArrayList dependent upon how many values it needs to hold.
Better use an ArrayList then, and use their .add() method
someArr.add(varNum)

How to fill a text area with random numbers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have a Java application and want to press a button to generate a 7 digit random number and put it into a text area.
Here is what I have so far:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextArea1.setText();
}
How do I do this?
i don't know what to write inside the brackets to get this random number of seven digits on the text area.
You can use a StringBuilder and append 7 random numbers between 0 and 9 using the nextInt(int n) method :
Random r = new Random();
StringBuilder sb = new StringBuilder();
for(int i = 0; i < 7; i++)
sb.append(r.nextInt(10));
jTextArea1.setText(sb.toString());
first you'll need to inport the java random number library at the top of your code, like this:
import java.util.Random;
this code will get you a random number from 1000000 to 9999999, it is a little weird, but take some time trying to figure it out
1000000 + (int)(Math.random() * ((8999999) + 1))
Try putting that between the parenthasis after setText, like this:
jTextArea1.setText(1000000 + (int)(Math.random() * ((8999999) + 1)));
For more information about the random function and how it works, looks here

Explain this part of code, poker deck class [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to learn how to make a poker game, so i searched around websites for poker projects.
This is the deck class i found.
This is the part of the code that make no sense to me.
int index_1, index_2;
for (int i = 0; i < 100; i++) {
index_1 = generator.nextInt(cards.size() - 1);
index_2 = generator.nextInt(cards.size() - 1);
temp = cards.get(index_2);
cards.set(index_2, cards.get(index_1));
cards.set(index_1, temp);
}
}
Looks like a random shuffle of the deck, picks two card positions at random. The nextInt(cards.size() - 1) will generate a random index in [0,cards.size() - 1) range:
index_1 = generator.nextInt(cards.size() - 1);
index_2 = generator.nextInt(cards.size() - 1);
swap the two cards, since you have to replace one of the cards first and you don't want to lose that object, the code uses temp to hold the first card to be replaced, cards.get(index_2) will obtain the first card object.
temp = cards.get(index_2);
The first set will replace the object in index_2 will the object from index_1 and the second set will replace the object in index_1 with the temporary object we stored earlier:
cards.set(index_2, cards.get(index_1));
cards.set(index_1, temp);
It does this procedure 100 times:
for (int i = 0; i < 100; i++)
If we assume cards.size() - 1 is the index of the last card in the deck, the nextInt call will skip the last card though, hard to know if that was intentional without more context.
What this code is doing is it's swapping two cards in the deck. It does this 100 times arbitrarily. A better way to do this is to randomly assign a card to a position in a deck. The way it is right now may never touch certain groups of cards and you may end up with a clump of cards in the same order as before in the same position. Plus it would only involve 52 moves instead of 300 (card2 -> temp, card1 -> card2, temp -> card1 ).

Categories