Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.66 KB | None | 0 0
  1. /**
  2. * EasyWriter (with apologies to Dennis Hopper)
  3. * Provides a simple interface for text stream output
  4. * @author Guy J Brown
  5. * First version 16/2/2000
  6. * Modified from a suggestion of Robert Chisholm to use PrintStream instead of
  7. * PrintWriter 6 October 2015
  8. * SDN removed a legacy method 21 November 2017
  9. */
  10.  
  11. package sheffield;
  12.  
  13. import java.io.*;
  14.  
  15. public class EasyWriter extends PrintStream {
  16.  
  17.     // Constructors
  18.    
  19.     /**
  20.     * Create a new EasyWriter that writes to standard output
  21.     */
  22.     public EasyWriter() {
  23.         super(System.out,true);
  24.     }
  25.        
  26.     /**
  27.     * Create a new EasyWriter that writes to a text file
  28.     * @param s the name of the text file
  29.     */
  30.     public EasyWriter(String s) {
  31.         super(getFileWriter(s),true);
  32.     }
  33.        
  34.     // Private methods
  35.    
  36.     private static FileOutputStream getFileWriter(String s) {
  37.         FileOutputStream f = null;
  38.         try {
  39.             f = new FileOutputStream(s);
  40.         }
  41.         catch (IOException e) {
  42.             System.err.println("Error in EasyWriter - couldn't create output file");
  43.             System.exit(0);
  44.         }
  45.         return f;
  46.     }
  47.        
  48.     private double rounded(double x, int ndp) {
  49.         return Math.round(x*Math.pow(10,ndp))/Math.pow(10,ndp);
  50.     }
  51.        
  52.     private String padfield(double x, int n, int w) {
  53.         String s = padfrac(x,n);
  54.         while (s.length()<w) s=" "+s;
  55.         return s;
  56.     }
  57.  
  58.     private String padfrac(double x, int n) {
  59.         String s = Double.toString(rounded(x,n));
  60.         int ndp = n-(s.length()-s.indexOf('.')-1);
  61.         for (int i=0; i<ndp; i++) s+="0";
  62.         return s;
  63.     }
  64.            
  65.     private String padwhole(long x, int n) {
  66.         String s = Long.toString(x);
  67.         while (s.length()<n) s=" "+s;
  68.         return s;
  69.     }
  70.                        
  71.     // formatted int
  72.    
  73.     /**
  74.     * Println an int within a specified field width
  75.     * @param x the number to println
  76.     * @param n the field width
  77.     */
  78.     public void println(int x, int n) {
  79.         println(padwhole(x,n));
  80.     }
  81.        
  82.     /**
  83.     * Print an int within a specified field width
  84.     * @param x the number to print
  85.     * @param n the field width
  86.     */
  87.     public void print(int x, int n) {
  88.         print(padwhole(x,n));
  89.     }
  90.    
  91.     // formatted long
  92.    
  93.     /**
  94.     * Println a long within a specified field width
  95.     * @param x the number to println
  96.     * @param n the field width
  97.     */
  98.     public void println(long x, int n) {
  99.         println(padwhole(x,n));
  100.     }
  101.  
  102.     /**
  103.     * Print a long within a specified field width
  104.     * @param x the number to print
  105.     * @param n the field width
  106.     */     
  107.     public void print(long x, int n) {
  108.         print(padwhole(x,n));
  109.     }
  110.    
  111.     // formatted short
  112.    
  113.     /**
  114.     * Println a short within a specified field width
  115.     * @param x the number to println
  116.     * @param n the field width
  117.     */
  118.     public void println(short x, int n) {
  119.         println(padwhole(x,n));
  120.     }
  121.        
  122.     /**
  123.     * Print a short within a specified field width
  124.     * @param x the number to print
  125.     * @param n the field width
  126.     */
  127.     public void print(short x, int n) {
  128.         print(padwhole(x,n));
  129.     }
  130.    
  131.     // formatted double
  132.    
  133.     /**
  134.     * Print a double with a specified precision
  135.     * @param x the number to print
  136.     * @param n the number of decimal places
  137.     */
  138.     public void print(double x, int n) {
  139.         print(padfrac(x,n));
  140.     }
  141.  
  142.     /**
  143.     * Println a double with a specified precision
  144.     * @param x the number to println
  145.     * @param n the number of decimal places
  146.     */
  147.     public void println(double x, int n) {
  148.         println(padfrac(x,n));
  149.     }
  150.  
  151.     /**
  152.     * Print a double with a specified precision and field width
  153.     * @param x the number to print
  154.     * @param n the number of decimal places
  155.     * @param w the field width
  156.     */
  157.     public void print(double x, int n, int w) {
  158.         print(padfield(x,n,w));
  159.     }
  160.  
  161.     /**
  162.     * Println a double with a specified precision and field width
  163.     * @param x the number to println
  164.     * @param n the number of decimal places
  165.     * @param w the field width
  166.     */
  167.     public void println(double x, int n, int w) {
  168.         println(padfield(x,n,w));
  169.     }
  170.  
  171.     // formatted float
  172.  
  173.     /**
  174.     * Print a float with a specified precision
  175.     * @param x the number to print
  176.     * @param n the number of decimal places
  177.     */
  178.     public void print(float x, int n) {
  179.         print(padfrac(x,n));
  180.     }
  181.  
  182.     /**
  183.     * Println a float with a specified precision
  184.     * @param x the number to println
  185.     * @param n the number of decimal places
  186.     */
  187.     public void println(float x, int n) {
  188.         println(padfrac(x,n));
  189.     }
  190.    
  191.     /**
  192.     * Print a float with a specified precision and field width
  193.     * @param x the number to print
  194.     * @param n the number of decimal places
  195.     * @param w the field width
  196.     */
  197.     public void print(float x, int n, int w) {
  198.         print(padfield(x,n,w));
  199.     }
  200.  
  201.     /**
  202.     * Println a float with a specified precision and field width
  203.     * @param x the number to println
  204.     * @param n the number of decimal places
  205.     * @param w the field width
  206.     */
  207.     public void println(float x, int n, int w) {
  208.         println(padfield(x,n,w));
  209.     }
  210.    
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement