Spanish Vocabulary Helper Translator Using Java - Sample Assignment

Updated: Feb 10




For this assignment, we are going to work with adding and removing data from arrays, linear search, and File I/O.

1. In addition, you will learn to work with parallel arrays.

  • This program will assist an English-speaking user to build their vocabulary in Spanish

  • This program will read a file containing a list of words in English and another containing the translation of those words in Spanish.

  • The words and corresponding translations should to be stored in two separate but parallel arrays.

1. In other words, the indices of the English words array will match the indices of the Spanish words array.

2. Thus the word in English at index 0 in the first array, will match the translation at index 0, in the second array, and so on.

  • You may assume that the user will not store more than 1000 words and translations.

3. Therefore, your arrays should be declared of length 1000.

  • Create two new text files inside of your project folder in Eclipse:

  • Name these text files english.txt and spanish.txt

  • Copy and paste the following alphabetized list of English words into the english.txt file:

bear

black

blue

cat

cow

dog

fall

gray

green

purple

red

spring

summer

to be

to be able

to go

to love

winter

white

yellow



  • Copy and paste the following list of Spanish translations of the above words into the spanish.txt file:

oso

negro

azul

gato

vaca

perro

otono

gris

verde

morado

rojo

primavera

verano

ser

poder

ir

amar

invierno

blanco

amarillo


  • Read in the words from each file into two separate arrays, one array called arrayEnglish and one array called arraySpanish.

  • Hint: you can use a variable to count how many values are in each file as you are reading in the data, and use this information to save the number of values currently stored in each array?

  • Next, you will need to write a menu-driven program to allow the user to complete 3 tasks:

Add a new word and its translation (choice 1)

Remove a word and its translation (choice 2)

Display the Spanish translation of an English word (choice 3)

Quit (choice 4)

  • For the add menu option, the program should do the following:

Prompt the user for a word in English and its transation in Spanish, and a position in the arrays at which to insert these new words.

Call the insert method twice to insert each word in the correct array

Hint: don't forget to update the count of words

  • For the remove menu option, the program should do the following:

Prompt the user for the word to remove in English.

Search for the position of this word inside arrayEnglish

If the word is not in the array, it should provide an error message to the user (see sample output below)

Otherwise, remove the word and its translation in Spanish

Hint: don't forget to update the count

  • For the search for a translation menu option, the program should do the following:

Prompt the user for the a word to search in English.

Search for the position of this word inside arrayEnglish

If the word is not in the array, it should provide an error message to the user (see sample output below)

Otherwise, display the word and its corresponding translation in Spanish

  • For the exit menu option, the program should do the following:


Prompt the user to enter the name of a file in which to print the words

End the program


  • Note: that you should also provide an error message if the user types an incorrect menu option (see sample output below) or if the array is full and no new values can be added (hint: you can make a check in your menu option 1 for this case)

  • To begin, copy and paste the starter code into a file called Translator.java

  • Make sure that you implement and call all of the methods whose signatures are specified below

  • Note that you made add any additional methods that you like to complete the program

  • When your program is working *identically* (including spacing and wording!) to the sample output, please upload Translator.java to Canvas.


Code Script:

/**
* @author 
* @author 
* CIS 36B
*/
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

public class Translator {
   
