apines 116 Practically a Master Poster Featured Poster

This code is partial, please elaborate what is the problem...

apines 116 Practically a Master Poster Featured Poster

Can you post the code that you wrote so far?

apines 116 Practically a Master Poster Featured Poster

Ok, if you don't know how to do that, please read about the If-Then and the While loop. After you have read those you will have enough knowledge to answer the simple question I have asked you. Please show some effort.

apines 116 Practically a Master Poster Featured Poster

Lets say you have a number stored in the variable called temp. How will you know if the variable temp2 holds the same value as temp?

apines 116 Practically a Master Poster Featured Poster

First, please post all of the information in one post, you can use the "edit" button next to your name. To give you an idea of what you need to do:

import java.util.Random;
public class ArrayRandom{
   public static void main(String args[])
   {
      Random r = new Random();
      int temp;
      int arr[] = new int[20];
      for(int i = 0; i < 20; i++)
      {
	 //random numbers from 1 to 10:
         temp = r.nextInt(10) + 1; // this will give you a random number between 0 to 10.
         
         //TODO: Here you need to check and see if any of the array cells 0 to i-1  
         //contain the same number as temp, and do a loop that exits only if the number is unique. 
         
         arr[i] = temp; //here we know that the number is unique, so we can put it inside arr[i]

      }
      for(int i = 0; i < 20; i++)
      {
	 System.out.print(arr[i] + " ");
      }
  } //main
} //class
apines 116 Practically a Master Poster Featured Poster

if the init is i=1, then it runs n-1 times, because it does not run when i=n. In the examples below as far that I can see there is no difference between worst case and other cases.
Take an example of a sorting algorithm - you get an array of integers and you need to sort them from the smallest to the biggest. If our algorithm is bubble sort then the worst case scenario will be that the entire array is reversed - from biggest to the smallest - we will need to much more work that way, and our complexity will be O(n^2). The best case is that the array is already sorted, in which we will use O(n) complexity.

apines 116 Practically a Master Poster Featured Poster

If you solved the problem (by yourself, good for you) then please mark it as solved :)

apines 116 Practically a Master Poster Featured Poster

(a) i=i*2 means that the loop will iterate for log2(n) times, therefore your complexity for the first problem is O(log2(n)). All other operations such as System.out can be counted as O(1).
(b) Correct.
(c) Think how many times the recursion is called. It is like a loop iteration, all the other operations except the recursion call is O(1), the recursion is called n times, therefore the complexity will be O(n). You can write the same formula using a loop, and then you'll be able to see it more clearly.
(d) Correct.

apines 116 Practically a Master Poster Featured Poster

Ok, I will help you with part of your research and will direct you to the Java 6 API, specifically to the java.util.Random class. This should help you with the creation of the Random numbers.
After creating the random numbers, think how you make sure that the number generated is unique. Try to work with that, and if you still have trouble with your code post it here and explain where exactly you get into trouble.

apines 116 Practically a Master Poster Featured Poster

Glad to hear that it's working :)

At a glance I just found one thing that you can change to a nicer format:

int it = i.intValue();
it++;
Integer p = new Integer(it);
topCards.put(h.topCard(),p);

To

Integer p = new Integer(++i.intValue());
topCards.put(h.topCard(),p);

But it is not necessary. Glad we could help you :) please mark the thread as solved.

apines 116 Practically a Master Poster Featured Poster

Again - even if the table size is <= 2, it does not necessary means that n-1 of the cards are of the same type - if you have only type1 and type2 cards, when half is type1, the other half is type2 - you will consider it a match when it's not. The condition on the table size should let you break the loop sooner but you cannot rely only on it when considering whether a match has been made.

apines 116 Practically a Master Poster Featured Poster

I used the "trivial" solution of invoking System.getProperty("os.name") :)

Glad I could help you man, please mark the problem as solved :)

apines 116 Practically a Master Poster Featured Poster

