Difference between revisions of "Esperanto"

From CompSciWiki
Jump to: navigation, search
Line 67: Line 67:
  
  
==Organize the Code==
 
First, organized code is vital to the readability of the code. The [http://courses.cs.umanitoba.ca/index.asp?sec=3394&too=30&eve=1&ppa=5178 COMP 1010 Coding Standards] give the foundation required to complete the code organization task for this [[Case_Study_I|case study]]. Each of the headings below describe a specific task that helps organize the code.
 
  
===Separate the Statements===
 
The code file contains more than one [[Your First Java Program#Statements|statements]] on each line.
 
<pre>
 
int digit1 = (isbn % 10);int total = digit1 * 9;isbn = isbn / 10;
 
int digit2 = (isbn % 10);total = total + digit2 * 8;isbn = isbn / 10;
 
</pre>
 
Each of the above lines of code performs a similar function. Each line can be interpretted as a block of code. By placing each [[Your First Java Program#Statements|statements]] on a separate line and grouping the statements into appropriate code blocks, the result should look something like the following:
 
<pre>
 
int digit1 = (isbn % 10);
 
int total = digit1 * 9;
 
isbn = isbn / 10;
 
int digit2 = (isbn % 10);
 
total = total + digit2 * 8;
 
isbn = isbn / 10;
 
</pre>
 
By placing each of the [[Your First Java Program#Statements|statements]] on a separate line, the readability of the code increases dramatically.
 
  
===Separate Statements into Code Blocks===
 
Once the [[Your First Java Program#Statements|statements]] are readable, the next step would be to organize them into code blocks as stated in [http://courses.cs.umanitoba.ca/index.asp?sec=3394&too=30&eve=1&ppa=5178 COMP 1010 Coding Standards]. Continuing with our previous example, the functionality of the code can be broken into two distinct code blocks.
 
<pre>
 
int digit1 = (isbn % 10);
 
int total = digit1 * 9;
 
isbn = isbn / 10;
 
  
int digit2 = (isbn % 10);
 
total = total + digit2 * 8;
 
isbn = isbn / 10;
 
</pre>
 
Almost each line of code in the messy code file can be considered a separate code block. Take the time to read the code and understand how everything works together before deciding which [[Your First Java Program#Statements|statements]] should be grouped together.
 
  
One of the code blocks that should be added is a variable declaration code block at the beginning of [[Your First Java Program#The Main Method|the main method]]. Throughout the [[Case Study I#Code|messy code]], integers are declared. All of the [[Variables and Literals#Variables|declaration statements]] should be placed at the beginning of [[Your First Java Program#The Main Method|the main method]] to ensure the code stays organized. Going to the previous example, two [[Variables and Literals#Variables|declaration statements]] can be moved to the top as depicted below.
 
<pre>
 
int digit1;
 
int digit2;
 
  
digit1 = (isbn % 10);
 
int total = digit1 * 9;
 
isbn = isbn / 10;
 
 
digit2 = (isbn % 10);
 
total = total + digit2 * 8;
 
isbn = isbn / 10;
 
</pre>
 
 
===Add Comments to Explain the Code===
 
The [http://courses.cs.umanitoba.ca/index.asp?sec=3394&too=30&eve=1&ppa=5178 COMP 1010 coding standards], or the Funky Books Inc. coding standards, make numerous points concerning [[Comments|comments]] in code. To be specific, statements 1, 2, 4, and 7 can be applied to the code file for this [[Case Study I|case study]]. All major code blocks should be identified by now. Look over each code block and briefly explain what it does in a comment. Since all [[Variables and Literals#Variables|declaration statements]] have been moved to the top of the [[Your First Java Program#The Main Method|the main method]] , make sure to apply coding standard 7 from the [http://courses.cs.umanitoba.ca/index.asp?sec=3394&too=30&eve=1&ppa=5178 COMP 1010 Coding Standards].
 
 
==Optimize the Code by Removing Unnecessary Variables==
 
At the top of [[Your First Java Program#The Main Method|the main method]], there should now be a number of [[Variables and Literals#Variables|variables]]
 
declared. Notice that there are nine different "digit" [[Variables and Literals#Variables|variables]] which are only used once to store the same calculation.
 
 
<pre>
 
int digit1;
 
int digit2;
 
 
digit1 = (isbn % 10);
 
int total = digit1 * 9;
 
isbn = isbn / 10;
 
 
digit2 = (isbn % 10);
 
total = total + digit2 * 8;
 
isbn = isbn / 10;
 
</pre>
 
The code sample from above shows two of the nine digit [[Variables and Literals#Variables|variables]]. These two [[Variables and Literals#Variables|variables]] can be replaced with a single one as follows:
 
<pre>
 
int digit;
 
 
digit = (isbn % 10);
 
int total = digit * 9;
 
isbn = isbn / 10;
 
 
digit = (isbn % 10);
 
total = total + digit * 8;
 
isbn = isbn / 10;
 
</pre>
 
 
==Fix the Errors in the Code==
 
The [[Case Study I#Code|messy code]] contained a total of eight different coding errors. Each error presented in this section is ordered as it appears in the [[Case Study I#Code|code]] from the [[Case Study I|case study]].
 
===Error One===
 
<pre>
 
String temp = JOptionPane.showInputDialog("")
 
</pre>
 
Technically, the above code does not break the functionality of the application as the method call still makes the [[Input using JOptionPane#Input Dialog|input dialog]] appear to take input from the user. Although, from a usability standpoint the code does cause an error. The user who is running the application needs to know what to enter as input into the [[Input using JOptionPane#Input Dialog|input dialog]]. Without a proper message, the user cannot be expected to know what the application is expecting as input. An example of a proper message is as follows:
 
<pre>
 
temp = JOptionPane.showInputDialog("Enter the first 9 digits of a 10-digit ISBN number.");
 
</pre>
 
 
===Error Two===
 
<pre>
 
int isbn = Temp;
 
</pre>
 
There are two problems with the above code sample. The first problem being that the [[Variables and Literals#Variables|variable]] "Temp" is of type [[Strings|string]] and not of the primitive type [[Common Primitive Variables#Primitive Type int|int]]. The second problem is the name of the [[Variables and Literals#Variables|variable]] "Temp". The [[Variables and Literals#Variables|variable]] was originally declared as "temp" and Java is a [[Your First Java Program#Case Sensitivity|case sensitive]] language. Both programs are repaired by replacing the code with the line below.
 
<pre>
 
isbn = Integer.parseInt(temp);
 
</pre>
 
 
===Error Three===
 
<pre>
 
isbn = isbn // 10;
 
</pre>
 
An extra front slash changes the [[Arithmetic Operators|division operation]] into a comment which also comments out the semi-colon required at the end of every [[Your First Java Program#Statements|statement]]. Remove the extra front slash to correct the error.
 
<pre>
 
isbn = isbn / 10;
 
</pre>
 
 
===Error Four===
 
<pre>
 
digit = (isbn % 10)
 
</pre>
 
The [[Your First Java Program#Statements|statement]] above is missing the semi-colon at the end of the line. Add the semi-colon to fix the error.
 
<pre>
 
digit = (isbn % 10);
 
</pre>
 
 
===Error Five===
 
<pre>
 
total = total + digit + 6;
 
</pre>
 
This error is known as a run time error. A run time error does not cause a compilation error, but it causes the program to produce incorrect results. In the explaination of the [[Case Study I#ISBN Check Digit Overview|ISBN check digit]], the sixth ISBN number should be multiplied by six, not added. This error is fixed by changing the second [[Arithmetic Operators|addition]] operation to a [[Arithmetic Operators|multiplication]] operation.
 
<pre>
 
total = total + digit * 6;
 
</pre>
 
 
===Error Six===
 
<pre>
 
digit = (isbn / 10);
 
</pre>
 
Just like [[#Error Five|error five]], the above [[Your First Java Program#Statements|statement]] causes a run time error. To isolate the last digit in the ISBN number, the ISBN must have the [[Arithmetic Operators|modulus operator]] applied, not the [[Arithmetic Operators|division operator]]. The above statement is fixed by replacing the [[Arithmetic Operators|division operator]] with the [[Arithmetic Operators|modulus operator]].
 
<pre>
 
digit = (isbn % 10);
 
</pre>
 
 
===Error Seven===
 
<pre>
 
digit = (isbn * 10);
 
</pre>
 
The above [[Your First Java Program#Statements|statement]] is almost identical to [[#Error Six]]. The [[Arithmetic Operators|multiplication operator]] should be replaced with the [[Arithmetic Operators|modulus operator]].
 
<pre>
 
digit = (isbn % 10);
 
</pre>
 
 
===Error Eight===
 
<pre>
 
total = total + digit - 1;
 
</pre>
 
The error in the code above is [[#Error Five|error five]] almost identical to [[#Error Five]]. Replace the [[Arithmetic Operators|subtraction operator]] with the [[Arithmetic Operators|multiplication operator]] to remedy this error.
 
<pre>
 
total = total + digit * 1;
 
</pre>
 
 
==Add Code to Output Progress Reports as the Program Executes==
 
To add progress reports to the program, three things need to be addressed:
 
<ul>
 
<li>Where to place the progress reports</li>
 
<li>What progress should be reported</li>
 
<li>Adding the code to report progress</li>
 
</ul>
 
By adding code to report on the status of the application, the user and yourself know what is happening behind the scenes when the program is executing. For a small application like this [[Case Study I|case study]] it may seem trivial. It is a good practice to get into as in larger programs consisting of many files each with many methods, it can be difficult to locate a run-time error in your code if the program does not explicitly say what it is currently doing.
 
 
===Location===
 
By now the code file should be broken up into multiple code blocks, as done [[#Separate the Statements into Code Blocks|earlier in this solution]]. Each code block should represent a major code segment in the program. Each code block can be considered a potential point for a progress report. Read over the code and decide which are vital points in the execution of the program.
 
 
===Report Content===
 
The next step is to decide what should be outputted to describe the progress of the program. This output can be the current value of a [[Variables and Literals|variable]], an output statement saying that the program has reached a certain point in the code, or potentially a combination of both. A progress report should output data on the program that is relevant to its execution. For this [[Case Study I|case study]], each calculation performed on the ISBN is vital to determining the check digit, therefore a progress report should output the value of each calculation.
 
 
===Adding Output Code===
 
The best way to add output to the program is by adding [[Output using System.out|System.out]] statements to the appropriate code blocks. When adding the code, make sure the message that will be outputted is unique in comparison to the other progress report [[Your First Java Program#Statements|statements]]. Each statement should be unique as they are meant to identify the section of code being executed. If the statements are not unique, then there will be no way of telling which progress report has been outputted. Here is an example of the code before adding a [[Output using System.out|System.out]] statement.
 
<pre>
 
//isolate last digit and multiply by 9
 
digit = (isbn % 10);
 
total = digit * 9;
 
isbn = isbn / 10;
 
</pre>
 
After the output [[Your First Java Program#Statements|statement]] is added, the code will look like the following:
 
<pre>
 
//isolate last digit and multiply by 9
 
digit = (isbn % 10);
 
total = digit * 9;
 
isbn = isbn / 10;
 
System.out.println (digit + " * " + 9 + " for a running total of " + total);
 
</pre>
 
When the program is executing, the following output will appear in the progress window. The ISBN number used as input for the example below is 1-2345-6789.
 
<pre>
 
9 * 9 for a running total of 81
 
</pre>
 
Now the you will know what part of the code is being executed along with the status of the current calculation.
 
 
Remember that this is not the only "correct" progress report. There can be many different progress reports based on how the vital points of the program were interpreted. Although, in this [[Case Study I|case study]] the calculations are exceptionally important points to the correct execution of the program.
 
  
 
|SolutionCode=
 
|SolutionCode=
Line 361: Line 175:
 
     }//end main
 
     }//end main
 
}//end class
 
}//end class
|SideSectionTitle=MY PICTURE
+
|SideSectionTitle=Esperanto
 
|SideSection=
 
|SideSection=
 
[[Image:Wiki_start01.jpg|center]]<BR>
 
[[Image:Wiki_start01.jpg|center]]<BR>
  
 
}}
 
}}

Revision as of 11:56, 29 March 2011

Back to the Case Studies homepage

Problem

Esperanto

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.


  • Organize the code.
  • Add comments to explain the code.
  • Optimize the code by removing unnecessary variables.
  • Find any errors in the code.
  • Add code to output progress reports as the program executes.

Remember to abide by the company coding standards while repairing the code.

 

Esperanto

Wiki start01.jpg

Solution

ENTER SOLUTION There are nine steps to improve this messy code so it complies with the company (and coincidentally, comp 1010) coding standards:

Code

Solution Code

Back to the Case Studies homepage