Lab 2: Working with Arrays (100 pts)

Due Monday, April 27 at 11:59pm on Canvas


Pair Programming Required (or No Credit)
  • Both partners fill in, sign, date, and submit the pair programming contract
  • Upload the document(s) along with your Lab code to Canvas.
  • Only ONE partner submits the Lab code on Canvas.
  • BOTH partners submit the contract.
  • Please make sure both your names are on all the files.
  • If you need help finding a partner, please contact me as soon as possible.


Spanish Vocabulary Helper

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

  • 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.

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

    • 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.

    • 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 a 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.



Starter Code

/**

* @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;

        }  

    }


   

    /**

     * 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) {

       

    }

   

    /**

     * 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) {

       

    }

   

    /**

     * 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)  {   

      

    }


    /**

     * 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, String fileName) throws IOException {

      

    }

   

   /**

    * 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) {

      

       return -1;

   }


}



Sample Output



Welcome to the Spanish Vocabulary Builder!

Below are the words in English:

0. bear
1. black
2. blue
3. cat
4. cow
5. dog
6. fall
7. gray
8. green
9. purple
10. red
11. spring
12. summer
13. to be
14. to be able
15. to go
16. to love
17. winter
18. white
19. yellow

Please select a menu option below (1-4):

1. Add a new word
2. Remove a word
3. View a word's translation
4. Quit

Enter your choice: 10

That menu option is invalid. Please try again.

Below are the words in English:

0. bear
1. black
2. blue
3. cat
4. cow
5. dog
6. fall
7. gray
8. green
9. purple
10. red
11. spring
12. summer
13. to be
14. to be able
15. to go
16. to love
17. winter
18. white
19. yellow

Please select a menu option below (1-4):

1. Add a new word
2. Remove a word
3. View a word's translation
4. Quit

Enter your choice: 1

Enter the word to add in English: book
Enter book's Spanish translation: libro
Enter the alphabetical position in the list for book(0-20): 3

Below are the words in English:

0. bear
1. black
2. blue
3. book
4. cat
5. cow
6. dog
7. fall
8. gray
9. green
10. purple
11. red
12. spring
13. summer
14. to be
15. to be able
16. to go
17. to love
18. winter
19. white
20. yellow

Please select a menu option below (1-4):

1. Add a new word
2. Remove a word
3. View a word's translation
4. Quit

Enter your choice: 3

Enter the word in English: golf

Sorry! That word does not exist in this dictionary.

Below are the words in English:

0. bear
1. black
2. blue
3. book
4. cat
5. cow
6. dog
7. fall
8. gray
9. green
10. purple
11. red
12. spring
13. summer
14. to be
15. to be able
16. to go
17. to love
18. winter
19. white
20. yellow

Please select a menu option below (1-4):

1. Add a new word
2. Remove a word
3. View a word's translation
4. Quit

Enter your choice: 3

Enter the word in English: book

book = libro

Below are the words in English:

0. bear
1. black
2. blue
3. book
4. cat
5. cow
6. dog
7. fall
8. gray
9. green
10. purple
11. red
12. spring
13. summer
14. to be
15. to be able
16. to go
17. to love
18. winter
19. white
20. yellow

Please select a menu option below (1-4):

1. Add a new word
2. Remove a word
3. View a word's translation
4. Quit


Enter your choice: 1

Enter the word to add in English: funny
Enter funny's Spanish translation: chistoso
Enter the alphabetical position in the list for funny(0-21): 8

Below are the words in English:

0. bear
1. black
2. blue
3. book
4. cat
5. cow
6. dog
7. fall
8. funny
9. gray
10. green
11. purple
12. red
13. spring
14. summer
15. to be
16. to be able
17. to go
18. to love
19. winter
20. white
21. yellow

Please select a menu option below (1-4):

1. Add a new word
2. Remove a word
3. View a word's translation
4. Quit

Enter your choice: 2

Enter the word to remove: scarf

Sorry! That word does not exist in this dictionary.

Below are the words in English:

0. bear
1. black
2. blue
3. book
4. cat
5. cow
6. dog
7. fall
8. funny
9. gray
10. green
11. purple
12. red
13. spring
14. summer
15. to be
16. to be able
17. to go
18. to love
19. winter
20. white
21. yellow

Please select a menu option below (1-4):

1. Add a new word
2. Remove a word
3. View a word's translation
4. Quit

Enter your choice: 2

Enter the word to remove: to be able

Below are the words in English:

0. bear
1. black
2. blue
3. book
4. cat
5. cow
6. dog
7. fall
8. funny
9. gray
10. green
11. purple
12. red
13. spring
14. summer
15. to be
16. to go
17. to love
18. winter
19. white
20. yellow

Please select a menu option below (1-4):

1. Add a new word
2. Remove a word
3. View a word's translation
4. Quit

Enter your choice: 5

That menu option is invalid. Please try again.

Below are the words in English:

0. bear
1. black
2. blue
3. book
4. cat
5. cow
6. dog
7. fall
8. funny
9. gray
10. green
11. purple
12. red
13. spring
14. summer
15. to be
16. to go
17. to love
18. winter
19. white
20. yellow

Please select a menu option below (1-4):

1. Add a new word
2. Remove a word
3. View a word's translation
4. Quit

Enter your choice: 4

Enter the name of the output file: translations.txt

You may now open translations.txt to view the translations.

Goodbye!

Contents of corresponding output file:

0. bear: oso

1. black: negro

2. blue: azul

3. book: libro

4. cat: gato

5. cow: vaca

6. dog: perro

7. fall: otono

8. funny: chistoso

9. gray: gris

10. green: verde

11. purple: morado

12. red: rojo

13. spring: primavera

14. summer: verano

15. to be: ser

16. to go: ir

17. to love: amar

18. winter: invierno

19. white: blanco

20. yellow: amarillo


  • Note that the user should be able to enter any file name of their choice. It does not have to be translations.txt.



How You Will Be Graded:
  • 100 points: The assignment works as shown in the sample output. All required features are implemented and implemented correctly.
  • 80-100 points: The assignment works mostly as shown in the sample output. Most required features are implemented and implemented correctly. Wording or spacing choices may differ from those shown.
  • 40-80 points: Some of the assignment works as shown in the sample output. Some required features are implemented and implemented correctly.
  • 10-40 points: Serious problems in implementation and little correspondence between sample output and what is shown. Few of the features implemented or implemented correctly.
  • 0 points: Code does not compile or outputs an error at runtime.
  • Incorrect formatting (indentation) and incorrect or missing Javadoc comments will each lead to a 5 point deduction.
  • Please remove any package statements from the top of your program before submitting (5 point deduction).
  •