Source of OutputFormat.java


  1: //OutputFormat.java
  2: 
  3: /**
  4:  * Solution to Self-Test Question 30.
  5:  */
  6: public class OutputFormat
  7: {
  8:     /**
  9:      * Displays a number with digitsAfterPoint digits after
 10:      * the decimal point. Rounds any extra digits.
 11:      * Does not advance to the next line after output.
 12:      */
 13:     public static void write
 14:     (
 15:         double number,
 16:         int digitsAfterPoint
 17:     )
 18:     {
 19:         if (number >= 0)
 20:             writePositive(number, digitsAfterPoint);
 21:         else
 22:         {
 23:             double positiveNumber = -number;
 24:             System.out.print('-');
 25:             writePositive(positiveNumber, digitsAfterPoint);
 26:         }
 27:     }
 28: 
 29:     /**
 30:      * Displays a number with digitsAfterPoint digits after
 31:      * the decimal point. Rounds any extra digits.
 32:      * Advances to the next line after output.
 33:      */
 34:     public static void writeln
 35:     (
 36:         double number,
 37:         int digitsAfterPoint
 38:     )
 39:     {
 40:         write(number, digitsAfterPoint);
 41:         System.out.println();
 42:     }
 43: 
 44:     //Precondition: number >= 0
 45:     //Displays a number with digitsAfterPoint digits after 
 46:     //the decimal point. Rounds any extra digits.
 47:     private static void writePositive
 48:     (
 49:         double number,
 50:         int digitsAfterPoint
 51:     )
 52:     {
 53:         int mover = (int)(Math.pow(10, digitsAfterPoint));
 54:         //1 followed by digitsAfterPoint zeros
 55:         int allWhole; //number with the decimal point
 56:         //moved digitsAfterPoint places
 57:         allWhole = (int)(Math.round(number * mover));
 58:         int beforePoint = allWhole / mover;
 59:         int afterPoint = allWhole % mover;
 60:         System.out.print(beforePoint);
 61:         System.out.print('.');
 62:         writeFraction(afterPoint, digitsAfterPoint);
 63:     }
 64: 
 65:     //Displays the integer afterPoint with enough zeros
 66:     //in front to make it digitsAfterPoint digits long.
 67:     private static void writeFraction
 68:     (
 69:         int afterPoint,
 70:         int digitsAfterPoint
 71:     )
 72:     {
 73:         int n = 1;
 74:         while (n < digitsAfterPoint)
 75:         {
 76:             if (afterPoint < Math.pow(10, n))
 77:                 System.out.print('0');
 78:             n = n + 1;
 79:         }
 80:         System.out.print(afterPoint);
 81:     }
 82: 
 83:     public static void main(String[] args)
 84:     {
 85:         double number = 123.456789;
 86:         System.out.println("Testing with the number 123.456789:");
 87:         writeln(number, 0);
 88:         writeln(number, 1);
 89:         writeln(number, 2);
 90:         writeln(number, 3);
 91:         writeln(number, 4);
 92:         writeln(number, 5);
 93:         writeln(number, 6);
 94:         writeln(number, 7);
 95:     }
 96: }