Posted By

joshnunn on 08/02/09


Tagged

beginner learning exercise


Versions (?)

Tomatoes.java


 / Published in: Java
 

A farmer has a small field that is 124 metres by 332 metres. The farmer wants to plant tomatoes in his field. It is possible to put 5 plants in every square metre. Each plant costs 95cents. Write a small program that calculates the number of plants that will go into the field and the resulting cost.

  1. public class Tomatoes {
  2.  
  3. /**
  4.   * @param args the command line arguments
  5.   */
  6. public static void main(String[] args) {
  7. int plantCost = 93;
  8. double xSide = 124.27473984;
  9. double ySide = 332.236238764;
  10. int plantsPerSquareMeter = 5;
  11. int totalPlants;
  12. int totalCost;
  13.  
  14. totalPlants = ((int) Math.floor(xSide * ySide)) * plantsPerSquareMeter;
  15. totalCost = totalPlants * plantCost;
  16.  
  17. System.out.println("You need to plant " + totalPlants + " plants to fill up a " + xSide + " by " + ySide + " field.");
  18. System.out.println("At $" + (double)plantCost/100 + " per plant the cost will be: $" + (double)totalCost/100);
  19. }
  20. }

Report this snippet  

You need to login to post a comment.