A2: Decimal Palindromes

On This Page


Overview

A palindrome is a number or a text phrase that reads the same backwards as forwards. A decimal palindrome is a special case of a palindrome, where the left side of the decimal point is reversed from the right side of the decimal point. For example, the numbers 12345.54321 and 23232.23232 are decimal palindromes, but the number 12345.23456 is not.

Specifications

Operation

Write an application named DecimalPalindrome that prompts the user and reads a ten digit number from the command line. With the decimal point in the middle, the user enters a total of eleven characters. The program then reports whether or not the number entered is a valid decimal palindrome.

To check if an entry is a decimal palindrome, you will need to isolate each numerical digit. An effective way to accomplish this task is by using the division and modulus operators.

Sample Operation and Output

I find decimal palindromes.

Enter a 10-digit number with a
decimal point in the middle: 12345.54321
12345.54321 is a decimal palindrome!!!
Another number (y/n)? y
Enter a 10-digit number with a
decimal point in the middle: 12345.65432
12345.65432 is not a decimal palindrome.
Another number (y/n)? n

Additional Specifications

  1. Use the method named isDecimalPalindrome to check whether a double number is a valid decimal palindrome.
  2. Do not put any user input or output code in the isDecimalPalindrome method.
  3. Do not use any String or StringBuffer methods, character arrays or similar techniques to check for a decimal palindrome. Use only arithmetic operations and control-flow statements inside the isDecimalPalindrome method.
  4. Allow users to keep entering more numbers to check until they decide to quit the program.
  5. You may assume that users will only enter valid data types and numbers of the correct length.
  6. Note that rounding errors can occur when using floating-point numbers such as doubles. You must deal with rounding errors when processing and evaluating the decimal numbers entered by users to ensure decimal palindromes are evaluated correctly.
  7. The program must pass all test cases for full credit.
  8. You must document and organize your code following the requirements in the How To Document and Organize Java Code that we have covered so far. Note that both jEdit and TextPad can automatically check many items of your programming style to help you locate problems.
  9. Do not put your code into a package at this time.

Starter Code

Use the following code to get started. Do not change the names of the methods in the starter code, though you may add more methods if you like.

import java.util.Scanner;

/**
 * CS-20J  Asn02
 * DecimalPalindrome.java
 * Purpose: Tests user input for a decimal palindrome.
 *
 * @version 1.0 2/12/05
 * @author Your Name
 */
public class DecimalPalindrome {
    // You may use these constants if you want
    public static final int E5 = 100000; // exponent 5th digit
    public static final int E4 = 10000;  // exponent 4th digit
    public static final int E3 = 1000;   // exponent 3rd digit
    public static final int E2 = 100;    // exponent 2nd digit
    public static final int E1 = 10;     // exponent 1st digit
    public static final double ROUND5 = 0.0000049;

    /**
     * The main method contains all user input and output.
     *
     * @param args Not used.
     */
    public static void main(String args[]) {
        Scanner input  = new Scanner(System.in);

        System.out.println("I find decimal palindromes.\n");
        do {
            System.out.print("Enter a 10-digit number with a\n"
                + "decimal point in the middle: ");
            double number = input.nextDouble();
            boolean valid = isDecimalPalindrome(number);
            if (valid) {
                System.out.println(number
                    + " is a decimal palindrome!!!");
            } else {
                System.out.println(number
                    + " is not a decimal palindrome.");
            }
            System.out.print("Another number (y/n)? ");
        } while (input.next().charAt(0) == 'y');
    }

    /**
     * Checks if number is a decimal palindrome.
     *
     * @param number The number to check.
     * @return true if number is a decimal palindrome,
     *      otherwise false.
     */
    public static boolean isDecimalPalindrome(double number) {
        // Add code here to check decimal palindromes
        return false; // change this line
    }
}

Test Cases

