Testing SoundLevels.java

Output:

Enter type of sound unit: Pa or dB: Pa
Enter the sound value: 200
immediate damage
pass

Test 1

Output:

Enter type of sound unit: Pa or dB: Pa
Enter the sound value: .02
no damage
pass

Test 2

Output:

Enter type of sound unit: Pa or dB: dB
Enter the sound value: 190
immediate damage
pass

Test 3

Output:

Enter type of sound unit: Pa or dB: Db
Enter correct correct units
pass

Test 4

Output:

Enter type of sound unit: Pa or dB: dB
Enter the sound value: 0
threshold of normal hearing
pass

Test 5

Output:

Enter type of sound unit: Pa or dB: dB
Enter the sound value: 191
don't even think about it
pass

Test 6

Output:

Enter type of sound unit: Pa or dB: dB
Enter the sound value: -15
I do not have that value
pass

Test 7

Output:

Enter type of sound unit: Pa or dB: dB
Enter the sound value: 90
I do not have that value
pass

Test 8

Output:

Enter type of sound unit: Pa or dB: dB
Enter the sound value: 85
damage after 8 hours
pass

Test 9

Output:

Enter type of sound unit: Pa or dB: dB
Enter the sound value: 100
damage after 15 minutes a day
pass

Student files

SoundLevels.java:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
import java.util.Scanner;
/**
 * Takes user input of the unit of sound
 * and value. Then prints a message
 */
public class SoundLevels
{
    public static void main(String[] args)
    {
        final double ReferenceSoundPressure = 20E-6;
        
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter type of sound unit: Pa or dB: ");
        String units = scan.nextLine();
        if (units.equals("Pa") || units.equals("dB"))
        {
            System.out.print("Enter the sound value: ");
            double value = scan.nextDouble();
            
            //convert Pa to dB if necessary
            // L = 20 log10 (p / p0)
            if (units.equals("Pa"))
            {
                value = 20 * Math.log10(value / ReferenceSoundPressure);
            }
            String message = "";
            if (value == 0)
            {
                message ="threshold of normal hearing";
            }
            else if (value == 60)
            {
                message = "no damage";
            }
            else if (value == 85)
            {
                message = "damage after 8 hours";
            }
            else if (value == 100)
            {
                message = "damage after 15 minutes a day";
            }
            else if (value >= 140 && value <= 190)
            {
                message = "immediate damage";
            }
            else if (value > 190)
            {
               message = "don't even think about it";
            }
            else
            {
                message = "I do not have that value";
            }
            System.out.println(message);
        }
        else
        {
            System.out.println("Enter correct correct units");
        }
    }
}

Score

10/10

Download

2015-09-26T01:02:43Z