The application will ask questions like:

What comes after D?

A child is expected to answer the question and the application will inform the child if the answer is correct or wrong. The application will ask a total of 20 questions. After all 20 questions have been answered, the application will display a score (from 0 to 20) to inform the child how many questions he or she answered correctly. The application will also list all the letters that were asked.

The letters tested are from ‘A’ up to ‘Y’. Do not include the letter ‘Z’. The letters are selected randomly. No letters are to be repeated, that is, each letter is only asked once in the 20 questions.

Note: The java.util.Random class can be used to generate random numbers. For example, the following statements may be used to generate a random number from 0 to 4 inclusive:

Random generator = new Random();
int number = generator.nextInt(5);

#I have been started and my code facing some problem in gui application ,can anybody help me in this coding of GUI.and the code i attached below also have some problem .when i tested it ,the letter is repeated .how to solve it?

Recommended Answers

All 10 Replies

public class Alphabet {
	//We declare the random generator globally
	private Random generator = new Random();
	
	//Here we will remember what the index was of the letter we asked
	private int askedLetterIndex;
	private int ind = 26;
	//Here we declare the alphabet
	private char[] alphabet = {'a','b','c','d','e','f','g','h'
			,'i','j','k','l','m','n','o','p','q'
			,'r','s','t','u','v','w','x','y','z'};
	
	private char [] tempChar;
	//This function will get you a random number from our alphabet
	private char getRandomLetter(){
		askedLetterIndex = generator.nextInt(ind);
		return alphabet[askedLetterIndex];
	}
	
	//This will check the answer of the user, it returns True if the answer was correct and False if it was incorrect
	private Boolean checkAnswer(char answer){
		if (alphabet[askedLetterIndex + 1] == answer){
			return true;
		}else{
			return false;
		}
	}
	
	//This method will do the test 
	//This is a console application, so for a GUI application you'll have to rewrite this method
	public void doTest(){
		//The scanner will be used to read the answer of the user
		Scanner in = new Scanner(System.in);
		
		//Here we will stock the answer of the user
		char answer;
		
		//This will ask the user a letter for 20 times
		for(int i = 0; i < 20; i++){
			System.out.println("What letter comes afther '" + getRandomLetter() + "' ?");
			answer = in.next().charAt(0);
			ind--;
			tempChar = new char[ind];
			System.arraycopy(alphabet, 0, tempChar,0, askedLetterIndex);
			System.arraycopy(alphabet, askedLetterIndex+1, tempChar,askedLetterIndex, ind-askedLetterIndex);
			if(checkAnswer(answer)){
				System.out.println("Correct!");
			}else
				System.out.println("Wrong!");
			alphabet = tempChar;
		}
	}
	
	//Run the program
	public static void main(String[] args) {
		Alphabet a = new Alphabet();
		a.doTest();
	}
}

but this question no need include z ...and when i tested the code ,what letter come after z,it returns error

change this line private int ind = 26;
to private int ind = 24;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Alphabet {
	//We declare the random generator globally
	private Random generator = new Random();
	
	//Here we will remember what the index was of the letter we asked
	private int askedLetterIndex;
	private int ind = 26;
	private int newInt = 24;
	//Here we declare the alphabet
	private char[] alphabet = {'a','b','c','d','e','f','g','h'
			,'i','j','k','l','m','n','o','p','q'
			,'r','s','t','u','v','w','x','y','z'};
	
	private char [] tempChar;
	//This function will get you a random number from our alphabet
	private char getRandomLetter(){
		askedLetterIndex = generator.nextInt(newInt);
		return alphabet[askedLetterIndex];
	}
	
	//This will check the answer of the user, it returns True if the answer was correct and False if it was incorrect
	private Boolean checkAnswer(char answer){
		if (alphabet[askedLetterIndex + 1] == answer){
			return true;
		}else{
			return false;
		}
	}
	
	//This method will do the test 
	//This is a console application, so for a GUI application you'll have to rewrite this method
	public void doTest(){
		//The scanner will be used to read the answer of the user
		Scanner in = new Scanner(System.in);
		
		//Here we will stock the answer of the user
		char answer;
		
		//This will ask the user a letter for 20 times
		for(int i = 0; i < 20; i++){
			System.out.println("What letter comes afther '" + getRandomLetter() + "' ?");
			answer = in.next().charAt(0);
			ind--;
			newInt--;
			tempChar = new char[ind];
			System.arraycopy(alphabet, 0, tempChar,0, askedLetterIndex);
			System.arraycopy(alphabet, askedLetterIndex+1, tempChar,askedLetterIndex, ind-askedLetterIndex);
			if(checkAnswer(answer)){
				System.out.println("Correct!");
			}else
				System.out.println("Wrong!");
			alphabet = tempChar;
		}
	}
	
	//Run the program
	public static void main(String[] args) {
		Alphabet a = new Alphabet();
		a.doTest();
	}
}

