Esperanto
Back to the Case Studies homepage
ProblemEsperanto is a language invented in the 1880s by L. L. Zamenhof. It was invented to "create an easy-to-learn and politically neutral language that would serve as a universal second language to foster peace and international understanding." One of the benefits of designing your own language is that you can impose strict rules on the language. In this question, you will use the rules of Esperanto to identify the parts of speech of different words. The rules for identifying parts of speech in Esperanto are:
Write a program that accepts words in Esperanto, and identifies whether each is an adjective, singular noun, plural noun or adverb. Input:Use Scanner to accept input in this question. Prompt the user to input a word. If the user types in "cesi" ("quit" in Esperanto), the program should quit. Otherwise, it should accept the input and process it as a word in Esperanto. After processing the word, the user should be prompted to enter another word. Assume the user inputs the words entirely in lowercase and that all words are at least three letters long. Calculate and Output:Use System.out. for all output. Use the charAt() and length() methods to find the last (one or possibly two) characters and determine which part of speech (adverb, singular noun, plural noun or adverb) the word is. If the word is in none of the four categories, print out an error message telling the user that the part of speech cannot be identified. An execution of your program would look like this: Enter a word in Esperanto: komputilo komputilo is a singular noun. Enter a word in Esperanto: sciencon sciencon is a singular noun. Enter a word in Esperanto: cesi Programmed by [your name here]. End of processing. |
Esperanto | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
SolutionRemember to abide by the COMP 1010 coding standards while writing the code. Organize the CodeWhen you begin to write a solution, the best way to approach this is to divide the answer into sections of code. You want to break down the problem into smaller sections or ideas that can be solved as steps. Begin by creating a new blank solution file using your standard setup. See below: public class A2Q1 { public static void main(String[] args) { } } Next begin to write the code for the sections you defined. For this solution the steps we will use are:
Step No. 1Begin by writing the code to get the user's input from the keyboard. As per the assignment specifications, we will be using Scanner to capture what the user inputs via the keyboard. In order for you to use scanner, you must first include the "java.util.Scanner" library. To include this library add the below import statement as first line of your solution file. import java.util.Scanner; Scanner does exactly what you think it does. It scans the keybaord logging input until it encounters a new line character. When a new line character is encountered, the Scanner will stop scanning and the keys pressed are now stored. An example of a new line character is when the "Enter" key is pressed. Usage:To use Scanner, as noted above you must first have imported the java.util.Scanner library. Next, you must make a new instance of Scanner. Do this by declaring a new instance and instantiating it. You must also allocate space to store what the Scanner captures. Do this by assigning Scanner's output to a local variable. See below: Scanner kbd = new Scanner(System.in); //create a new instance of Scanner String word; // Esperanto word - holds the User's input System.out.print("Enter a word in Esperanto: ");//print a message to let the User know what they should do word = kbd.next(); // get input from User CHECK POINT: At this point you can now compile and run the code you have. Ensure that your program does compile without any errors. If your code does not compile, look closely at the error message generated and try to correct the problem. If you successfully compile your code, try running it. Running your code should prompt you for input and you should be able to enter it. At this point, you will only be prompted for input once. Step No. 2At this point we have code that can read in input from a user. However, the current code will only read input in once and then end. We now want to modify this code so that it will read in input until some "end" condition is met. For this problem, our end condition will be when the word "cesi" is entered. Since we know what our end condition is we can test for it. The best way to carry out this test is through the use of a while loop. Please see the section on while loops to familiarize yourself with their workings. Syntax:In Java, the general form of a while loop is: while(test_condition) //this is where we test for the end condition { ... statements; ... }
while (!word.equalsIgnoreCase("cesi")) { ...statements.... } The way that this statement will be evaluated is as follows:
Note the usage of equalsIgnoreCase() here. The reason for this is to catch input that matches "cesi" in both upper and lower case. We are not concerned with the case of the letters, just that "cesi" is entered. If you choose to use equals() then "cesi" would have to be entered exactly. "CESI" wound not qualify and the loop would continue to run.
At this point your code should look as follows: public class A2Q1 { public static void main(String[] args) { Scanner kbd = new Scanner(System.in); String word; // Esperanto word System.out.print("Enter a word in Esperanto: "); word = kbd.next(); // keep going until cesi is typed. while (!word.equalsIgnoreCase("cesi")) { word = kbd.next(); } } } Again compile and run your code. Your code should continue to prompt you for input until "cesi" is entered. If you are not successful in compiling your code, pay close attention to the error message generated and try to correct the problem. Step No. 3Now that we have a program that will read in input until the user enters "cesi", we need to write the code that looks at what the user has entered. We need to define some rules so that we can determine what class the input will be classified as. We know from the problem specs that a word is classified by looking at the last character or the last two characters. This means that we need a way to grab the last character and the last two characters of the input from the user. Once we have the last character(s) we then will need to compare them to some test condition to determine what the word will be classified as. To do this we will make use of the following String methods:
Usage:The .length() method will return the length of a string. For example, if we have a variable called word which contains "cat", if we call word.length(), the number 3 will be returned. The .charAt() method will return the character at a given location. For example, if we again use the variable word which contains "cat" and call word.charAt(0), the character 'c' will be returned. It is important to note that the indexing of the string starts at 0 and NOT 1. This means that word.charAt(1) will return 'a'. Let's begin by declaring some variables that will make it easier to get the last and last two characters of a word that has been captured. We will also want to define a variable to hold the length of a word entered. wordLength = word.length(); lastChar = word.charAt(wordLength-1); secondLastChar = word.charAt(wordLength-2); Next, let's setup a rough framework of how we will classify a word. To do this we will be using if-else Statements. Please see the section on if-else statements to familiarize yourself with their workings. Syntax:In Java, the general form of an if-else statement is: if (condition) { code to execute if condition == true } else { code to execute if condition == false } The framework that we will use to classify the words entered will be based on the table given in the problem section of this document. We will first look at the last letter of the word then we will move on to the last two letters. The code for this will be as follows: if (lastChar=='a') { response = " is an adjective."; } else if (lastChar=='e') { response = " is an adverb."; } else if (lastChar=='o') { response = " is a singular noun."; } else if (lastChar=='j' && secondLastChar=='o') { response = " is a plural noun."; } else if (lastChar=='n') { if (secondLastChar=='o') { response = " is a singular noun."; } else if (secondLastChar=='j') { response = " is a plural noun."; } } else { response = " cannot be identified."; } CHECK POINT: At this point your code can again be complied and run. You will have an almost fully functional program now. Your code at this point should look as follows: public class A2Q1 { public static void main(String[] args) { Scanner kbd = new Scanner(System.in); String word; // Esperanto word String response = ""; // response to be printed. int wordLength; char lastChar, secondLastChar; // the relevant characters. // get first input. System.out.print("Enter a word in Esperanto: "); word = kbd.next(); // keep going until cesi is typed. while (!word.equalsIgnoreCase("cesi")) { // extract last and 2nd last characters from // the word. wordLength = word.length(); lastChar = word.charAt(wordLength-1); secondLastChar = word.charAt(wordLength-2); // classify the word according to these // characters. if (lastChar=='a') { response = " is an adjective."; } else if (lastChar=='e') { response = " is an adverb."; } else if (lastChar=='o') { response = " is a singular noun."; } else if (lastChar=='j' && secondLastChar=='o') { response = " is a plural noun."; } else if (lastChar=='n') { if (secondLastChar=='o') { response = " is a singular noun."; } else if (secondLastChar=='j') { response = " is a plural noun."; } } else { response = " cannot be identified."; } word = kbd.next(); } } } Step No. 4Now that our program reads in words until "cesi" is entered and it capable of classifying the words entered, we need to have some output printed to the screen. The easiest way of doing this is through the use of print statements. A print statement simply allows us to print text to the screen. A print statement in Java looks as follows: System.out.println("This is an example print statement") The above code will print to the screen: "This is an example print statement". For our program we want to print out what we have learned about the word entered. Let's print out the word entered followed by what we learned about the word. For example, is it an adjective, adverb, etc... Let's add the following lines of code: // print message and get next input System.out.println(word + response); System.out.print("Enter a word in Esperanto: "); Step No. 5Finally let's put the final touches on our program. We need to add our name to the program and we should also add a line that gets executed on a successful program run. Adding this line will allow us and others to know our program has successfully run and completed. Here is what we will add: System.out.println("Programmed by [your name here]."); System.out.println("End of processing."); COMPLETE!Congratulations! You have now finished your Esperanto program. You can now compile and run your program. You can also click the Solution button below to see a full solution. | ||||||||||||
Code |