Esperanto

From CompSciWiki
Revision as of 12:47, 31 March 2011 by ErikM (Talk | contribs)

Jump to: navigation, search

Back to the Case Studies homepage

Problem

Esperanto 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:


If the word ends in .. it is a(n)...
aadjective
o or onsingular noun
oj or ojnplural noun
eadverb


This means there are no exceptions in Esperanto, like how in English the plural of goose is geese and while you usually add 'ly' to the end of an adjective to make it an adverb, 'goodly' isn't an adverb.

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

Wiki start01.jpg

Solution

Remember to abide by the COMP 1010 coding standards while writing the code.

Organize the Code

When 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:

  • Capture the User's input from the keyboard
  • Get the program to loop until some end condition is met - in this case when "cesi" is entered
  • Write the compare code to determine what was entered - ie. adjective, adverb, etc
  • Write the code to display the output

Step No. 1

Begin 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 fisrt have imported the java.util.Scanner library. Next, you must make a new instance of Scanner. Do this by delcaring a new instance. 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 you 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. 2

At 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;
   ...
}


For our program we will instert a while loop statement that looks as follows:


while (!word.equalsIgnoreCase("cesi")) 
{
	...statements....
}

The way that this statement will be evaluated is as follows:

  • word has already been assigned the input from the user (from the previsous step)
  • the while loop will take word and apply the method "equalsIgnoreCase"
  • if the result of this matches the expression "cesi" then the loop will exit
  • if the result does not match the expression "cesi" the loop will continue to run


IMPORTANT NOTE:

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 exaclty. "CESI" wound not qualify and the loop would continue to run.


CHECK POINT:

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")) 
		{
		}

	}

}


Again compile and run your code. Your code should not 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.

Code

Solution Code

Back to the Case Studies homepage