now i am testing it ,but when the question ask what letter come after e,when i type f ...it say wrong ..where the error

package Practice;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Alphabet {
	//We declare the random generator globally
	private Random generator = new Random();
	
	//Here we will remember what the index was of the letter we asked
	private int askedLetterIndex;
	private int ind = 26;
	private int newInt = 24;
	//Here we declare the alphabet
	private char[] alphabet = {'a','b','c','d','e','f','g','h'
			,'i','j','k','l','m','n','o','p','q'
			,'r','s','t','u','v','w','x','y','z'};
	private char[] alphabetTest = {'a','b','c','d','e','f','g','h'
			,'i','j','k','l','m','n','o','p','q'
			,'r','s','t','u','v','w','x','y','z'};
	
	private char [] tempChar;
	//This function will get you a random number from our alphabet
	private char getRandomLetter(){
		askedLetterIndex = generator.nextInt(newInt);
		return alphabet[askedLetterIndex];
	}
	
	//This will check the answer of the user, it returns True if the answer was correct and False if it was incorrect
	private Boolean checkAnswer(char answer){
			int chkInd = 0;
			for(int i=0; i<25; i++){
				if(alphabet[askedLetterIndex]==alphabetTest[i]){
					chkInd = i;
				}
			}
		if (alphabetTest[chkInd + 1] == answer){
			return true;
		}else{
			return false;
		}
	}
	
	//This method will do the test 
	//This is a console application, so for a GUI application you'll have to rewrite this method
	public void doTest(){
		//The scanner will be used to read the answer of the user
		Scanner in = new Scanner(System.in);
		
		//Here we will stock the answer of the user
		char answer;
		
		//This will ask the user a letter for 20 times
		for(int i = 0; i < 20; i++){
			System.out.println("What letter comes afther '" + getRandomLetter() + "' ?");
			answer = in.next().charAt(0);
			ind--;
			newInt--;
			tempChar = new char[ind];
			System.arraycopy(alphabet, 0, tempChar,0, askedLetterIndex);
			System.arraycopy(alphabet, askedLetterIndex+1, tempChar,askedLetterIndex, ind-askedLetterIndex);
			if(checkAnswer(answer)){
				System.out.println("Correct!");
			}else
				System.out.println("Wrong!");
			alphabet = tempChar;
		}
	}
	
	//Run the program
	public static void main(String[] args) {
		Alphabet a = new Alphabet();
		a.doTest();
	}
}

may i know what is the gui application code ?i do not know how 2 do it ...

Why, have you given any effort?

In the entire thread, vchandra has been given you the answer even though it is against the forum rules. You have showed no effort from your part. You just took the code, run it, and report back what you didn't like in order to get the latest code. Now that you are "satisfied" with the result you want the gui program written for you as well?

If the code given works for you, then do some studying on your own and start writing the gui application. Post some code in order to get positive feedback.

And to vchandra.
Please next time use code tags when posting code. Press the code button and put your code inside.
Also, as noble as your motives were, you were giving hime everything and when there was an error, YOU were the one again trying to solve it in order to post the correct code. After given him the code instead of trying to understand it and correct it, he was expecting everything from you. It is not your job to do funjoke88's homework.
Now that he wants the GUI version, he is unable to do it on his own because he hadn't learned anything. How was that helpful and how is he going to learn how to complete his next assignments. Do you plan on doing all of his homework?

I am only saying this, because I don't think it is fair on your behalf to do his work for him. If you want to spend your time to do some one else's work then go ahead. But I don't think it is fair for you.

I didnt know the rules and regulations, I have joined this forum before 3 days and only purpose was to help others.
I didnt know that ppl are posting only homeworks here.

I didnt know the rules and regulations, I have joined this forum before 3 days and only purpose was to help others.
I didnt know that ppl are posting only homeworks here.

It is good to help others with their homework. But in this case you ended up doing all the work.
Let's us both wait for funjoke88 to post some of his code, before giving any more assistance. It wouldn't be too difficult with all the help you gave him.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.