001 /*
002 * cs101 Math utilities
003 * $Id: MoreMath.java,v 1.1.1.1 2002/06/05 21:56:32 root Exp $
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 cs101.util;
014
015 /**
016 * cs101.util.MoreMath is an extension of the java.lang.Math library.
017 * <br>
018 * Copyright 1996 Massachusetts Institute of Technology
019 *
020 * @author Todd C. Parnell, tparnell@ai.mit.edu
021 * @author Lynn Andrea Stein, las@ai.mit.edu
022 *
023 */
024 public class MoreMath {
025
026 /**
027 * Generate a random int between 0 and range. Note that if range is
028 * negative, this returns a negative number.
029 *
030 * @param range desired maximum int.
031 */
032 public static int randomInt(int range)
033 {
034 return (int) Math.round(Math.random() * range);
035 }
036
037 /**
038 * Returns the square of the input. No checks are made for MaxInt overflow.
039 *
040 * @param x the number to square
041 */
042
043 public static int square(int x)
044 {
045 return(x * x);
046 }
047
048 /**
049 * Returns 1 if the input is positive, -1 if it is negative, and 0 otherwise.
050 *
051 * @param x the number to get the sign of
052 */
053
054 public static int sign(int x)
055 {
056 if(x == 0)
057 {
058 return(0);
059 }
060 else if(x > 0)
061 {
062 return(1);
063 }
064 return(-1);
065 }
066
067 /**
068 * Prevent instantiation
069 */
070 private MoreMath() {}
071 }
072
073 /*
074 * $Log: MoreMath.java,v $
075 * Revision 1.1.1.1 2002/06/05 21:56:32 root
076 * CS101 comes to Olin finally.
077 *
078 * Revision 1.5 1999/08/17 18:37:02 emarcus
079 * Added sign and square methods.
080 *
081 * Revision 1.4 1998/07/24 17:19:30 tparnell
082 * Placate new javadoc behavior
083 *
084 * Revision 1.3 1998/07/21 19:38:19 tparnell
085 * added more javadoc & private MoreMath()
086 *
087 * Revision 1.2 1998/06/03 19:47:44 tparnell
088 * added header, javadoc and logging
089 * */
090