    public static void main(String[] args) throws IOException {
        System.out.println("Welcome to the Spanish Vocabulary Builder!");
        String choice = "", word1 = "", word2 = "";
        int pos = -1;
        int countValues = 0;
        boolean flag = true;
        
        File englishFile = new File("./english.txt");
    	File spanishFile = new File("./spanish.txt");
    	
    	String arrayEnglish[] = new String[100];
    	String arraySpanish[] = new String[100];
    	
    	Scanner english = new Scanner(englishFile);
    	Scanner spanish = new Scanner(spanishFile);
    	while(english.hasNextLine()) {
    		arrayEnglish[countValues] = english.nextLine();
    		arraySpanish[countValues] = spanish.nextLine();
    		countValues++;
    		}
    	
    
    	Scanner input = new Scanner(System.in);
    	
    	do {
    		System.out.println("\nBelow are the words in English: ");
        	printArrayConsole(arrayEnglish, countValues);
        	System.out.println("Please select a menu option below (1-4):");
    		System.out.println("\n1. Add a new word\n" +
                    "2. Remove a word\n" +
                    "3. View a word's translation\n" +
                    "4. Quit");
    		System.out.print("Enter your choice: ");	
    		
    		choice = input.next(); 	
    		if(choice.equals( "1")) {
    			
    			System.out.print("Enter the word to add in English: ");
    			word1 = input.nextLine();
    			
    			System.out.print("Enter book's Spanish translation: ");
    			
      			word2 = input.nextLine();
      			

    			System.out.print("Enter the alphabetical position in the list for book(0- " + countValues + "): ");
    			pos = input.nextInt();
    			
    			insert(arrayEnglish, countValues, pos, word1);
    			insert(arraySpanish, countValues, pos, word2);
    			countValues++;
    			
    		}
    		
    		else if (choice.equals("2")) {
    			
    			System.out.print("Enter the word to remove: ");
    			word1 = input.nextLine();
    			pos = linearSearch(arrayEnglish, word1, countValues);
    			
    			if (pos == -1) {
    				
    				System.out.println("Sorry! That word does not exist in this dictionary.");
    			}
    			else {
    			pos = linearSearch(arrayEnglish, word1, countValues);
    				remove(arrayEnglish, countValues, pos);
    				remove(arraySpanish, countValues, pos);
    				countValues--;
    				}
    		}
    		else if (choice.equals("3")) {
    			System.out.print("Enter the word in English: ");
    			
    			word1 = input.nextLine();
    			
    			pos = linearSearch(arrayEnglish, word1, countValues);
    			if (pos == -1) {
    				System.out.println("Sorry! That word does not exist in this dictionary.");
    			}
    			else {
    				System.out.println(arrayEnglish[pos]+" = "+arraySpanish[pos]);
    				}
    			
    		}
    		else if (choice.equals("4")) {
    			System.out.print("Enter the name of the output file: ");
    			File outFile = new File(input.next());
    			printArraysFile(arrayEnglish, arraySpanish, countValues, outFile);
    			System.out.println("You may now open " + outFile + " to view the translations.");
    			System.out.println("Goodbye!");
    			
    		}
    		else {
    			System.out.println("That menu option is invalid. Please try again.");
    			input.nextLine();
    		}
    		
    		
    	} while(!choice.equals("4"));}
    
    	
        
    

   
    /**
     * Inserts a String element into an array at a specified index
     * @param array the list of String values
     * @param numElements the current number of elements stored
     * @indexToInsert the location in the array to insert the new element
     * @param newValue the new String value to insert in the array
     */
 
    public static void insert(String array[], int numElements,
            int indexToInsert, String newValue) {
        if (array.length == numElements) {
            System.out.println("Array is full. No room to insert.");
            return;
        }
        for (int i = numElements; i > indexToInsert; i--) {
            array[i] = array[i-1];
        }
        array[indexToInsert] = newValue;    
    }
   
    /**
     * Removes a String element from an array at a specified index
     * @param array the list of String values
     * @param numElements the current number of elements stored
     * @param indexToRemove where in the array to remove the element
     */
   
    public static void remove(String array[], int numElements, int indexToRemove) {
        for (int i = indexToRemove; i < numElements - 1; i++) {
        	array[i] = array[i + 1]; 
        }
        return;
    }
   
    /**
     * Prints an arrays of Strings to the console
     * in the format #. word
     * @param words the list of words in English
     * @param numElements the current number of elements stored
     */
    public static void printArrayConsole(String[] words, int numElements)  {   
    	for (int i = 0; i < numElements; i++) {
    		System.out.println(i + ". " + words[i]);
    		}
    }

    /**
     * Prints two arrays of Strings to a file
     * in the format #. english word: spanish word
     * @param english the list of words in English
     * @param spanish the list of corresponding translations in Spanish
     * @param numElements the current number of elements stored
     * @param file the file name
     */
    public static void printArraysFile(String[] english, String[] spanish,
            int numElements, File fileName) throws IOException {
       PrintWriter output = new PrintWriter(fileName);
       for (int i = 0; i < numElements; i++) {
    	   output.println(i+". "+english[i]+": "+spanish[i]);
       }
       
    }
   
   /**
    * Searches for a specified String in a list
    * @param array the array of Strings
    * @param value the String to search for
    * @param numElements the number of elements in the list
    * @return the index where value is located in the array
    */
   public static int linearSearch(String array[], String value, int numElements) {
       for (int i = 0; i < numElements; i++) {
    	   if (array[i].equalsIgnoreCase(value)) {
    		   return i; }
       }
       return -1;
   	}
}

Are you looking for Java Web and console-based Programming experts to solve your assignment, homework, coursework, coding, and projects? Codersarts web developer experts and programmers offer the best quality Java web and console-based Programming programming, coding or web programming experts. Get Web Assignment Help at an affordable price from the best professional experts Assignment Help. Order now at get 15% off. CONTACT NOW



Contact Us

Tel: (+91) 0120  4118730  

Time :   10 : 00  AM -  08 : 00 PM IST 

Registered address: G-69, Sector 63, 

 Noida - 201301, India

We Provide Services Across The different countries

USA    Australia   Canada   UK    UAE    Singapore   New Zealand    Malasia   India   Ireland   Germany

CodersArts is a Product by Sofstack Technology Solutions Pvt. Ltd.

  • CodersArts | Linkedin
  • Instagram