apines 116 Practically a Master Poster Featured Poster

You need to fix the build path the include the other project as well, it's not enough that they are in the same workspace. If you are using eclipse, you need to go to the build path (right click on the project, build path -> configure build path) and add the project by pressing "Add" in the Projects tab.

apines 116 Practically a Master Poster Featured Poster

You have 4 threads in your program - the three that you have created and the main thread. The main thread (the main method) is finished after creating the three other threads. There is no guarantee regarding the order in which the threads will run and for how long, which explains why you are getting different output.

apines 116 Practically a Master Poster Featured Poster

You see, the forum has such a good effect on you, that you just needed to post the code in order to see how to solve it :)

apines 116 Practically a Master Poster Featured Poster

Accessing the first element of row i: arr[i][0]; Accessing the last element of row i: arr[i][arr[i].length - 1]; From here you just need to do the adding :) Give it a go, try to implement it.

apines 116 Practically a Master Poster Featured Poster

thanks apines. yeah that makes sense. learnt something new today!

Happy to help ;)

apines 116 Practically a Master Poster Featured Poster

Integer.parseInt will throw an exception that you will have to catch - not a good practice.

jon.kiparsky commented: Yep. +1
apines 116 Practically a Master Poster Featured Poster

You were talking about objects - in Java objects are always on the heap, never local on the stack, so the scenario you have mentioned cannot happen.

apines 116 Practically a Master Poster Featured Poster

Objects are always by reference in Java, which means that a=b will point a to the place in the heap the b resides - any future changes to b will affect a as well.

apines 116 Practically a Master Poster Featured Poster

FileInputStream fstream[] is not initialized and therefore is null. Before you can write the code in lines 14 and beyond, you have to initialize the array itself:

fstream = new FileInputStream[array size]

If the array size is dynamic, you might want to consider a list instead.

apines 116 Practically a Master Poster Featured Poster

They ask you to build a class that will test the class you have previously created.

apines 116 Practically a Master Poster Featured Poster

You can always implement copy constructors in your class:

public Class yourClass
{
   public yourClass(yourClass yourClassObject)
   {
      //implement
   }
}

Other than that, you need to read the API about mutable and immutable objects to help you decide how to implement their copy in the constructor.

apines 116 Practically a Master Poster Featured Poster
a=a+b;
b=a-b;
a=a-b;

Will indeed swap to variables without a temp. Notice that you have to make sure that a+b does not exceed the maximum allowed integer. There is another way of swapping the variables without these constraints:

void swap (int *x, int *y) 
{
   if (x != y) 
   {
      *x ^= *y;
      *y ^= *x;
      *x ^= *y;
   }
}
apines 116 Practically a Master Poster Featured Poster

First, excellent post - every experienced programmer is doing all of above and more after learning it the hard way - you cannot force your brain to work and solve all your problems, sometimes you need to rest, sometimes you need a different approach, and almost all of the time you can't assume that you know what your program is doing - you need to verify it.
Regarding the prints - I usually override the toString() method in order to print all the material that I want to know about the class and print that information completely in other parts of the code where I use instances of that class, I find it very helpful.
In addition to the prints, it is important to learn how to use the debugger, since it will give you more information in a more convenient fashion then just printing it.
JamesCherrill - I agree with you regarding testing each class, and I have a small main method in every class to check that the class itself doing all the it suppose to do (and does not do what it does not suppose to do).

jon.kiparsky commented: I like the override +1
apines 116 Practically a Master Poster Featured Poster

Try to do one swap as showed in the diagram and as kramerd elaborated, and than think about special cases that need your attention, such as what if the length of the linked list is 1, or 2?

minimi commented: Helpful tip! +1
apines 116 Practically a Master Poster Featured Poster

kremerd is right - drawing is very helpful. I will try to help you out with the pictures. We will do together one scenario, you will try and translate into code. The rest of the scenarios will be easier then I believe. Let's have this linked list:

1.png

We want to swap node1 and node2, meaning that the list should go head [TEX]\rightarrow[/TEX] node2[TEX]\rightarrow[/TEX] node1[TEX]\rightarrow[/TEX] the rest of the list as usual. First thing let's change node2.next() so it will point to next1, the next in line in the new order: 2.png

Now, node1.next() should point to where node pointed before, because now this is his new place in the list: 3.png

And eventually we need head, who pointed at node1, to point now at node2: 4.png

Now, note that you need not to "lose" your nodes, for example after changing node2.next() to point to node1, we make the older node2.next() unreachable: 5.png

We can make a temp node that will point to node2.next() before changing it to node1: 6.png

You need to be sure that during the entire swap you don't lost any nodes.

This was one swap scenario - can you think about more scenarios that you need to check and handle as part of the swap?

apines 116 Practically a Master Poster Featured Poster

You are not using the printf properly - you need to tell the method where do you want the String to be printed. Try this:

public Square(String name) 
{
   super(name);
   System.out.printf("Shape: %s", this.getName());
}
apines 116 Practically a Master Poster Featured Poster

The syntax for creating a new instance is

Class1 varName = new Class1();

This will call the default constructor (you haven't defined any other). Now, to access the only thing you have in the class, which is the getAllSubjects() method, you use the dot notation syntax

varName.getAllSubjects()
apines 116 Practically a Master Poster Featured Poster

I see that there are many questions here regarding complexity analysis, specifically regarding the Big-O notation of algorithms. I will attempt to shed some light on the subject. I will start with an introduction, after which I will go over some common complexities and last we will solve a few simple examples for practice.

1. Introduction
When looking at the Algorithm, we want to know its order of growth regarding the input size given to it. Consider two simple algorithms that sort an array of [TEX]n[/TEX] cells. The first algorithm is doing [TEX]n[/TEX] operations in order to complete the sort, and the second algorithm is doing [TEX]n^2[/TEX] operations in order to complete it. If the array is of size [TEX]n=2[/TEX], then algorithm one would have done 2 operations while algorithm two would have done 4. When [TEX]n=10[/TEX] then algorithm one would have done 10 operations, and algorithm two 100 – see the difference? When talking about large values of [TEX]n[/TEX] (as in [TEX]{n\to\infty}[/TEX]) those differences will be much more significant – and we are talking just about the difference between [TEX]n[/TEX] and [TEX]n^2[/TEX], not mentioning [TEX]n^3[/TEX], [TEX]n^4[/TEX], and non-polynomial ones like [TEX]2^n[/TEX]!
This is what the Big-O notation is for – expressing the worst-case growth order of an algorithms given input data of size [TEX]n[/TEX]. Knowing if an algorithm can act as [TEX]n^2[/TEX] or at worst as [TEX]n[/TEX] can make big difference when choosing your algorithm or simply evaluating how good it is time-wise or space-wise.

This is …

Direwolf007 commented: Great post! +4
Nick Evan commented: Nice one +16
~s.o.s~ commented: Forgot to upvote your effort, d'oh! :-) +18
Ancient Dragon commented: Very good :) +34
\007 commented: well wrote +0
Anuradha Mandal commented: Thank a lot. +0
TrustyTony commented: Great stuff! +13
NathanOliver commented: Great Job +10
mynameisfish.22 commented: Great post helped a lot thanks +0
apines 116 Practically a Master Poster Featured Poster

As far as I know, you can't pass statically created multidimensional array as an argument to a function. The way you have done it seems like a good way (if not the only one :)). Just be careful and don't do that on a dynamically allocated array, since you cannot assume that all the cells are in the same memory chunk. Then again, if the array is dynamically allocated, you have better ways to do it.

Direwolf007 commented: Good answer! +4
apines 116 Practically a Master Poster Featured Poster

Please can anyone solve this big O complexity problem by using summation method?

Not familiar with summation method in regard to complexity analysis, at least not by that name.

apines 116 Practically a Master Poster Featured Poster

In Java a booolean is a boolean, and an int is an int. 0 is not false, and 1 is not true. You can change your code to something like:

if (((num1 % 2 == 1) || (num2 % 2==1)) && (num1 % 2 != num2 % 2))
{
   System.out.println ("\n YOU LOSE! ='( " + num1 + " " + num2);
}

And this should work :)

One more comment - you are using Math.random() in order to create integers. It will be much easier to use the java.util.Random class, and using its nextInt(int n) method.

optikali commented: Correct, concise, and illuminating! +2
apines 116 Practically a Master Poster Featured Poster

well, everyone in my class is using netbeans to create GUIs.. for some reason, i hand coded mine.. you get a better understanding of GUIs..

But after you got all the understanding you need you just want to do them fast and not spend time coding them :)

apines 116 Practically a Master Poster Featured Poster

Old C programmers never die. They are just cast into void*

apines 116 Practically a Master Poster Featured Poster

The split method returns an array. If you are not allowed to use them - use your previous methods, just make sure that they work. Your main problem was what javaAddict and I have stated - your comparison was wrong.

minimi commented: Thank you very much! Great Help! +1
apines 116 Practically a Master Poster Featured Poster

