[Source]Simple vocab quizzer

Discussion in 'Programming General' started by SuF, Sep 22, 2009.

[Source]Simple vocab quizzer
  1. Unread #1 - Sep 22, 2009 at 1:35 PM
  2. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    [Source]Simple vocab quizzer

    I made this because I thought it would be cool and it was a nice project to work on shit.


    Sample execution:

    Code:
    public class Word
    {
    	private String english;
    	private String spanish;
    	
    	public Word(String english, String spanish)
    	{
    		this.english = english;
    		this.spanish = spanish;
    	}
    	
    	public String getEnglish()
    	{
    		return english;
    	}
    	
    	public String getSpanish()
    	{
    		return spanish;
    	}
    	
    }
    Code:
    import java.util.Collections;
    import java.util.LinkedList;
    
    public class Question
    {
    	private LinkedList<Word> words;
    	private int lang;
    	private Word ans;
    	
    	public Question(LinkedList<Word> words, Word ans, int lang)
    	{
    		this.words = words;
    		this.lang = lang;
    		this.ans = ans;
    		this.words.add(ans);
    		Collections.shuffle(this.words);
    	}
    	
    	public void print()
    	{
    		System.out.println(this.toString());
    	}
    	
    	public Word getAns()
    	{
    		return ans;
    	}
    	
    	public boolean checkAns(int a)
    	{
    			return words.get(a - 1).equals(ans);
    	}
    	
    	public String toString()
    	{
    		String a = "";
    		if(lang == 1)
    		{
    			a = ans.getSpanish() + ":";
    			for(int b = 0; b < 4; b++)
    			{
    				a += "\n" + (b + 1) + ") " + words.get(b).getEnglish();
    			}
    		}
    		else
    		{
    			a = ans.getEnglish() + ":";
    			for(int b = 0; b < 4; b++)
    			{
    				a += "\n" + (b + 1) + ") " + words.get(b).getSpanish();
    			}
    		}
    		return a;
    	}
    }
    
    Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.InputMismatchException;
    import java.util.LinkedList;
    import java.util.Scanner;
    
    public class Main
    {
    	private LinkedList<Word> wordList;
    	private LinkedList<Word> unUsedList;
    	private int ans;
    	private int lang;
    
    	public static void main(String[] args)
    	{
    		new Main();
    	}
    
    	public Main()
    	{
    		wordList = loadWords("spanish.txt");
    		unUsedList = new LinkedList<Word>();
    		unUsedList.addAll(wordList);
    		lang = getInputInt("Enter 1 for Spanish to English; 2 for English to Spanish:\n");
    		loop();
    	}
    
    	private void loop()
    	{
    		while (true)
    		{
    			while (unUsedList.size() > 0)
    			{
    				Question a = createQuestion();
    				a.print();
    				ans = getInputInt("");
    				if (a.checkAns(ans))
    				{
    					System.out.println("You are right!\n");
    				}
    				else
    				{
    					if (lang == 1)
    					{
    						System.out.println("You are wrong!\nThe correct answer is: " + a.getAns().getEnglish());
    					}
    					else
    					{
    						System.out.println("You are wrong!\nThe correct answer is: " + a.getAns().getSpanish());
    					}
    				}
    			}
    			System.out.println("You have completed all the words.");
    			if (getInputString("Would you like to study again? (Y or N)").equalsIgnoreCase("N"))
    			{
    				break;
    			}
    			unUsedList.addAll(wordList);
    		}
    		System.out.println("Thank you for using the program.");
    	}
    
    	private Question createQuestion()
    	{
    		LinkedList<Word> ansList = new LinkedList<Word>();
    		ansList.addAll(wordList);
    		LinkedList<Word> others = new LinkedList<Word>();
    		Word answer = unUsedList.get(random(0, unUsedList.size() - 1));
    		unUsedList.remove(answer);
    		ansList.remove(answer);
    		for (int a = 1; a <= 3; a++)
    		{
    			Word b = ansList.get(random(0, ansList.size() - 1));
    			others.add(b);
    			ansList.remove(b);
    		}
    		return new Question(others, answer, lang);
    	}
    
    	private Word randomWord()
    	{
    		return wordList.get(random(0, wordList.size() - 1));
    	}
    
    	private LinkedList<Word> loadWords(String file)
    	{
    		try
    		{
    			LinkedList<Word> c = new LinkedList<Word>();
    			BufferedReader input = new BufferedReader(new FileReader(file));
    			String a;
    			while ((a = input.readLine()) != null)
    			{
    				String[] b = a.split(",");
    				c.add(new Word(b[0], b[1]));
    			}
    			return c;
    		}
    		catch (IOException e)
    		{
    			return null;
    		}
    	}
    
    	private static int random(int min, int max)
    	{
    		return (int) (Math.random() * (max - min + 1)) + min;
    	}
    
    	private String getInputString(String msg)
    	{
    		Scanner a = new Scanner(System.in);
    
    		while (true)
    		{
    			try
    			{
    				System.out.print(msg);
    				return a.nextLine();
    			}
    			catch (InputMismatchException e)
    			{
    				a.next();
    			}
    		}
    
    	}
    
    	private int getInputInt(String msg)
    	{
    		Scanner a = new Scanner(System.in);
    
    		while (true)
    		{
    			try
    			{
    				System.out.print(msg);
    				return a.nextInt();
    			}
    			catch (InputMismatchException e)
    			{
    				a.next();
    			}
    		}
    
    	}
    }
    
    And here is the file of words that it loads....

    Code:
    to remember,acordarse (o:ue)
    to go to bed,acostarse (o:ue)
    to shave,afeitarse
    to bathe; to take a bath,banarse
    to brush,cepillarse
    to say goodbye,despedirse (e:i)
    to wake up,despertarse (e:ie)
    to go to sleep; to fall asleep,dormirse (o:ue)
    to shower; to take a shower,ducharse
    to get angry,enojarse
    to go away; to leave,irse
    to wash,lavarse
    to get up,leventarse
    to be called; to be named,llamarse
    to put on makeup,maquillarse
    to comb one's hair,peinarse
    to put on,ponerse
    to become,ponerse (+adj.)
    to worry,preocuparse
    to try on,probarse (o:ue)
    to stay; to remain,quedarse
    to take off,quitarse
    to dry,secarse
    to sit down,sentirse
    to get dressed,vestirse
     
  3. Unread #2 - Sep 22, 2009 at 5:05 PM
  4. super_
    Joined:
    Dec 20, 2008
    Posts:
    91
    Referrals:
    0
    Sythe Gold:
    0

    super_ Member

    [Source]Simple vocab quizzer

    unUsedList... why not just remove()
     
  5. Unread #3 - Sep 22, 2009 at 8:36 PM
  6. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    [Source]Simple vocab quizzer

    Its randomly selected. Not from the front... I could just shuffle the list... But. Meh. Either way works... >_>.
     
< Minimizing | Help >

Users viewing this thread
1 guest


 
 
Adblock breaks this site