 Coding in Java

# A Program to Calculate the Cost of Meadowdale Dairy Farm’s Organic Brown Eggs

``````import java.util.Scanner;
public class Eggs {
public static void main(String[] args) {
int num_of_eggs,dozen,extra;
double price_of_dozen,price_of_extra,total;
System.out.println("Enter the number of eggs:");
Scanner input=new Scanner(System.in);
num_of_eggs=input.nextInt();
dozen=num_of_eggs/12;
price_of_dozen=dozen*3.25;
extra=num_of_eggs%12;
price_of_extra=extra*.45;
total=price_of_extra+price_of_dozen;
System.out.println("you ordered "+num_of_eggs+ " eggs"+" that's "+dozen+" dozens at 3.25\$ per dozen and "+extra+" loose eggs at 45.0 cents");
System.out.print("so your total price will be: "+total+" \$");

}
}
``````

## Explanation

This program, named “Eggs.java,” is designed to calculate the cost of purchasing organic brown eggs from Meadowdale Dairy Farm. The program prompts the user to enter the number of eggs they wish to order, and then calculates the total cost of the order based on Meadowdale Dairy Farm’s pricing structure.

Meadowdale Dairy Farm charges \$3.25 for a dozen eggs, and 45 cents for individual eggs that are not part of a dozen. Based on this pricing structure, the program calculates the total cost of the order by determining the number of dozen eggs ordered and the number of individual eggs that are not part of a dozen.

After calculating the total cost of the order, the program displays a full explanation of the calculation, including the number of eggs ordered, the number of dozen eggs at \$3.25 per dozen, the number of loose eggs at 45 cents each, and the total cost of the order.

For example, if the user enters an order for 27 eggs, the program would display a message such as: “You ordered 27 eggs. That’s 2 dozen at \$3.25 per dozen and 3 loose eggs at 45 cents each for a total of \$7.85.”

This program is a simple example of how to prompt a user for input, perform calculations based on that input, and display the results in a clear and informative way.