Fat gram calculator

The problem

Write a program that asks the user to enter the number of calories and fat grams in a food item. The program should display the percentage of the calories that come from fat. One gram of fat has 9 calories, so:

  • (Calories from fat) = (fat grams) * 9

The percentage of calories from fat can be calculated as:

  • (Calories from fat) / (total calories)

If the calories from fat are less than 30% of the total calories of the food, it should also display a message indicating the food is low in fat. Note: The number of calories from fat cannot be greater than the total number of calories. If the program determines that the number of calories from fat is greater than the number calories in the food item, it should display an error message indicating that the input is invalid.

Breaking it down

public static void main(String[] args) {

    // Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);

    // Ask user for number of calories.
    System.out.print("How many calories does the food have? ");
    double calories = keyboard.nextDouble();

    // Get the number of fat grams.
    System.out.print("How many fat grams does the food have? ");
    double fatGrams = keyboard.nextDouble();

    // close scanner
    keyboard.close();

    // Calculate calories from fat.
    double fatCalories = calculateFatCalories(fatGrams);

    // validation check
    if (validateData (fatCalories, calories) ) {
        System.out.println("Invalid data.");
    } else {

        // Calculate percentage of calories from fat.
        double fatPercentage = calculateFatPercentatge(fatCalories, calories);

        // Display the results.
        System.out.println("The percentage of calories from fat is "
                + (fatPercentage * 100) + "%.");

        if (fatPercentage < 0.3) {
            System.out.println("The food is low in fat.");
        } else {
            System.out.println("The food is NOT low in fat.");
        }
    }
}

/**
 * Method should calculate fat calories based on
 * fat grams
 *
 * @param fatGrams
 * @return
 */
static double calculateFatCalories (double fatGrams) {
    return fatGrams * 9;
}

/**
 * Method should calculate fat percentage.
 *
 * @param fatCalories
 * @param calories
 * @return
 */
static double calculateFatPercentatge(double fatCalories, double calories) {
    return fatCalories / calories;
}

/**
 * @param fatCalories
 * @param calories
 * @return
 */
static boolean validateData (double fatCalories, double calories) {
    return fatCalories > calories;
}

Output

How many calories does the food have? 250
How many fat grams does the food have? 10
The percentage of calories from fat is 36.0%.
The food is NOT low in fat.

Unit tests

@Test
public void test_calculateFatCalories() {

    double val = FatGramCalculator.calculateFatCalories(100);

    assertEquals(900, val, 0);
}

@Test
public void test_calculateFatPercentatge() {

    double val = FatGramCalculator.calculateFatPercentatge(100, 100);

    assertEquals(1, val, 0);
}

@Test
public void test_validateData() {

    boolean val = FatGramCalculator.validateData(100, 100);

    assertFalse(val);
}