So my project is about creating a table that adds statistic comparisons to a selection sort table. I've done the bulk of the work but am stuck at this one part that asks me to It should add "comparisons" to "totalComparisons". It should compare "comparisons" to "minComparisons" and set "minComparisons" to whichever is
smaller. It should also set "maxComparisons" in an analogous fashion. The words in quotes are variables. I know I need to write an if statement but I have no idea how to write it. Can somebody show me how to do the part where you compare "comparisons" to "minComparisons" and set "minComparisons" to whichever is
smaller.
My code so far:
private static void endStatistics(){
totalComparisons += comparisons;
if (comparisons ){
}
thanks for any help.
Try this:
if(comparisons < minComparisons)
minComparisons = comparison;
if (comparisons <= minComparisons ){
minComparisons = comparisons;
}
if (minComparisons <= comparisons ){
minComparisons = minComparisons;
}
You don't actually need the second if statement though, just to put it here to show you the whole picture.
Related
I am trying to add a random number to a set. If it is already in the set, then the loop should continue and try again.
Which is better practice,
do {
nextChosenInt = rand.nextInt(48) + 1;
addFailed = !chosenInts.add(nextChosenInt);
}
while (addFailed);
or
do {
nextChosenInt = rand.nextInt(48) + 1;
}
while (!chosenInts.add(nextChosenInt));
I would vote for the second option here, with a slight change.
You are utilising the fact that the .add method returns a boolean which is a beneficial, in that you don't need to have a redundant flag variable which, from the context of your code, serves no other purpose than just terminating the loop.
Personally however I would choose to extract:
!chosenInts.add(nextChosenInt)
into its own method which is more descriptive and readable. By doing this, you are allowing anyone to understand this condition with no prior knowledge of the Collections API.
According to Java documentation, "if this set already contains the element, the call leaves the set unchanged and returns false." Regardless of when you add to the set, if the set already contains the element, it would remain unchanged. Thus, it doesn't really make a difference in performance.
Neither since sets can't hold duplicates. Just do it until set has a desired number of elements. But you could also do something like this.
Random rand = new Random();
int next = 50;
for (int i = 0; i < 10; i++) {
next = rand.nextInt(48) + next+1;
set.add(next);
}
They are all unique the first time (but not entirely random). To add a number that is not in the set, the following is possible.
int size = set.size();
while (set.size() == size) {
set.add(rand.nextInt(48));
}
I am creating a game board. I need it to reveal a the selected column after a user input, while the rest of the columns still print as "X". This game holds values I have set in each column, but does not print them on the screen. When the user selects a column, I need it to print showing the value that column is holding while the rest of the columns still print "X" so they do not reveal what they have. I am new to this, thank you for your help.
This is the function where I think the problem is. If you look, you will see that I have the if statement "if (isCovered) - then I want it to print the covered version. Then the "else" - which is where I want it to print just the one that was guessed as its actual value. I have tried multiple ways of achieving this with no luck. Is there are way to make it like (!isCovered)? But that doesn't work, because it states it needs to be an array and the function "!" does not work. Right now it just seems like it never prints the "else" statement at all. I have functions that take the user input and compare them to "isCovered" and they work correctly, because the piece moves on the board as it should. I just cannot get it to print the actual value instead of an "X". Thank you for any help and if further information would be helpful, please let me know. It is due today unfortunately I only had a few days to work on it and have been working constantly on it.
public static void PrintRevealBoard(int[][] myArray,Boolean[][] isCovered)
{
int i, j;
for (i = 0; i<myArray.length ; i++ ) { // array.length = max rows
System.out.print((i+1) + " ");
for(j = 0; j <myArray[0].length; j++) { // array[0].length = max
cols
if(isCovered[i][j]){
System.out.print(GetRollColorCovered(myArray[i][j]) + " ");
} else {
System.out.print(GetRollColor(myArray[i][j]) + " ");
}
}
your main module is kinda messy. And I don't know how GetRollColor(dice) works. Anyway as I understand you have a two dimensional array and you want to show only a specific value. Seems like u want to show the entire input column.
use this to update isRevealed() after the input of inputCol.
public static Boolean[][] updateRevealed(Boolean[][] isRevealed, int inputCol){
for(int i=0;i<isRevealed[inputCol].length;i++)
isRevealed[inputCol][i] = true;
return isRevealed;
}
update like this,
isRevealed = updateRevealed(isRevealed,inputCol);
your printRevealBoard is almost correct. Just remove the first line. It doesn't make sense and you don't want it as I see
int isRevealed = inputCol;
I don't know how your array looks like. But because of the first for loop u will definitely get an
index out of bounds exception
loop runs until I becomes myarray.length. and in the next loop you access index I of myArray. Exception will be thrown if I=myArray.length. u gotta fix it. If any problem occurs lemme know.
thankyou
edit:
try this for printRevealBoard
public static void printRevealBoard(char[][] myarray , Boolean[] []isRevealed){
for(int i=0;i<myarray.length;i++){
for(int j=0;j<myarray[0].length;j++){
if (isRevealed[i][j]) System.out.print(myArray[i][j] + " ");
else System.out.print("* ");
}
System.out.println();
}
}
So i have this code-generated table and i've been trying to get this code to work, basically i want to know how many rows have the word so i can later work with that number of elements, this code is like not working maybe you guys can see the error?
Code:
public int UntilArraySearch (String filtro){
int tope=0;
for(int i = 0;i<mTableLayout.getChildCount();i++){
if(data[i].productName.contains(filtro) || data[i].productCode.contains(filtro))
tope++;
}
return tope;
}
And this error pops up:
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=52; index=52
I get this error whenever i run the function
Your loop would work only if mTableLayout.getChildCount() is <= data.length and it seems that it is not, because if it was the case then i would not exceed the max index for data.
Since you want to iterate through data, why don't you write the loop like this:
for(int i = 0;i<data.length;i++){
if(data[i].productName.contains(filtro) || data[i].productCode.contains(filtro))
tope++;
}
and check if this is the expected result?
If you are searching through data, then you might as well iterate over the length of the data array. Whether or not your view (mTableLayout) properly reflects the model (data), this operation can better isolate itself from the view by only considering the model which is a good practice to follow in general.
public int UntilArraySearch (String filtro){
int tope=0;
for(int i = 0;i<data.length;i++){
if(data[i].productName.contains(filtro) || data[i].productCode.contains(filtro))
tope++;
}
return tope;
}
I have built a method which takes two datasets: dataset1 and retirementSimpleData.
It matches the two datasets based on a primary key/number, defined as cnum, in the code below.
I wanted to return the value of the difference between the getAssets value and the getSums value, and this is working, except for one little problem.
Some of the cnums that exist in dataset1 don't exist in retirementSimpleData. Similarly, some cnums which may exist in retirementSimpleData may not exist in
dataset1. This is resulting in no data being returned for that cnum.
I would like to implement two passes at the end which check in one direction to see if I missed anything. The second pass would check in the opposite direction.
However, not sure how I would go about implementing this.
public void getReportData(int index) {
String date1 = Util.dateTimeToShortString(reportDate);
String date2 = reportDao.getPreviousRetirementDate(date1);
List<SurveyCompareAssetsCheckData> dataset1 = reportDao.getSurveyCheckCompareAssetsData(date1);
List<RetSurveyAssets> retirementSimpleData = reportDao.findRetSurveyByDate(date1);
for (SurveyCompareAssetsCheckData surveyCompareAssetsCheckData : dataset1) {
for (RetSurveyAssets surveyCompareAssetsCheckData2 : retirementSimpleData) {
if (surveyCompareAssetsCheckData.getCnum() == surveyCompareAssetsCheckData2.getCnum()) {
surveyCompareAssetsCheckData.setRetirementsimple(surveyCompareAssetsCheckData2.getSums());
surveyCompareAssetsCheckData.setDifference(surveyCompareAssetsCheckData.getAssets() - surveyCompareAssetsCheckData2.getSums());
Caveat: dataset1 and retirementSimpledata both use existing SQL pulls which I am not allowed to touch, otherwise I would have simply defined new SQL for these methods in my "DAOImpl." Therefore, I have to work with the data I am getting, and programmatically check for this.
Below, is the report which is being generated with my code. As you can see, I am ending up with zeros, which is showing the difference (incorrectly) as zeros, because Cnum #45, in this example simply doesn't exist in the second dataset (retirementSimpleData)
What is the datatype of Cnum, if it is int then default value is Zero.
You have to add else-if condition to check for example:
else if (surveyCompareAssetsCheckData2.getCnum()== 0){
-------- logic here --------------------
}
else if (surveyCompareAssetsCheckData.getCnum() ==0){
----------logic here -----------
}
OK, so I created a console app that, among other things, takes an array of numbers and prints them out one by one, line by line. Now, I have to take the class that I created for that console app, and pop it into a separate GUI app we're creating. I have all of the other methods working fine, but for the life of me I cannot get the array method to print out correctly. It just gives me the last number I typed into the text field. I'm hoping someone can give me a nudge to help me figure this part out so I can move along, and get to the whole SpringLayout stuff, (the main part of the new assignment) I am limited in what I can show you here because this is a current assignment, so I will have to stick to this stuff as specifically as I can. And please, don't just post the code as an answer, (because then I can't use it), thanks.
Here's what I had for my original project for the array method:
int [] getArray(int x)
{
breakUpNum(x);
return numAry;
}
From there, inside my new class I have this, in an attempt to get it to print:
private class ButtonTest implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
Lab1 tester = new Lab1();
int[] test4 = tester.getArray(num);
for(int i = 0; i < test4.length; i ++)
{
crossTest.getArrCross.setText("" + test4[i]);
}
}
}
Any help pointing me in the right direction would be greatly appreciated, thanks!
setText does just that, sets the text you pass to as the current text content, it does not append it.
If you were to use JTextArea, you could use it's append method...however, for a JTextField you need to have a different approach.
Now you could use getArrCross.setText(getArrCross.getText() + test4[i])...but to quite frank, that's rather inefficient, as each call to setText is going to stage a paint event...
StringBuilder sb = new StringBuilder(128);
for(int i = 0; i < test4.length; i ++)
{
sb.append(test4[i]);
}
crossTest.getArrCross.setText(sb.toString());
Now, if you want to separate each element, you need to add
if (sb.length() > 0) {
sb.append(", ");
}
Before sb.append(test4[i]);
The last bit of actionPerformed in the for loop isn't working right. setText replaces the current text with its argument, and it doesn't seem like you want to do that. To fix it, replace the line in the for loop with this:
crossTest.getArrCross.setText(crossTest.getArrCross.getText() + test4[i]);