Difference between revisions of "Esperanto"
(Added solution code and problem text) |
|||
Line 67: | Line 67: | ||
+ | |SolutionCode= | ||
− | + | import java.util.Scanner; | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | import java.util. | + | |
/** | /** | ||
− | * | + | * A2Q2 - |
* | * | ||
− | * @author | + | * COMP 010 Section A00 |
− | * @version | + | * INSTRUCTOR |
+ | * ASSIGNMENT 2, Question 2 | ||
+ | * @author 1010 | ||
+ | * @version 2010-Oct-18 | ||
+ | * | ||
+ | * PURPOSE: calculates the decimal version of a binary number. | ||
*/ | */ | ||
+ | |||
+ | public class A2Q2 | ||
+ | { | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | public static void main(String[] args) | |
− | + | { | |
− | + | Scanner kbd = new Scanner(System.in); | |
− | + | ||
− | + | ||
− | + | ||
− | + | long binaryNumber, copyOfBinNum; // input from the user. | |
− | + | int decimalNumber; // output value. | |
− | + | final int BASE = 2; // binary base. | |
− | + | int currDigit, currPower; // values used in loops. | |
− | + | // get input from the user. | |
− | + | System.out.print("Enter a binary number:"); | |
+ | binaryNumber = kbd.nextLong(); | ||
− | + | // keep looping until -1 is encountered. | |
− | + | while (binaryNumber != -1) | |
− | + | { | |
− | + | ||
− | + | ||
− | + | // reset values to 0, use copyOfBinNum for calculations. | |
− | + | decimalNumber = 0; | |
− | + | currPower = 0; | |
− | + | copyOfBinNum = binaryNumber; | |
− | + | ||
− | + | // keep looping until all digits of copyOfBinNum are removed. | |
− | + | while (copyOfBinNum > 0) | |
− | + | { | |
− | + | ||
− | + | ||
− | + | // get last digit and strip it off copyOfBinNum | |
− | + | currDigit = (int) copyOfBinNum % 10; | |
− | + | copyOfBinNum /= 10; | |
− | + | ||
− | + | ||
− | + | // add to the running total if the digit is one | |
− | + | if (currDigit == 1) | |
− | + | { | |
− | + | decimalNumber += Math.pow(BASE,currPower); | |
− | + | } | |
− | + | // increment the power of two for next loop. | |
− | + | currPower++; | |
− | + | } | |
− | + | ||
− | + | ||
− | + | // output the right decimal value. | |
− | + | System.out.println(binaryNumber + " in decimal is " + decimalNumber + "."); | |
− | + | ||
− | + | ||
− | + | ||
− | + | // get ready for next iteration of the loop. | |
− | + | System.out.print("Enter a binary number:"); | |
− | + | binaryNumber=kbd.nextLong(); | |
− | + | } | |
− | + | ||
+ | System.out.println("Programmed by [your name here]."); | ||
+ | System.out.println("End of processing."); | ||
− | + | } | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | } | |
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|SideSectionTitle=Esperanto | |SideSectionTitle=Esperanto | ||
|SideSection= | |SideSection= |
Revision as of 12:04, 29 March 2011
Back to the Case Studies homepage
ProblemEsperanto 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:
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.
Remember to abide by the company coding standards while repairing the code. |
Esperanto | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
SolutionENTER SOLUTION There are nine steps to improve this messy code so it complies with the company (and coincidentally, comp 1010) coding standards: | ||||||||||||
Code |