Testing is an important part of software development. Usually, programmers develop tests for each unit of code they produce. This is know as "unit testing" and allows the programmer to verify that the code they develop works correctly.

To assist you in developing your code, I provide tests for this assignment. These tests, known collectively as "test cases", help to make sure that your assignment meets its requirements. I will use these test cases (and perhaps others) to test your assignment, and you should too. If the test cases do not pass, then you will lose points for the assignment.

Install the tests by copying the following file into the same directory as your .java source code files:

Run the tests as you would any other Java program. You will need to compile your source code before compiling and running the test code.

Extra Credit

The following are worth extra credit points:

  1. Install CheckStyle (see jEdit and TextPad) and use it to check your programming style. Document your installation experience in your README.txt file. (1 point)
  2. Use a loop for isolating digits so that your decimal palindromes can be any length up to 12 digits (not counting the decimal point). (1 point)
  3. Use a BigDecimal for user input (that is nextBigDecimal()), along with the loop in extra credit #2, for isolating digits so that your decimal palindromes can be any practical length. Note that you will need to write a second method that accepts a BigDecimal argument to implement this extra credit. (2 points)
public static boolean isDecimalPalindrome(BigDecimal number) {
        // Add code here to check decimal palindromes
        return false; // change this line
}

Make certain that your README.txt file lists any extra credit attempted.

Grading Criteria

The instructor will evaluate your assignment using the following criteria. Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive.

Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.

For information on grading policies, including interpretation of scores, see the course information page.

Program Compilation

  • 4: Source code compiles with no errors or warnings
  • 2: Source code compiles with warnings
  • 0: Does not compile

Functionality

  • 10: Demonstrates mastery of the assignment
    • Has extra features or demonstrates techniques beyond the assignment
    • Applies concepts from the lessons appropriately
    • Meets all specifications (see above) with particularly elegant solutions
    • Runs to completion with no abnormal error conditions
    • Generates correct output given correct input
    • Behaves in a reasonable way in response to incorrect data
  • 8: Has all the functionality expected of the assignment
    • Demonstrates many techniques from the lesson
    • Meets all specifications (see above)
    • Implementation seems more complicated than necessary.
    • May have one minor error
  • 6: Has most of the functionality expected of the assignment
    • Demonstrates some techniques from the lesson
    • Meets all but one of the specifications (see above)
    • Implementation seems excessively complicated.
    • May have 2-3 minor errors
  • 4: Has some of the functionality expected of the assignment
    • Demonstrates some techniques from the lesson
    • Meets at least 1/2 of the specifications (see above)
    • Implementation seems excessively complicated.
    • May have more than 3 minor errors
  • 2: Serious functional problems but shows some effort and understanding
    • Meets less than 1/2 of the of the specifications (see above)
    • Has a major error or many minor errors
    • Implementation seems very convoluted
    • Demonstrates few techniques from the lesson
  • 0: Does not execute

Programming Style

  • 4: Code is well-documented
    • Name, date, and page description in opening comment block
    • Specified class and method names are used
    • Proper use of whitespace and indenting
    • Descriptive variable names
    • Meets all requirements in How To Document and Organize Java Code covered to date
  • 3: Code has minor documentation errors
  • 2: Code has some documentation errors
  • 1: Code has many documentation errors
  • 0: No apparent attempt to document code

REAME.txt File

  • 2: README.txt file submitted with specified information included
  • 1: README.txt submitted but some information was not included
  • 0: No README.txt submitted

Maximum Score: 20, plus extra credit

What to Turn In

Submit your assignment following the instructions for homework. Include the following items for grading:

  1. README.txt file
  2. DecimalPalindrome.java

You must submit all the files needed to make your assignment function properly. Do not assume that the instructors has any files. Your assignment must work as submitted.

Home | WebCT | Announcements | Schedule | Room Policies | Course Info
Help | FAQ's | HowTo's | Links

Last Updated: April 01 2005 @16:37:27