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

Convert to JAVA #include <iostream> using namespace std; //function prototypes v

ID: 3722063 • Letter: C

Question

Convert to JAVA

#include <iostream>

using namespace std;

//function prototypes

void displayMonthly(double []);

void displayTotal(double []);

int main()

{                  

       //declare variable and array

                    int choice = 0;

                    double rainfall[12] = {0.0};

    

    for (int x = 0; x < 12; x += 1)

    {     //get rainfall amounts

                    cout << "Enter rainfall for month " << x + 1 << ": ";

                    cin >> rainfall[x];

     } //end for

   do

    {     //display menu and get menu choice

                    cout << endl;

                    cout << "1 Display monthly amounts" << endl;

                    cout << "2 Display total amount" << endl;

                    cout << "3 End program" << endl;

                    cout << "Enter your choice: ";

                    cin >> choice;

                              

        //call appropriate function or end program

                    if (choice == 1)

                                         displayMonthly(rainfall);

                    else if (choice == 2)

                                         displayTotal(rainfall);

                    //end ifs

    } while (choice == 1 || choice == 2);

    return 0;

}   //end of main function

//*****function definitions*****

void displayMonthly(double rain[])

{   

                    cout << "Monthly rainfall amounts:" << endl;

                    for (int x = 0; x < 12; x += 1)

                                         cout << rain[x] << endl;

                    //end for

} //end of displayMonthly function

void displayTotal(double rain[])

{

                    double total = 0.0;                           

                    for (int x = 0; x < 12; x += 1)

                                         total = total + rain[x];

                    //end for

                    cout << "Total rainfall: " << total << endl;

} //end of displayTotal function

Explanation / Answer

import java.util.Scanner;//reads data from input stream
class DisplayAmount
{
public static void main(String[] args)
{
//declare variable and array
int choice = 0;
double rainfall[]=new double[12];
Scanner input=new Scanner(System.in);
for(int x=0;x<12;++x)
rainfall[x]=0.0;
for (int x = 0; x < 12; x += 1)
{ //get rainfall amounts
System.out.print("Enter rainfall for month "+ x + 1+": ");
rainfall[x]=input.nextDouble();
} //end for

do
{ //display menu and get menu choice
System.out.println();
System.out.println("1 Display monthly amounts");
System.out.println("2 Display total amount" );
System.out.println( "3 End program");
System.out.print( "Enter your choice: ");
choice=input.nextInt();
  
//call appropriate function or end program
if (choice == 1)
displayMonthly(rainfall);
else if (choice == 2)
displayTotal(rainfall);
//end ifs
} while (choice == 1 || choice == 2);
//end of main function
}
//*****function definitions*****
public static void displayMonthly(double rain[])
{   
System.out.println( "Monthly rainfall amounts:");
for (int x = 0; x < 12; x += 1)
System.out.println(rain[x]);
//end for
} //end of displayMonthly function

public static void displayTotal(double rain[])
{
double total = 0.0;   
for (int x = 0; x < 12; x += 1)
total = total + rain[x];
//end for
System.out.println("Total rainfall: "+ total);
} //end of displayTotal function
}