"Documents\" + text4.getText();" is actually the string Documents"<text file> - perhaps you want "Documents\\" + text.getText(); ?

apines 116 Practically a Master Poster Featured Poster

The fact that topCards.size() <= 2 does not necessary means that there was a match - half of the cards can be type1, half type2, meaning there is no match. The expression should be that if topCards.size() > 2 then for certain you don't have a match.

apines 116 Practically a Master Poster Featured Poster

Try and explain the changes from your last code. The previous code that you have posted seemed more clear...

apines 116 Practically a Master Poster Featured Poster

What operating system are you using and to what operating system are you trying to connect? A while ago I had problems when connecting to a remote system with a different OS. You can try "\\" as well instead of "/".

apines 116 Practically a Master Poster Featured Poster

Does the sub folder exist? What happens when you try to create a new sub folder?

apines 116 Practically a Master Poster Featured Poster

I made a flowchart that will print counting numbers 1-10 automatically, and I'm supposed to print their sum and average at the end. Now, I have to convert the flowchart into three Java programs: For, While, DoWhile.

I'm having a bit of a problem on the flowchart and the programs. Can someone please help? Thanks in advance.

Btw, I'm kinda new to Java, so if ya'll can just dumb it down a bit. Lol. Not too dumb though.

First, let's go over the three kind of loops - for, while and do while

  1. The for statement
    The for loop enables you to iterate over a range of values in an easy way. Syntax:
    for (initialization; termination; increment) {
        //do something
    }

    The initialization expression initializes the loop and it is only executed when the loop begins. The termination expression is such that when it evaluates to false, the loop terminates. After each loop iteration the increment statement is invoked, incrementing the variable as we see fit. We can of course decrement it as well. Examples:

    for (int i =0; i < 10; ++i) {
        //do something
    }

    This code will initialize an integer variable named i to get the initial value of 0. The loop will iterate as long as i < 10, meaning for i-0,1,2,3,4,5,6,7,8,9 - 10 iterations. Each iteration, the value of i will be incremented by 1.

    for (int i =0; i < 10; i=i+2) {
        //do something
    }

    This time i will be incremented by 2 each …

coil commented: good explanation +1
apines 116 Practically a Master Poster Featured Poster

Oh I misunderstood your last sentence. :P

Happens :)

apines 116 Practically a Master Poster Featured Poster

Taywin - I have stated that using a try/catch in this way is bad coding, so why do you think that I support it?

apines 116 Practically a Master Poster Featured Poster

So have I.

apines 116 Practically a Master Poster Featured Poster

Assuming that each line consists of <Grade><one whitespace><Number> Then when charAt(0) is the letter, charAt(1) is the sign. If you are getting the grade separately and the number separately, then (grade.length() > 1) should tell you if there is a sign or not.

Handling it through exception unless it is absolutely necessary is bad coding in my opinion.

apines 116 Practically a Master Poster Featured Poster

Well, right now I am off to bed, but if you still can't get your code to work post it here and I'll try and give it a look tomorrow. Good luck!

apines 116 Practically a Master Poster Featured Poster
public void checkMatch(){
		for(CardHolder h: holders){
			if(topCards.containsKey(h.topCard())){
				Integer i = (Integer) topCards.get(h.topCard());
				topCards.put(h.topCard(),i++);
			}
			else{		
				topCards.put(h.topCard(), 1);
			}
		}
		if(topCards.size() <= 2){
			for(CardHolder h: holders){
				player.add(h.takeTopCard());
			}
		}
	}

I know the last if statement isn't finished yet, need to check the hashmap for keys with values more than 1, and just remove those from the cardHolders.

You can insert the if statement into loop, terminating the loop when the statement is true. Other than that it seems that the usage of the table is correct. I am not certain what you are trying to do inside the if, but I guess this is part of what your program needs to do.

You know - the best way to be certain that you are correct is to build a small test class for it.

apines 116 Practically a Master Poster Featured Poster

You can have either '-', '+' or whitespace, and you can compare all three cases. When you read the line check that the entire length is more than 1 - if not then you don't even have a number afterwards, and you should (probably) ignore the line.

apines 116 Practically a Master Poster Featured Poster

ok Thanks for your help, so how do i update the integer number in the hashtable?

The Put method with the same key suppose to do this for you, and return the previous value. So get the current value, increment it, and put it back to the table.

Also should i use containsKey instead of contains? as I'm checking the key?

Seems like the correct choice.

apines 116 Practically a Master Poster Featured Poster

I'm not sure that I understand your problem. If you represent the grades as String then if the length is more than 1, you have a sign afterwards, otherwise you don't. You can represent the grades using enum as well.

apines 116 Practically a Master Poster Featured Poster

I'm an OO purist. I'd put the Card every time. I'd also write an equals(Card otherCard) method in the Card class and not rely on the other code knowing about how the Strings work.

apines has a good point - you can incorporate that by aborting if the hashtable size goes >2.

Was in the middle of writing the same thing :) Override the equals method is the best practice here in my opinion.

apines 116 Practically a Master Poster Featured Poster

Ok thanks for your help. Just to let you know, theres not just two types of cards, there are 52 cards in a deck, so 52 different types. But N number of cards will be compared. At one time, there could be N different cards on top.

It's quite complicated and has got me pulling my hair out :P

I know that there are 52 different types, but if you have more than two types there, than it's false. You only need to check whether the first type appears N-1 times, or the second one. If a third type appears - you automatically return false.

apines 116 Practically a Master Poster Featured Poster

In a bit of a hurry but I hope this will give you some idea what to do. It includes some cases that you have missed, and let you know which is the type to remove.

private boolean compareCards()
{
	Iterator it = topCards.iterator();
	String type1 = null, type2 = null, current;
	int numCards, counter1, counter2;
	if(it.hasNext())
	{
		type1 = it.next();
		++counter1;
		++numCards;
	}
	while(it.hasNext() && !(counter1 > 1 && counter2 > 1))
	{
		current = it.next().toString();
		if(current.equals(type1))
		{
			++counter1;
		}
		else
		{
			if(type2 == null)
			{
				type2 = current;
				++counter2;
			}
		}
		++numCards;
	}
	if(counter1 > 1 && counter2 > 1)
	{
		return false;
	}
	else if(counter1 <= 1)
	{
		//you know that the type is type2, you need to remove type2 from the array and the stack.
		return true;
	}
	else
	{
		//you know that the type is type1, you need to remove type1 from the array and the stack.
		return true;
	}
}

I will come back later today and see if I have some better idea. Hope that will help you for now.

apines 116 Practically a Master Poster Featured Poster

Basically you don't need to compare all of the elements in the array - if you start with card type A, you iterate over the array cells and you are allowed to only once encounter a different card. If you encounter a different card more than once then there is no way that N-1 cards will be equal, and you return false. Just keep two counters for two different card types.

apines 116 Practically a Master Poster Featured Poster

Let me get this straight - you have an array list consisting of N cards and you want to see that each one of the cards is different?

apines 116 Practically a Master Poster Featured Poster

How about the following psuedo-code, I think it should do the trick:

  1. B[][] = new Array[n][n]
  2. C[] = new Array[n]
  3. for i: 1 to n do
  4. ----C = 0
  5. for j: 1 to n do
  6. ----d=(A[j]/n)+1
  7. ----B[d][C[d]] = A[j]
  8. ----C[d]++
  9. for i: 1 to n do
  10. // Perform a version of counting sort on each sub array (bucket). the total of the actions performed here is n no matter how the buckets are filled. The counting sort should be performed on each array but from 1 to c.
  11. counter = 1
  12. for i: 1 to n
  13. ----for j: 1 to c
  14. --------A[counter] = B[j]
  15. --------counter++

I couldn't indent for some reason so I used ---- instead, I hope the code is readable.
As you see, this is a modified bucket sort using counting sort on each bucket. Hope that was helpful.