Flow control is one of the main building blocks that determines how a program should run. Ghana Cocoa Growing company wishes to compare the impart of two newly acquired fertilizers on cocoa. They will do this by applying the same quantity of the two fertilizers on two similar but different cocoa plant under the same conditions (humidity, sunlight, soil moisture etc.). They will then choose the fertilizer whose crop produces the best harvest (highest quantity per square feet). As a programmer,
c. create a method that takes four arguments i.e. (the names of the two fertilizer and their individual yields). The methods should return the name of the fertilizer with the greatest yield.
d. Using “c” above, write a program that request for two fertilizer name and their respective yields. Your program should display the fertilizer with the most yield
e. Discuss your answer in “d” above
import java.util.Scanner;
public class Main {
public static String findBetter(String fertilizerNameOne, String fertilizerNameTwo,
int yieldOne, int yieldTwo) {
return yieldOne >= yieldTwo ? fertilizerNameOne : fertilizerNameTwo;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the fertilizer name: ");
String fertilizerNameOne = in.nextLine();
System.out.print("Enter the yield: ");
int yieldOne = Integer.parseInt(in.nextLine());
System.out.print("Enter the fertilizer name: ");
String fertilizerNameTwo = in.nextLine();
System.out.print("Enter the yield: ");
int yieldTwo = Integer.parseInt(in.nextLine());
System.out.println("The better one: " + findBetter(fertilizerNameOne, fertilizerNameTwo, yieldOne, yieldTwo));
}
}
Read data -> Call the method and pass the read data to it -> Show result
Comments
Leave a comment