|
| 1 | +import java.util.Scanner; |
| 2 | + |
| 3 | +public class PlantingGrapevines { |
| 4 | +/* A vineyard owner is planting several new rows of grapevines and needs to know how many grapevines to plant in each |
| 5 | + row. She has determined that after measuring the length of a future row, she can use the following formula to |
| 6 | + calculate the number of vines that will fit in the row, along with the trellis end-post assemblies that will need to |
| 7 | + be constructed at each end of the row: |
| 8 | +
|
| 9 | + V=R−2ES |
| 10 | +
|
| 11 | + The terms in the formula are: |
| 12 | + V is the number of grapevines that will fit in the row. |
| 13 | + R is the length of the row, in feet. |
| 14 | + E is the amount of space used by an end-post assembly. |
| 15 | + S is the space between vines, in feet. |
| 16 | +
|
| 17 | + Write a program that makes the calculation for the vineyard owner. The program should ask the user to input the |
| 18 | + following: |
| 19 | + - The length of the row, in feet |
| 20 | + - The amount of space used by an end-post assembly, in feet |
| 21 | + - The amount of space between the vines, in feet |
| 22 | +
|
| 23 | + Once the input data has been entered, the program should calculate and display the number of grapevines that will |
| 24 | + fit in the row.*/ |
| 25 | + |
| 26 | + private static final Scanner input = new Scanner(System.in); |
| 27 | + private double numberOfGrapevinesPerRow; |
| 28 | + // in feet: |
| 29 | + private double lengthOfRow; |
| 30 | + private double spacePerEndPost; |
| 31 | + private double spaceBetweenVines; |
| 32 | + |
| 33 | + public double calculateNumberOfGrapevines() { |
| 34 | + double V; |
| 35 | + double R = getLengthOfRow(); |
| 36 | + double E = getSpacePerEndPost(); |
| 37 | + double S = getSpaceBetweenVines(); |
| 38 | + |
| 39 | + V = R - (2 * E * S); |
| 40 | + |
| 41 | + return V; |
| 42 | + } |
| 43 | + |
| 44 | + public void setNumberOfGrapevinesPerRow(int numberOfGrapevinesPerRowInput) { |
| 45 | + numberOfGrapevinesPerRow = numberOfGrapevinesPerRowInput; |
| 46 | + } |
| 47 | + |
| 48 | + public void setLengthOfRow(double lengthOfRowInput) { |
| 49 | + lengthOfRow = lengthOfRowInput; |
| 50 | + } |
| 51 | + |
| 52 | + public void setSpacePerEndPost(double spacePerEndPostInput) { |
| 53 | + spacePerEndPost = spacePerEndPostInput; |
| 54 | + } |
| 55 | + |
| 56 | + public void setSpaceBetweenVines(double spaceBetweenVinesInput) { |
| 57 | + spaceBetweenVines = spaceBetweenVinesInput; |
| 58 | + } |
| 59 | + |
| 60 | + public double getNumberOfGrapevinesPerRow() { |
| 61 | + return numberOfGrapevinesPerRow; |
| 62 | + } |
| 63 | + |
| 64 | + public double getLengthOfRow() { |
| 65 | + return lengthOfRow; |
| 66 | + } |
| 67 | + |
| 68 | + public double getSpacePerEndPost() { |
| 69 | + return spacePerEndPost; |
| 70 | + } |
| 71 | + |
| 72 | + public double getSpaceBetweenVines() { |
| 73 | + return spaceBetweenVines; |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + public static void main(String[] args) { |
| 78 | + PlantingGrapevines plantingGrapevines = new PlantingGrapevines(); |
| 79 | + |
| 80 | + System.out.println( "Grapevines per row Calculator\n"); |
| 81 | + |
| 82 | + System.out.println( "What is the length of the row? (in ft)"); |
| 83 | + plantingGrapevines.setLengthOfRow(input.nextDouble()); |
| 84 | + System.out.println( "How much space does your end post occupy? (in ft)"); |
| 85 | + plantingGrapevines.setSpacePerEndPost(input.nextDouble()); |
| 86 | + System.out.println( "How much space is between the vines? (in ft)"); |
| 87 | + plantingGrapevines.setSpaceBetweenVines(input.nextDouble()); |
| 88 | + System.out.println( "\nYou input:"); |
| 89 | + System.out.println( "Length of row: " + plantingGrapevines.getLengthOfRow() + "ft."); |
| 90 | + System.out.println( "Space for End-post: " + plantingGrapevines.getSpacePerEndPost() + "ft."); |
| 91 | + System.out.println( "Space between vines: " + plantingGrapevines.getSpaceBetweenVines() + "ft."); |
| 92 | + System.out.println("\nYour Calculation:"); |
| 93 | + plantingGrapevines.setNumberOfGrapevinesPerRow((int) plantingGrapevines.calculateNumberOfGrapevines()); |
| 94 | + System.out.println(plantingGrapevines.getNumberOfGrapevinesPerRow() + " grapevines per row."); |
| 95 | + } |
| 96 | +} |
0 commit comments