001 /* 002 * Utils.java 003 * part of the Spirograph problem set 004 * 005 * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group. 006 * For more information, see <a href="http://www.ai.mit.edu/projects/cs101/">the 007 * CS101 homepage</a> or email <las@ai.mit.edu>. 008 * 009 * Copyright (C) 1996 Massachusetts Institute of Technology. 010 * Please do not redistribute without obtaining permission. 011 */ 012 013 package spirograph; 014 015 /** 016 * This class has some all purpose utilities that will probably find their 017 * way into cs101.util. I didn't really hunt through cs101.util before I 018 * wrote these, so better versions of these may already exist somewhere. <p> 019 * 020 * Copyright (c) 1998 Massachusetts Institute of Technology 021 * 022 * @author Luis Sarmenta, lfgs@cag.lcs.mit.edu 023 * @author Henry Wong, henryw@mit.edu 024 * @version $Id: Util.java,v 1.1.1.1 2002/06/05 21:56:35 root Exp $ 025 * 026 */ 027 028 import java.io.*; 029 030 public abstract class Util { 031 032 // Exit status of successful compilation 033 public static final int SUCCESS = 0; 034 035 // System command to compile a program 036 public static final String JAVAC = "javac"; 037 038 /** 039 * This method reads the specified file off of the current directory 040 * and returns it as a String. 041 */ 042 043 public static String readFile(String filename) { 044 File theFile = new File(filename); 045 FileInputStream myFIS = null; 046 String content = ""; 047 BufferedReader br; 048 boolean moreFile = true; 049 050 if (theFile.exists() && theFile.canRead()) { 051 System.out.println ("Opening " + filename + " file."); 052 try { 053 myFIS = new FileInputStream(theFile); 054 } catch (FileNotFoundException e) { 055 System.out.println ("Error, file not found."); 056 return ""; 057 } 058 059 br = new BufferedReader(new InputStreamReader(myFIS)); 060 061 // Keep reading the file until hit an EOF, but don't add the 062 // EOF to the String. 063 while (moreFile) { 064 try { 065 String temp = br.readLine(); 066 if (temp == null) { 067 moreFile = false; 068 } else { 069 content = content + temp + "\n"; 070 } 071 } catch (IOException e) { 072 System.out.println ("Error reading."); 073 } 074 } 075 return (content); 076 } 077 return ""; 078 } 079 080 /** 081 * This routine takes a String and writes it to a file in the current 082 * directory. If the file already exists it is overwritten. 083 */ 084 085 public static void writeFile (String filename, String message) { 086 File myFile = new File(filename); 087 FileOutputStream myFOS = null; 088 089 try { 090 myFOS = new FileOutputStream(myFile); 091 } catch (IOException e) { 092 System.out.println ("Error creating FileOutputStream"); 093 return; 094 } 095 096 if (!myFile.exists()) { 097 System.out.println ("Creating new " + filename + " file."); 098 } else if (myFile.isFile() && 099 myFile.canWrite()) { 100 System.out.println ("Editing existing " + filename + " file."); 101 } else { 102 System.out.println ("Error. Can't write to " + filename); 103 try { 104 myFOS.close(); 105 } catch (IOException e) {} 106 return; 107 } 108 109 try { 110 myFOS.write(message.getBytes()); 111 } catch (IOException e) { 112 System.out.println ("Error writing to file."); 113 } 114 try { 115 myFOS.close(); 116 } catch (IOException ioe) {} 117 return; 118 } 119 120 /** 121 * This routine takes a string and compiles that file in the current 122 * directory. If there are any error then the method will return a 123 * non-zero exit status and the errors will be piped to the screen. 124 */ 125 126 /** 127 * This method calculates the distance between any two points in 2D space 128 */ 129 public static double dist(double ax, double ay, double bx, double by) { 130 return Math.sqrt(Math.pow(ax-bx,2)+Math.pow(ay-by,2)); 131 } 132 133 /** 134 * This method calculate whether or not a given position is inside a 135 * given ellipse. 136 */ 137 138 public static boolean inEllipse(double x, double y, double a, double b) { 139 return (Math.pow(x/a,2)+Math.pow(y/b,2) < 1); 140 } 141 142 }