Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #21 (permalink)  
Old 01-22-2008, 03:16 PM
Member
 
Join Date: Jan 2008
Posts: 39
mcal is on a distinguished road
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:

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 07:12 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #22 (permalink)  
Old 01-23-2008, 12:37 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Bug found
Sorry for that.

This time I use an IDE and I found a silly bug.
Change this part:
Code:
Random generator = new Random(); int pos = generator.nextInt(A.length);
to this
Code:
Random generator = new Random(); int pos = generator.nextInt(countA);
I tested the method and it should work now.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #23 (permalink)  
Old 01-23-2008, 12:52 AM
Member
 
Join Date: Jan 2008
Posts: 39
mcal is on a distinguished road
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. :-)
Bookmark Post in Technorati
Reply With Quote
  #24 (permalink)  
Old 01-23-2008, 01:03 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #25 (permalink)  
Old 01-23-2008, 01:17 AM
Member
 
Join Date: Jan 2008
Posts: 39
mcal is on a distinguished road
Thanks, that would be a great idea. :-) But now i have to go, because its very late (11:16pm). Cu and thanks for everything. :-)
Bookmark Post in Technorati
Reply With Quote
  #26 (permalink)  
Old 01-23-2008, 10:52 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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:
Code:
Vector<Object> data = new Vector<Object>();
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:
Code:
data.add("There is no spoon"); data.add(4400);
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:
Code:
for (int i = 0; i < data.size(); i++){ System.out.println((data.get(i)).toString()); }
Are you following mcal?
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #27 (permalink)  
Old 01-23-2008, 09:56 PM
Member
 
Join Date: Jan 2008
Posts: 39
mcal is on a distinguished road
Yeah i understood that. Thanks a lot, Tim.
Bookmark Post in Technorati
Reply With Quote
  #28 (permalink)  
Old 01-23-2008, 11:42 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Adding polymorphism
Hello mcal

Lets add some classes to the show. Lets have the abstract class Animal and a few extensions of it:
Code:
public abstract class Animal(){ protected String sound; public Animal(){ sound = ""; } public void printSound(){ System.out.println(sound); } public abstract void createSound(); }
And the extensions:
Code:
public class Dog extends Animal{ public Dog(){ super(); } public void createSound(){ sound = "bark"; } }
Code:
public class Snake extends Animal{ public Snake(){ super(); } public void createSound(){ sound = "hisss"; } }
Let's create a vector of Animal instances.
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);
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.
Code:
for (int i = 0; i < animals.size(); i++){ Animal animal = animals.get(i); animal.printSound(); }
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.

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.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #29 (permalink)  
Old 01-24-2008, 10:58 AM
Member
 
Join Date: Jan 2008
Posts: 39
mcal is on a distinguished road
Yeah, thanks Tim, this is really interesting. Ok thanks a lot. You'r Awesome!
Bookmark Post in Technorati
Reply With Quote
  #30 (permalink)  
Old 01-24-2008, 11:37 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Casting and instanceof
Hello mcal

I'm glad you feel that way. Lets continue with the instanceof keyword and casting.
Quote:
Originally Posted by tim
This is all very handy, but what do you do if you want to know what classes you are handling?
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:
Code:
<object> instanceof <class>
,will give a boolean value. For example:
Code:
boolean result1 = dog instanceof Dog; boolean result2 = dog instanceof Animal;
both result1 and result2 will now be true. Now, lets modify our for loop to display which classes we are working with:
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(); }
I'm going to modify the Dog class a bit:
Code:
public class Dog extends Animal{ public Dog(){ super(); } public void createSound(){ sound = "bark"; } public void howl(){ sound = "hawooooo"; } }
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:
Code:
<subclass here> child = (<subclass here>)parent;
for example, if I know the animal object is a dog then:
Code:
Dog dog = (Dog) animal;
Back to the for loop. Let's make the dog howl, and note that snakes cannot howl:
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(); }
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.

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.

Are you interested mcal? Please ask if you get stuck or if there is a bug somewhere.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #31 (permalink)  
Old 01-24-2008, 08:25 PM
Member
 
Join Date: Jan 2008
Posts: 39
mcal is on a distinguished road
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 09:49 PM.
Bookmark Post in Technorati
Reply With Quote
  #32 (permalink)  
Old 01-25-2008, 05:33 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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.

Thank you for your interest mcal.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #33 (permalink)  
Old 01-25-2008, 05:51 PM
Member
 
Join Date: Jan 2008
Posts: 39
mcal is on a distinguished road
Ok Tim, thanks for everything. I really appreciated your help. And i wish you Good Luck for your studies. Thanks a lot again. :-)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
do...while loop eva New To Java 16 01-31-2008 08:44 AM
A loop that doesn't loop MichYer New To Java 2 07-30-2007 10:44 AM
While loop leebee New To Java 1 07-18-2007 05:11 PM
Loop Help HeavyD New To Java 5 07-10-2007 03:26 PM
eternal loop problem sandor New To Java 3 04-29-2007 05:55 PM


All times are GMT +3. The time now is 01:16 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org