Results 21 to 33 of 33
Thread: For loop problem
- 01-22-2008, 01:16 PM #21
Member
- Join Date
- Jan 2008
- Posts
- 39
- Rep Power
- 0
Thanks a lot. Its really kind of you to help me with this problem. I have understood what you told me, but unfortunately, its still repeating questions( to be precise it is repeating only the last question for 3 to 4 times). This is the code, where i put the source code you told me. I think that there is something wrong with the way "pos" is calculated. But i can't figure it out. Have i have done something wrong? Thanks a lot again and i'm sorry for bothering you. Thanks again. :-)
The code:
Java Code:public void askQuestions(String[] questions, String[] answers) { int count = 0; int point = 0; int[] A = new int[questions.length]; int[] B = new int[A.length]; int countA = A.length; int countB = 0; for (int i = 0; i < A.length; i++){ A[i] = i; } while (countA > 0){ Random generator = new Random(); int pos = generator.nextInt(A.length); B[countB] = A[pos]; countB++; for (int i = pos; i < A.length - 1; i++){ A[i] = A[i + 1]; } countA--; } for(int j = 0; j < questions.length; j++) { timeForMore = true; int randomIndex = B[j]; String input = JOptionPane.showInputDialog(null, questions[randomIndex]); if(answers[randomIndex].equalsIgnoreCase(input)) count++; // incrementing counter if entered answer is correct point++; if(!timeForMore) // if time is over, the program executes the loop an stops asking questions. break; } JOptionPane.showMessageDialog(null, "You answered " + count + " out of " + questions.length + " questions correctly."); }Last edited by mcal; 01-22-2008 at 05:12 PM.
- 01-22-2008, 10:37 PM #22
Bug found
Sorry for that.
This time I use an IDE and I found a silly bug. :rolleyes:
Change this part:
to thisJava Code:Random generator = new Random(); int pos = generator.nextInt(A.length);
I tested the method and it should work now. ;)Java Code:Random generator = new Random(); int pos = generator.nextInt(countA);
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 01-22-2008, 10:52 PM #23
Member
- Join Date
- Jan 2008
- Posts
- 39
- Rep Power
- 0
Yeah it worked 100% now. Thanks a lot. That was really kind of you. And no problem, i knew it was a small problem somewhere in the code, but without your help i would never had done it. Thanks again. :-)
- 01-22-2008, 11:03 PM #24
Good
I'm glad it works now. If you want, I can show you how to use classes and vectors. Maybe some streaming perhaps? The stuff that one can do with Java is amazing.
Good luck with your studies. ;)Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 01-22-2008, 11:17 PM #25
Member
- Join Date
- Jan 2008
- Posts
- 39
- Rep Power
- 0
Thanks, that would be a great idea. :-) But now i have to go, because its very late (11:16pm). Cu and thanks for everything. :-)
- 01-23-2008, 08:52 AM #26
Vectors
Hello mcal.
Since you seem interested I will start with vectors. In the java.util package are many tools and data structures that one can use for a variety of problems. For most problems, I prefer to use vectors. To use vectors, make sure that you import the java.util.Vector class.
Okay, a vector is a dynamic data structure unlike arrays. That means that vectors will grow automatically as you need more space. When using vectors, it is good practice to use generics. To define a vector object that can contain a variety of different objects we divine, lets say data, like this:
This will create a empty vector of Object types. At the moment, data has no elements. To add an element, we use the Vector.add() method like this:Java Code:Vector[COLOR="RoyalBlue"]<Object>[/COLOR] data = new Vector[COLOR="RoyalBlue"]<Object>[/COLOR]();
Note that 4400 is a literal and a primitive type. Java will box this automatically, that is turn the int into an Integer. To get the size of a vector, we use the Vector.size() method. To access an element of the vector, we use the Vector.get() method. In the code below we will print all the objects:Java Code:data.add("There is no spoon"); data.add(4400);
Are you following mcal? ;)Java Code:for (int i = 0; i < data.size(); i++){ System.out.println((data.get(i)).toString()); }Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 01-23-2008, 07:56 PM #27
Member
- Join Date
- Jan 2008
- Posts
- 39
- Rep Power
- 0
Yeah i understood that. Thanks a lot, Tim. :) ;)
- 01-23-2008, 09:42 PM #28
Adding polymorphism
Hello mcal :p
Lets add some classes to the show. Lets have the abstract class Animal and a few extensions of it:
And the extensions:Java Code:public abstract class Animal(){ protected String sound; public Animal(){ sound = ""; } public void printSound(){ System.out.println(sound); } public abstract void createSound(); }
Java Code:public class Dog extends Animal{ public Dog(){ super(); } public void createSound(){ sound = "bark"; } }Let's create a vector of Animal instances.Java Code:public class Snake extends Animal{ public Snake(){ super(); } public void createSound(){ sound = "hisss"; } }
What all this code does is create a structure where we can define classes that inherit attributes and behavior from their parent. So, I've started with an abstract class Animal. An abstract class cannot be instanced. This makes sense, since we cannot create an Animal, unless we know what type of animal it is. The subclasses of Animal, Dog and Snake, are not abstract. The reason for this structure is that when you collect information, you want some way of handling all your data types will the minimum amount of work and code. Lets say that I want to print all the sounds that the animals make in my vector.Java Code:Vector<Animal> animals = new Vector<Animal>(); Dog dog = new Dog(); Snake snake = new Snake(); dog.createSound(); snake.createSound(); animals.add(dog); animals.add(snake);
Now, if I wanted to add a Cat class, I just have to extend the Animal class and make sure I implement the createSound() abstract method. I don't have to touch the for loop above. This makes it easy to extend your Java programs. :DJava Code:for (int i = 0; i < animals.size(); i++){ Animal animal = animals.get(i); animal.printSound(); }
This is all very handy, but what do you do if you want to know what classes you are handling? So, mcal, are you still interested? Just ask if something is unclear. ;)Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 01-24-2008, 08:58 AM #29
Member
- Join Date
- Jan 2008
- Posts
- 39
- Rep Power
- 0
Yeah, thanks Tim, this is really interesting. Ok thanks a lot. You'r Awesome! ;)
- 01-24-2008, 09:37 AM #30
Casting and instanceof
Hello mcal
I'm glad you feel that way. Lets continue with the instanceof keyword and casting.
The answer to my question is the instanceof keyword. The instanceof keyword is used to test if an object is an instance of a class or subclasses of it. The condition:
Originally Posted by tim
,will give a boolean value. For example:Java Code:<object> [COLOR="RoyalBlue"]instanceof [/COLOR]<class>
both result1 and result2 will now be true. Now, lets modify our for loop to display which classes we are working with:Java Code:boolean result1 = dog instanceof Dog; boolean result2 = dog instanceof Animal;
I'm going to modify the Dog class a bit:Java Code:for (int i = 0; i < animals.size(); i++){ Animal animal = animals.get(i); if (animal instanceof Dog) System.println("A dog goes:"); if (animal instanceof Snake) System.println("A snake goes:"); animal.printSound(); }
Now, Dog has a howl method that is not inherited from Animal. So how will you access it in the for loop? The answer is to use casting. Casting is when you force one type into another. You can only cast a super class into one of its subclasses. To cast we use the synax:Java Code:public class Dog extends Animal{ public Dog(){ super(); } public void createSound(){ sound = "bark"; } public void howl(){ sound = "hawooooo"; } }
for example, if I know the animal object is a dog then:Java Code:<subclass here> child = (<subclass here>)parent;
Back to the for loop. Let's make the dog howl, and note that snakes cannot howl:Java Code:Dog dog =[COLOR="Magenta"] (Dog)[/COLOR] animal;
We can now handle objects in a generic way. On your quiz problem you needed to save data and read it again. That is a real word problem that programmers face all the time.Java Code:for (int i = 0; i < animals.size(); i++){ Animal animal = animals.get(i); if (animal instanceof Dog){ Dog castedDog = (Dog)animal; // dog can also be used, but lets ignore that for now :) castedDog.howl(); System.println("A dog goes:"); } if (animal instanceof Snake) System.println("A snake goes:"); animal.printSound(); }
Object serialization allows you to take an object just as it is and save or load it at will. Object serialization will make it possible to save an entire data structure will multiple fields and sub data structures to be saved and loaded in just a few lines of code. :eek:
Are you interested mcal? Please ask if you get stuck or if there is a bug somewhere. :DEyes dwelling into the past are blind to what lies in the future. Step carefully.
- 01-24-2008, 06:25 PM #31
Member
- Join Date
- Jan 2008
- Posts
- 39
- Rep Power
- 0
Yeah we have done serialization at school. We have used it in object files. We have done random files too (but i didn't understand random files much). And we have done type casting too. Thanks Tim, i really am appreciating this. Thanks again. :-)
Last edited by mcal; 01-24-2008 at 07:49 PM.
- 01-25-2008, 03:33 PM #32
Excellent
Hello mcal.
I was going to post on serialization and interfaces, but it looks like you already know that. I'm glad, but unfortunately, this is where my knowledge in polymorphism and data structures end. I have recently studied XML, but I still have no JDBC, HTML, SQL, ect experience yet. I'm starting second year varsity now. :D
Thank you for your interest mcal. ;)Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 01-25-2008, 03:51 PM #33
Member
- Join Date
- Jan 2008
- Posts
- 39
- Rep Power
- 0
Similar Threads
-
Loop Help
By HeavyD in forum New To JavaReplies: 7Last Post: 09-22-2010, 09:55 PM -
do...while loop
By eva in forum New To JavaReplies: 16Last Post: 01-31-2008, 06:44 AM -
A loop that doesn't loop
By MichYer in forum New To JavaReplies: 2Last Post: 07-30-2007, 08:44 AM -
While loop
By leebee in forum New To JavaReplies: 1Last Post: 07-18-2007, 03:11 PM -
eternal loop problem
By sandor in forum New To JavaReplies: 3Last Post: 04-29-2007, 03:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks