/** SoundLevel.java computes the loudness of a sound at one distance,
 *   given its loudness at a different distance.
 *   @author Joel Adams
 */

import java.util.Scanner;

public class SoundPressureLevel {
  public static void main(String[] args) {
    System.out.println("To find the SPL of a sound at a distance:");
    Scanner kbd = new Scanner( System.in );
    // read loudness1, distance1, distance2
    System.out.print("- what is the reference loudness (decibels)? ");
    int loudness1 = kbd.nextInt();
    System.out.print("- what is the reference distance (meters)? ");
    double distance1 = kbd.nextDouble();
    System.out.print("- what is the new distance? ");
    double distance2 = kbd.nextDouble();
    // compute loudness2
    long loudness2 = splChange(loudness1, distance1, distance2); 
    // display result
    System.out.println("\nThe SPL of the sound at distance "
    		           + distance2 + " is " + loudness2 + " db");
  }
  
  /** Compute change in sound pressure level with change in distance.
   *  @param spl1, an int; the reference SPL in decibels
   *  @param dist1, a double; the distance at which spl1 was measured
   *  @param dist2, a double; the distance whose SPL we want to know
   *  @return the SPL at dist2
   */
  public static long splChange(int spl1, double dist1, double dist2) {
	    return spl1 - Math.round( 20.0 * Math.log10(dist2/dist1) );
  }
}