Beginners Programs :

Java Farm Management Project Source Code | Java Programs

Java Program for Farm Management – Here is an in-depth article that expresses the Farm Management Project or the Farm Fest Java Program along with suitable examples as well as the source code with output.

In this challenge, a farmer is asking you to tell him how many legs can be counted among all his animals. The farmer breeds three species:

  • chickens = 2 legs
  • cows = 4 legs
  • pigs = 4 legs

The farmer has counted his animals and he gives you a subtotal for each species. You have to implement a function that returns the total number of legs of all the animals.

  • The problem here is to calculate the total number of legs given a specified number of animals using Java language.
  • The input here is the quantity of each animal which is defined by an input taken from the user.
  • The output is the total number of legs calculated from a given specified number of different animals based on user-defined input.
 

Output -1:

  • Solution:
  1. In the main method, the int variables chicken, cows, pigs, and l are initialized.
  2. After that, a new Scanner class object is initialized and a reference variable sc is set to represent the object. This class is used to take user input in Java. Scanner class is part of java.util package
  3. Hence the import statement, in the beginning, is stated to import the functionality of the specified class in the farm management system project source code.
  4. The next line prints a statement to the console instructing the user to enter a count for different animals.
  5. In the following lines, the user is asked to input the number of chickens, cows, and pigs.
  6. The .nextInt() method is called by the Scanner object sc for taking in an input count for each animal. This method reads an int value input by the user and stores it in the corresponding int variable.
  7. In this particular case of the e farming project source code in java, the int variables chickens, cows, and pigs each get assigned the value as defined by the user.
  8. In the next line, the method AnimalLegs is called and the variables chickens, cows, pigs are passed in as arguments.
  9. This method assigns the values of these arguments to the parameters x, y, z and calculates the total number of legs accordingly, and returns it as well.
  10. The variable is assigned the value returned by this method and the print statement in the last line of the main method prints out the total number of legs of all animals.
  • To better understand the output, we shall go over the calculations in the AnimalLegs method.
  • The parameter denotes the number of chickens and is hence multiplied by 2 since chickens have two legs, representing cows is accordingly multiplied by 4, and z representing pigs is multiplied by 4 as well.
  • In the first output example, the user assigns the value 2 to x i.e. x = 2 (parameter x takes value from the argument chickens). Similarly, y = 4 and z = 4.
  • x*2 + y*4 + z*4 = 2*2 + 4*4 + 4*4 = 4 + 16 + 16 = 36.
  • Hence, our output or answer is 36 legs in all.
Output -2:

  • As we saw in the case of the first output, here : x = 5, y = 2, z = 8.
  • x*2 + y*4 + z*4 = 5*2 + 2*4 + 8*4 = 10 + 8 + 32 = 50.
  • Hence, our answer here is 50 legs in all.