Han

Han Ferik

Home | Projects | AP CSA

Boiling Water

Tools: VSCode | Source Code: GitHub

This Java program is designed to calculate the atmospheric pressure based on the boiling point of water. It begins by reading an integer input, which represents the temperature at which water boils in Celsius. Using a formula, P=101.325−0.033×(T−100), the program calculates the atmospheric pressure at that boiling temperature. The formula adjusts the pressure relative to the standard boiling point of water at sea level (100°C), where the pressure is 101.325 kPa.

After computing the pressure, the program rounds it to the nearest integer using Math.round(). It then compares this rounded value to 100 kPa to determine the sea level status. If the pressure is greater than 100 kPa, the location is below sea level, and the program assigns a value of -1. If the pressure is exactly 100 kPa, the location is at sea level, represented by a value of 0. If the pressure is less than 100 kPa, it indicates that the location is above sea level, and the program assigns a value of 1.

Finally, the program outputs the rounded atmospheric pressure followed by the integer that represents the sea level status. The program also ensures that the Scanner object used for input is properly closed at the end to prevent resource leaks. This solution operates efficiently with constant time complexity, as the calculations and comparisons are straightforward.


BoilingWater Java Code

BoilingWater Java Code


    import java.util.Scanner;
    
    public class BoilingWater {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int T = scanner.nextInt();
            double P = 101.325 - 0.033 * (T - 100);
            int P_rounded = Math.round(P);
            int seaLevelStatus;
            if (P_rounded > 100) {
                seaLevelStatus = -1;
            } else if (P_rounded == 100) {
                seaLevelStatus = 0;
            } else {
                seaLevelStatus = 1;
            }
            System.out.println(P_rounded);
            System.out.println(seaLevelStatus);
            scanner.close();
        }
    }
        

Find me on the interwebs!

Github LinkedIn Instagram Facebook