import java.util.Random; /** * Class that contains static methods and constants to exercise Java * number types and functions from the Math class. * * @author Scott Russell * @version 9/25/2007 */ public class Newton { //the number of bits per byte public static final short BITS_PER_BYTE = 8; public static final double EARTH_GRAVITY = 9.8; /** * A method to compute the logarithm base 2 of the specified number. * @param value a float to compute the log of * @return the log base 2 as a float */ public static double log2(double value) { return Math.log(value)/Math.log(2); } /** * Calculates the time needed to travel a given distance in meeters at a constant * rate of acceleration in meters/second squared from an initial velocity in meters/second. * @param accel * @param initVelocity * @param distance * @return time in seconds */ public static double travelTime(double accel, double initVelocity, double distance) { //equation we are applying quadratic formula to is accel*time^2+initVelocity*time-distance double root = Math.sqrt(initVelocity*initVelocity + 4*accel*distance); return (-initVelocity + root)/(2*accel); } /** * Calculates the distance traveled at a constant acceleration. * @param accel the constant rate of acceleration in meters/second squared * @param velocity the initial velocity in meters/second * @param time the time in seconds */ public static double distanceTraveled(double accel, double initVelocity, double time) { return (accel*time+initVelocity)*time; } public static int base2To10(String binaryNumber) { //variable to store the partial result as we compute it int result = 0; //variable to store the exponent used to compute the magnitude of each digit int exponent = 0; // variable to store the magnitude of each digit int magnitude =0; // variable to hold a single digit of the given binary number int digit = 0; //read the binaray number from left to right for (int pos = 0; pos < binaryNumber.length(); pos++) { //for each digit get the digit at the specified position and convert to an integer digit=Integer.parseInt(binaryNumber.substring(pos,pos+1)); //compute the value of the exponent based on the digit's position exponent = binaryNumber.length() - 1 - pos; //compute the magnitude as a power of 2 to the computed exponent magnitude = (int) Math.pow(2,exponent); //add this digits contribution to the running total result = result + digit*magnitude; } //return the final total result return result; } //the method that will test our work public static void main(String[] args) { System.out.println("The largest and smallest numbers in the int class are " + Integer.MAX_VALUE + " and " + Integer.MIN_VALUE +"."); long intValues = (long) Integer.MAX_VALUE - (long) Integer.MIN_VALUE + 1; System.out.println("This means there are " + intValues + " different values"); short bitsStored = (short) log2(intValues); System.out.println("The int class uses " + bitsStored + " bits or " + bitsStored/BITS_PER_BYTE + " bytes to store each value."); //test the method with a fixed value String testSequence = "10011"; System.out.println("Binary string " + testSequence + " is " + base2To10(testSequence) +"?"); //generate a random binary string to test with //a constant in the method to determine how long to make the string final int NUM_DIGITS=8; //create random number generator Random randNumGen = new Random(); String randSequence = new String(); //generate some random digits for (int pos=0; pos < NUM_DIGITS; pos++) { randSequence = randSequence + String.valueOf(randNumGen.nextInt(2)); } //test the method on the random sequence System.out.println("Binary string " + randSequence + " is " + base2To10(randSequence) +"?"); } }