If you are not forced to use charAt() (assignment restrictions, etc') I recommend a different approach - using String's split(String regex) method with the regex "/", you can divide your String into a String array with the day, month and year in its cells. This will be much more convinient code-wise and will be more flexible in terms if how user formats the date (i.e. 2/5/10, 2/5/2010, 2/05/2010 will all work).

intes77 commented: thaaanks +1
apines 116 Practically a Master Poster Featured Poster

It's called Generics, and I think that this will give you some sense into it :)

apines 116 Practically a Master Poster Featured Poster

You really need to specify where the curly brackets begin and end. Assuming that the code is the more complicated version:

for i = 1 to n 
{
   j = n * 2 
   while j >= 1 
   {
      j = j – 1
   }
}

Then the outer loop iterates from 1 to n, meaning n iterations. The inner loop, at the first outer loop iteration when i=1, will have j=2, and it will go for two iterations until j will no longer be higher or equal to 1. The second outer loop iteration i=2, meaning j=4, and we will have 4 iterations of the inner loop. We can see that for every i=k, we will have j=2k and will do 2k iterations. This will go on until the last iteration where i=n, j=2n and we will have 2n iterations. Overall we will have n(2+4+6+...+2n) = n*(2n+2)n/2 (using the sum of an arithmetic progression again).

apines 116 Practically a Master Poster Featured Poster

Where is the class Reservation? Post it please.

apines 116 Practically a Master Poster Featured Poster

Assuming that the code is

for i = 1 to 2 * n 
{
   j = i + 1
   while j >= 1
   {
      j = j – 1   
   }
}

Then let's analyze the complexity: The outer loop is simple, it goes from 1 to 2n, meaning 2n iterations. The inner loop is dynamic. The first iteration of the outer loop, when i = 1, then j=1+1=2 and it goes until j is no longer bigger or equals to 1, with decremation of 1 in each of the inner loop's iteration - meaning it will go twice: first iteration j=2, second iteraion j=1, and then j=0 and the inner loop exists. The second iteration of the outer loop i=2, meaning j=3, meaning that following the same logic as before we will have 3 iterations. The next iteration of the outer loop i=3, j=4 and we will have 4 iterations. This will go on until i=2n, j=2n+1, and then we will have 2n+1 iterations. So the inner loop will go 1+2+3+...+2n+(2n+1) iterations. Using the sum of arithmetic progression we can tell that this equals to ((2n+2)(2n+1))/2. So the inner loop goes 2n iterations, the outer loop goes ((2n+2)(2n+1))/2 iterations, making the total number of iterations to be 2n*((2n+2)(2n+1))/2=n*((2n+2)(2n+1)).

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

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

apines 116 Practically a Master Poster Featured Poster

Let's analyze the code step by step. At the beginning you have 1 loop:

for (i=n; i>0;i=i-1)
{
   y=y+1;
}

The loop is going from 1 to n, meaning O(n) complexity. Next you have a nested loop

for (i=1;i<=n;i=i*3)
{ 
   for (j=1;j<=3n;++j)
   {
      for(k=0;k<n; k=k+5)
      {
         x=x+5; //this is O(1)
      }
   }
}

Three loops nested, the first one goes to n with jumps of i*3, which makes it's complexity O(log3(n))=O(log(n)), the second one goes up to 3n with increment of 1, which makes it O(3n)=O(n), and the third loop goes to n with jumps of 5, making it O((1/5)n)=O(n). So the complexity will be O(n + (n^2)log(n)).

If you don't understand any of the step that I have done, assuming that you don't just want me to do your homework - you are welcome to ask.

fahadyousaf commented: thank you apines for your guide line +0
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

In concurrent programming (using threads) you might have a resource that you cannot have it being accessed simultaneously.

For example, imagine that you have a linked list, and you want to remove a node from it. Imagine that in the middle of the removal, when not all the pointers are reattached, you have a context switch and another thread is accessing the linked list. The other thread does not know that the list is in a mess, and continues as usual. In order to avoid such scenarios, you need to to put the code that has to be completed without interruption inside what is called a critical section. In our example, lets create a global boolean variable that is true when someone is using the linked list. Thread A wants to delete a node, so it declares the variable as true, to indicate all the other threads not to use the linked list. After that, thread A continues with the node removal. If a context switch occurs in the middle, to thread B, thread B will first check the the variable is false - if its true, it will sleep until it becomes true, and then will continue with what it wanted to do to the resource. When thread A is done with the node deletion, it will change the variable back to false, letting other threads use the resource.

This is a very simple scenario. Few keywords that you might want to look for regarding mutual exclusion …

apines 116 Practically a Master Poster Featured Poster

Perhaps a little late, but since it is not marked as solved I will give it a go. In order to swap two variables, denoted 'a' and 'b', using a third variable, denoted 'temp', you need to do the following:
1. place one of the numbers, say the one inside variable 'a', inside 'temp'.
2. then place the value of 'b' inside 'a'.
3. Put previous value of 'a', now placed inside 'temp', into 'a'.

In code it will look like this:

SWAP_NUMBERS(int a, int b) //a and b are given as parameters
{
   int temp;
   temp = a //step 1.
   a = b    //step 2.
   b = temp //step 3.
}

now (even though you didn't ask but you might find it interesting), there is another way to swap to numbers without using the temp variable. With a simple math trick you can do the swap:

SWAP_NUMBERS_2(int a, int b) //a and b are given as parameters
{
   a = a + b; 
   b = a - b; //now b has the value of a.
   a = a - b; //now a has the value of b.
}

Hope that this helps you, and if it does, just mark the thread as solved :)

ddanbe commented: Nice :) +8