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 legscows
= 4 legspigs
= 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Scanner; class FarmProblem { public static void main(String[] arg) { int chickens,cows,pigs,l; Scanner sc=new Scanner(System.in); System.out.println("Enter Animals count"); System.out.println("chickens:"); chickens=sc.nextInt(); System.out.println("cows:"); cows=sc.nextInt(); System.out.println("pigs:"); pigs=sc.nextInt(); l=AnimalLegs(chickens,cows,pigs); System.out.println("Total number of legs of all animals : "+l); } static int AnimalLegs(int x,int y,int z) { return (x*2+y*4+z*4); } } |
Output -1:
- Solution:
- In the main method, the int variables chicken, cows, pigs, and l are initialized.
- 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
- 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.
- The next line prints a statement to the console instructing the user to enter a count for different animals.
123456System.out.println("chickens:");chickens=sc.nextInt();System.out.println("cows:");cows=sc.nextInt();System.out.println("pigs:");pigs=sc.nextInt(); - In the following lines, the user is asked to input the number of chickens, cows, and pigs.
- 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.
- 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.
123static int AnimalLegs(int x,int y,int z) {return (x*2+y*4+z*4);} - In the next line, the method AnimalLegs is called and the variables chickens, cows, pigs are passed in as arguments.
- 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.
- The variable l 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 x denotes the number of chickens and is hence multiplied by 2 since chickens have two legs, y 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.
1 2 3 4 5 6 7 8 |
Enter Animals count chickens: 2 cows: 4 pigs: 4 Total number of legs of all animals : 36 |
- 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.
1 2 3 4 5 6 7 8 |
Enter Animals count chickens: 5 cows: 2 pigs: 8 Total number of legs of all animals : 50 |