Java Fundamentals Prework Practice Solution

Here is one possible solution to the Data Types and Variables Practice exercise:

public class Variables {
    public static void main(String[] args) {

        // TODO: Complete the following variable declarations.

        int yearCurrent = 2024;
        int numberOfPlanets = 8;

        int heightMountEverestMeters = 8_848;
        int averageHumanLifespanYears = 72;

        int earthDiameterKM = 12_742;
        int lightSpeedKMPerSecond = 299_792;

        String intro = "Let's explore some interesting facts about our world and beyond.";

        // TODO: Use a print statement to display the intro message.

        System.out.println(intro);

        // TODO: Complete the following mathematical calculation.

        // Calculate the time it would take for light to travel across the Earth's diameter, and print the result.

        System.out.println((double) earthDiameterKM / (double) lightSpeedKMPerSecond);

        // The formula would be: time = earthDiameterKM / lightSpeedKMPerSecond

        // TODO: Convert the following String into a number, then print it.

        String earthPopulation2011 = "7_000_000_000";

        long intPopulation2011 = Long.parseLong(earthPopulation2011.replace("_", ""));

        System.out.println(intPopulation2011);

        // TODO: Complete the following variable declarations, and replace the /*something*/'s in the String with the proper variables and print the result.

        int numberOfOceans = 5;
        int numberOfContinents = 7;
        String earthFacts = "Our planet has " + numberOfOceans + " oceans and " + numberOfContinents + " continents.";

        System.out.println(earthFacts);
    }
}