001 /* 002 * SpiroUtils.java 003 * Part of the Spirograph problem set 004 * 005 * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's Computers 006 * and Cognition Group. For more information, see 007 * <a href="http://www.cognition.olin.edu/projects/cs101/">the 008 * CS101 homepage</a> or email <las@olin.edu>. 009 * 010 * Copyright (C) 1998 Massachusetts Institute of Technology. 011 * Copyright (C) 2002-2003 Franklin W. Olin College of Engineering. 012 * Please do not redistribute without obtaining permission. 013 */ 014 015 package spirograph; 016 017 import java.io.*; 018 019 /** 020 * This class contained a bunch of Utilities that were specific to this 021 * problem set, but only one of them turned out to be neccesary. 022 * 023 * <p>Copyright © 1998 Massachusetts Institute of Technology<br /> 024 * Copyright © 2003 Franklin W. Olin College of Engineering 025 * 026 * @author Luis Sarmenta, lfgs@cag.lcs.mit.edu 027 * @author Henry Wong, henryw@mit.edu 028 * @author Patrick G. Heck, gus.heck@olin.edu 029 * @version $Id: SpiroUtils.java,v 1.6 2004/02/09 20:55:03 gus Exp $ 030 * 031 * @see Accelerator 032 */ 033 public abstract class SpiroUtils { 034 035 /** 036 * The reserved words for java 1.4. Altough the student's shouldn't ever 037 * use assert in this lab, I have included it for completeness. If you are 038 * using an SDK earlier than 1.4 and a student picks a variable named 039 * assert, this will cause problems, but this should be acceptably rare. 040 */ 041 public static final String[] RESERVED_WORDS = 042 { "abstract", "assert", "boolean", "break", 043 "byte", "case", "catch", "char", 044 "class", "const", "continue", "default", 045 "do", "double", "else", "extends", 046 "false", "final", "finally", "float", 047 "for", "goto", "if", "implements", 048 "import", "instanceof", "int", "interface", 049 "long", "native", "new", "null", 050 "package", "private", "protected", "public", 051 "return", "short", "static", "stictfp", 052 "super", "switch", "synchronized", "this", 053 "throw", "throws", "transient", "true", 054 "try", "void", "volatile", "while" }; 055 056 /** 057 * Check to see if a string contains a java reserved word. 058 * 059 * @param someCode The code or other string to check for reserved words 060 * @return true if a reserved word is detected, false otherwise 061 */ 062 063 public static boolean isReservedWord(String someCode) { 064 for (int i=0; i<RESERVED_WORDS.length; i++) { 065 if (someCode.equals(RESERVED_WORDS[i])) { 066 return true; 067 } 068 } 069 return false; 070 } 071 072 /** This method reads in a class file, creates an Object from that 073 * class and casts it to an Accelerator. <p> 074 * 075 * Class.forName(String) caches names, if I ask it to reread 076 * a file that the user has changed it will return the old 077 * version of that file. Therefore, I use some system calls 078 * to copy the file to a new name, read it in and erase it. <p> 079 * 080 * This method is only used to load the classes, if any, specified 081 * as command line arguments when the program is started. It is <em>not</em> 082 * used to load the classes that are compiled during the execution 083 * of the program. To do this, a cs101.util.MultiClassLoader is used. 084 * 085 * @return The loaded class if it implements Accelerator, null otherwise. 086 * @param className The string specifying the name of the class to load 087 */ 088 public static Accelerator createAccel(String className) { 089 Object temp = null; 090 Class c = null; 091 092 // read in temporary file 093 try { 094 // MultiClassLoader loader = new FileClassLoader(""); 095 //loader.monitorOn = true; 096 097 // c = loader.forceLoadClass(className); 098 099 // We do not need to use a MultiClassLoader here. Class.forName works 100 // fine, since it is the first time we are loading the class. Only 101 // use the MultiClassLoader when loading a class that has changed. 102 c = Class.forName(className); 103 System.out.println( "Loaded class " + c.getName() ); 104 105 temp = c.newInstance(); 106 107 } catch (Exception e) { 108 System.out.println (e); 109 System.out.println ("Class "+ className + " not found."); 110 return(null); 111 } 112 113 if (!(temp instanceof Accelerator)) { 114 System.out.println ("Class doesn't implement Accelerator"); 115 return(null); 116 } 117 118 return (Accelerator)temp; 119 } 120 121 /** The distance between any two points in 2D space. 122 * 123 * @param ax first x coordinate 124 * @param ay first y coordinate 125 * @param bx second x coordinate 126 * @param by second y coordinate 127 * @return the distance between the (ax,ay) and (bx,by) 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 /** Calculates whether or not a given position is inside a 134 * given ellipse. The elipse is assumed to be centered on 135 * 0,0 with major and minor axes coinciding with 136 * the x and y coordinate axes. 137 * 138 * @param x The x coordinate of the point being tested 139 * @param y The y coordinate of the point being tested 140 * @param a The major or minor axis of the elipse in the x direction 141 * @param b The major or minor axis of the elipse in the y direction 142 * @return true if the point is in the elipse false otherwise 143 */ 144 145 public static boolean inEllipse(double x, double y, double a, double b) { 146 return (Math.pow(x/a,2)+Math.pow(y/b,2) < 1); 147 } 148 149 } 150 151 /* 152 * $Log: SpiroUtils.java,v $ 153 * Revision 1.6 2004/02/09 20:55:03 gus 154 * javadoc fixes 155 * 156 * Revision 1.5 2003/01/09 21:29:31 gus 157 * renamed hasReservedWord to isReserved word and changed functionality 158 * corespondingly. Also cleaned up docs and indentation, and copyright in SpiroUtils 159 * 160 * Revision 1.4 2003/01/09 20:47:53 gus 161 * Added a method to check for java keywords, and completed the missing javadoc 162 * 163 * Revision 1.3 2003/01/09 16:50:42 gus 164 * Merging all static utilities into one class. Util.java is now just a shell 165 * 166 * Revision 1.2 2003/01/09 16:34:32 gus 167 * 168 * Unused constant removed: 169 * ------------------------ 170 * CUR 171 * 172 * Unused Methods removed: 173 * ------------------------ 174 * public static void delFile(String) 175 * public static void copyFiles(String, String) 176 * 177 */