Title: tools
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

tools

MyInput.java


package tools;
import java.io.*;

public class MyInput {
// If a file is not passed in, we will open up a Buffered Reader
// created from System.in

   InputStreamReader instream = new InputStreamReader(System.in);
   BufferedReader in = new BufferedReader(instream);
   String fileName=null;


public MyInput()  {
   }

public MyInput(String f)
   {
     fileName = f;
     if (!open(fileName))
         System.out.println("Defaulting to reading from keyboard");
   }
public boolean open(String file)
   {
       try {
               if (file.length() > 0)
                   {
                       FileReader fr = new FileReader(file);
                       in =    new BufferedReader(fr);
                   }
               return true;
       }
       catch (FileNotFoundException e) {
           System.out.println("File Not Found Exception in get");
           return false;
       }


   }

public void close()
{
   try {
       in.close();
       }
   catch (IOException e){
       System.out.println("IO Exception on close call");
       }
}


public String get()
   {
       try {
               return in.readLine();
       }
       catch (FileNotFoundException e) {
           System.out.println("File Not Found Exception in get");
           return null;
       }
       catch(IOException e) {
           System.out.println("IO Exception in get");
           return null;
       }
   }



public double getDouble()
{
    String str = get();
    if (str != null)
    {
       try {
            return Double.parseDouble(str);
            }
       catch (NumberFormatException e) { /* Do Nothing */}
    }
    System.out.println("Bad double input");
    return -1.0;
}

public float getFloat()
{
    String str = get();
    if (str != null)
    {
       try {
            return Float.parseFloat(str);
            }
       catch (NumberFormatException e) { /* Do Nothing */}
    }
    System.out.println("Bad float input");
    return -1.0F;

}

public long getLong()
{
    String str = get();
    if (str != null)
    {
       try {
            return Long.parseLong(str);
            }
       catch (NumberFormatException e) { /* Do Nothing */}
    }
    System.out.println("Bad long input");
    return -1;
}

public int getInt()
{
    String str = get();
    if (str != null)
    {
       try {
            return Integer.parseInt(str);
            }
       catch (NumberFormatException e) { /* Do Nothing */}
    }
    System.out.println("Bad int input");
    return -1;

}

public short getShort()
{
    String str = get();
    if (str != null)
    {
       try {
            return Short.parseShort(str);
            }
       catch (NumberFormatException e) { /* Do Nothing */}
    }
    System.out.println("Bad short input");
    return -1;

}

public byte getByte()
{
    String str = get();
    if (str != null)
    {
       try {
            return Byte.parseByte(str);
            }
       catch (NumberFormatException e) { /* Do Nothing */}
    }
    System.out.println("Bad byte input");
    return -1;
}



public static void main(String args[])  {
       MyInput input;
       String str;

       if (args.length > 0)
           input = new MyInput(args[0]);
       else
           input = new MyInput();

       do {
           str = input.get();
           if (str != null)
           {
               System.out.println(str);
                   if (str.equals("####"))
                       break;
           }
       } while (str != null);

       input.close();

   }
}






P.java


//P.java
package tools;

public class P {
  public static void print(String s) {
    System.out.print(s);
  }
  public static void print(char c) {
    System.out.print(c);
  }
  public static void print(int i) {
    System.out.print(i);
  }
  public static void print(long l) {
    System.out.print(l);
  }
  public static void print(float f) {
    System.out.print(f);
  }
  public static void print(double d) {
    System.out.print(d);
  }
  public static void print(boolean b) {
    System.out.print(b);
  }
  public static void print(char[] s) {
    System.out.print(s);
  }
  public static void print(Object obj) {
    System.out.print(obj);
  }
  public static void bitPrint(long lValue) {
    bitprintGuts(lValue, 64);
  }
  public static void bitPrint(int iValue) {
    bitprintGuts(iValue, 32);
  }
  public static void bitPrint(short sValue) {
    bitprintGuts(sValue, 16);
  }
  public static void bitPrint(byte bValue) {
    bitprintGuts(bValue, 8);
  }
  public static void bitPrint(char cValue) {
    bitprintGuts(cValue, 16);
  }

/* ************************************* */
   public static void println(String s) {
    System.out.println(s);
  }
  public static void println(char c) {
    System.out.println(c);
  }
  public static void println(int i) {
    System.out.println(i);
  }
  public static void println(long l) {
    System.out.println(l);
  }
  public static void println(float f) {
    System.out.println(f);
  }
  public static void println(double d) {
    System.out.println(d);
  }
  public static void println(boolean b) {
    System.out.println(b);
  }
   public static void println() {
    System.out.println();
  }
  public static void println(Object obj) {
    System.out.println(obj);
  }
  public static void println(char[] s) {
    System.out.println(s);
  }


 public static void bitPrintln(long lValue) {
    bitPrint(lValue); System.out.println(" ");
  }
  public static void bitPrintln(int iValue) {
     bitPrint(iValue); System.out.println(" ");
  }
  public static void bitPrintln(short sValue) {
     bitPrint(sValue); System.out.println(" ");
  }
  public static void bitPrintln(byte bValue) {
     bitPrint(bValue); System.out.println(" ");
  }
  public static void bitPrintln(char cValue) {
     bitPrint(cValue); System.out.println(" ");
  }



  static void bitprintGuts(long lValue, int count)
  {
      for (int i=count-1; i >= 0; i--)
      {
        if (  ((1L << i) & lValue) != 0)
            System.out.print("1");
          else
            System.out.print("0");
        if ((i % 4) == 0) System.out.print(" ");
      }
  }
}




Last Updated: September 2, 2002 4:27 PM