Difference between revisions of "Esperanto"

From CompSciWiki
Jump to: navigation, search
(Added solution code and problem text)
Line 67: Line 67:
  
  
 +
|SolutionCode=
  
 
+
import java.util.Scanner;
 
+
 
+
 
+
 
+
|SolutionCode=
+
import javax.swing.*;
+
import java.util.Date;
+
  
 
/**
 
/**
  * calculate a check digit for ISBNs
+
  * A2Q2 -
 
  *
 
  *
  * @author:    1010 Instructors
+
* COMP 010 Section A00
  * @version:    2007-September
+
* INSTRUCTOR
 +
* ASSIGNMENT 2, Question 2
 +
  * @author 1010
 +
  * @version 2010-Oct-18
 +
*
 +
* PURPOSE: calculates the decimal version of a binary number.
 
  */
 
  */
 +
 +
public class A2Q2
 +
{
  
public class CaseStudy1_ISBN_Solution
 
{
 
    /**
 
    * PURPOSE: inputs a 9-digit ISBN, calculates the check digit and outputs a 10-digit ISBN
 
    */
 
    public static void main (String [] args)
 
    {
 
  
        //variables declared here
+
public static void main(String[] args)
        String temp;        //temporary input string
+
{
        int isbn;            //9-digit ISBN
+
Scanner kbd = new Scanner(System.in);
        int digit;          //isolated ISBN digit
+
        int total;          //total of isbn number when each digit is multiplied by check value
+
        int checkDigit;      //value of total%11(as per ISBN standard)
+
  
        //get input
+
long binaryNumber, copyOfBinNum; // input from the user.
        temp = JOptionPane.showInputDialog
+
int decimalNumber; // output value.
        ("Enter the first 9 digits of a 10-digit ISBN number.");
+
final int BASE = 2; // binary base.
        isbn = Integer.parseInt(temp);
+
int currDigit, currPower; // values used in loops.
  
        //confirm input
+
// get input from the user.
        System.out.println ("You have entered " + temp + ".\n\nThe weighted value of the ISBN is:");
+
System.out.print("Enter a binary number:");
 +
binaryNumber = kbd.nextLong();
  
        //isolate last digit and multiply by 9
+
// keep looping until -1 is encountered.
        digit = (isbn % 10);
+
while (binaryNumber != -1)  
        total = digit * 9;
+
{
        isbn = isbn / 10;
+
        System.out.println (digit + " * " + 9 + " for a running total of " + total);
+
  
        //isolate next digit and multiply by 8
+
// reset values to 0, use copyOfBinNum for calculations.
        digit = (isbn % 10);
+
decimalNumber = 0;
        total = total + digit * 8;
+
currPower = 0;
        isbn = isbn / 10;
+
copyOfBinNum = binaryNumber;
        System.out.println (digit + " * " + 8 + " for a running total of " + total);
+
  
        //isolate next digit and multiply by 7
+
// keep looping until all digits of copyOfBinNum are removed.
        digit = (isbn % 10);
+
while (copyOfBinNum > 0)  
        total = total + digit * 7;
+
{
        isbn = isbn / 10;
+
        System.out.println (digit + " * " + 7 + " for a running total of " + total);
+
  
        //isolate next digit and multiply by 6
+
// get last digit and strip it off copyOfBinNum
        digit = (isbn % 10);
+
currDigit = (int) copyOfBinNum % 10;
        total = total + digit * 6;
+
copyOfBinNum /= 10;
        isbn = isbn / 10;
+
        System.out.println (digit + " * " + 6 + " for a running total of " + total);
+
  
        //isolate next digit and multiply by 5
+
// add to the running total if the digit is one
        digit = (isbn % 10);
+
if (currDigit == 1)  
        total = total + digit * 5;
+
{
        isbn = isbn / 10;
+
decimalNumber += Math.pow(BASE,currPower);
        System.out.println (digit + " * " + 5 + " for a running total of " + total);
+
}
  
        //isolate next digit and multiply by 4
+
// increment the power of two for next loop.
        digit = (isbn % 10);
+
currPower++;
        total = total + digit * 4;
+
}
        isbn = isbn / 10;
+
        System.out.println (digit + " * " + 4 + " for a running total of " + total);
+
  
        //isolate next digit and multiply by 3
+
// output the right decimal value.
        digit = (isbn % 10);
+
System.out.println(binaryNumber + " in decimal is " + decimalNumber + ".");
        total = total + digit * 3;
+
        isbn = isbn / 10;
+
        System.out.println (digit + " * " + 3 + " for a running total of " + total);
+
  
        //isolate next digit and multiply by 2
+
// get ready for next iteration of the loop.
        digit = (isbn % 10);
+
System.out.print("Enter a binary number:");
        total = total + digit * 2;
+
binaryNumber=kbd.nextLong();
        isbn = isbn / 10;
+
}
        System.out.println (digit + " * " + 2 + " for a running total of " + total);
+
 +
System.out.println("Programmed by [your name here].");
 +
System.out.println("End of processing.");
  
        //isolate next digit and multiply by 1
+
}
        digit = (isbn % 10);
+
        total = total + digit * 1;
+
        isbn = isbn / 10;
+
        System.out.println (digit + " * " + 1 + " for a running total of " + total);
+
  
        //calculate check digit
+
}
        checkDigit = total % 11;
+
  
        //output weighted total and check digit
 
        System.out.println ("The weighted total is: " + total +
 
                            "\nThe check digit is : " + checkDigit +
 
                            "\n\nThe 10-digit ISBN is: " + temp + checkDigit);
 
  
        System.out.println("\nProgrammed by COMP 1010 Instructors");
 
        System.out.println("Date: " + new Date());
 
        System.out.println ("*** End of Processing ***");
 
  
    }//end main
 
}//end class
 
 
|SideSectionTitle=Esperanto
 
|SideSectionTitle=Esperanto
 
|SideSection=
 
|SideSection=

Revision as of 12:04, 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