Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program to track the growth of a plant. You are given two arrays th

ID: 3753104 • Letter: W

Question

Write a Java program to track the growth of a plant.

You are given two arrays that store the average temperature and rainfall in San Francisco, for months 0 to 11, representing January to December respectively.

final int NUMMONTHS = 12;

int [] avgTemp = {46, 48, 49, 50, 51, 53, 54, 55, 56, 55, 51, 47};

int [] avgRain = {5, 3, 3, 1, 1, 0, 0, 0, 0, 1, 3, 4};

You are starting a new plant in month 0. You are given the declaration for an array newGrowth[]; newGrowth[i] will keep track of the number of inches of growth in month i.

int [] newGrowth;

newGrowth = new int[NUMMONTHS];

What your program does:

            Prompt the user to enter the minimum and maximum temperature for the plant

            Prompt the user to enter the minimum rainfall for the plant

            (all temperatures and rainfall are integers, for simplicity)

            for each month i from 0 to 11, calculate newGrowth[i]:

           

if the average temperature for month i is < the minimum or > the maximum, newGrowth[i] is set to -1 (plant dies back);

otherwise, newGrowth[i] is set to the average rainfall for month i minus the minimum rainfall for the plant

The plant starts at height = 0 inches. For each month, calculate the height of the plant at the end of the month, and print a table as shown in the sample runs, displaying the month, the average temperature, the average rainfall, the new growth, and the height of the plant at the end of that month. (Align columns of the table using tabs, as in earlier projects.) Note that the height of the plant never becomes negative; the smallest height attained is always zero.

Explanation / Answer

Answer:

import java.util.Scanner;

class plant

{

public static void main(String[] args)

{

final int NUMMONTHS = 12;

int [] avgTemp = {46, 48, 49, 50, 51, 53, 54, 55, 56, 55, 51, 47};

int [] avgRain = {5, 3, 3, 1, 1, 0, 0, 0, 0, 1, 3, 4};

int [] newGrowth;

int flag=0,month=0,i;

Scanner sc=new Scanner(System.in);

newGrowth = new int[NUMMONTHS];

for (i=0;i<NUMMONTHS;i++ ){

System.out.println("Enter the minimum and maximum temperature for the plant");

int min=sc.nextInt();

int max=sc.nextInt();

System.out.println("Enter the average rainfall for the plant");

int ar=sc.nextInt();

if (avgTemp[i]<min&&avgTemp[i]>max)

newGrowth[i]=-1;

else

newGrowth[i]=avgRain[i]-ar;

}

for (i=0;i<NUMMONTHS;i++ ){

if(newGrowth[i]<0){

flag=1;

month=i;

}

}

if (flag==1){

System.out.println("Plant dies in the month " +month);

}

else{

System.out.print("Month Growth ");

for (i=0;i<NUMMONTHS;i++ )

System.out.println(i+" "+newGrowth[i]);

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote