Question

In: Computer Science

Plz convert this C++ code into JAVA code thanks #include<iostream> using namespace std; //function for calculating...

Plz convert this C++ code into JAVA code thanks

#include<iostream>

using namespace std;

//function for calculating the average sightings from the Total Sightings array
float calcAverage(float totalSightings[],int n)
{
   int i;
   float sum=0.0;
   for(i=0;i<n;i++)
   sum=sum+totalSightings[i];
   return sum/n;
}

int main()
{
   // n is no. of bird watchers
   //flag , flag2 and flag3 are for validating using while loops
   int n,i,flag,flag2,flag3;
  
   //ch also helps in validating
   char ch;
   cout<<"------------------Welcome to the Prince William County Bird Watching weekend tracking system---------------------";
   while(1)
   {
       cout<<"\nPlease enter the number of bird watchers:\n";
       cin>>n;
       if(n==0)
       {
           flag=1;
           break;
       }
       else if(n>0 and n<100)
       {
           flag=2;
           break;
       }
       else
       {
           /*
           here I have added ch as an extra variable cause you don't tell in the question that what happen if user enters value equal to
           greater than 100 , you have only told for between 0 and 100 and for 0 also,thats why i added this
           */
           cout<<"\nInvalid input, want to enter again , if yes press 'y' else 'n' for no : ";
           cin>>ch;
           if(ch=='y')
           continue;
           else
           {
               flag=1;
               break;
           }
       }
   }
  
   //prints the goodbye message
   if(flag==1)
   {
       cout<<"\nGoodbye !!";
   }
  
   //if user enters valid n , then :-
   else if(flag==2)
   {
      
       char BirdWatcherName[n][30]; // this char array is for entering the names of the bird watchers
       float SightingSaturday[n],SightingSunday[n],totalSightings[n]; //these functions are for sightings on saturday,sunday and total sightings respectively.
      
      
       for(i=0;i<n;i++)
       {
           cout<<"\n-------------------------Bird Watcher"<<i+1<<" -------------------------";
           cout<<"\nPlease enter the bird watcher’s Name:\n";
          
           //clearing buffer
           cin.clear(); cin.sync();
           cin.getline(BirdWatcherName[i],30);
          
           ch='y';
          
           //this while loop is for validating that the sightings quantity is between 0 and 250 or not .And flag2 helps in checking this.
           while(ch!='n')
           {
               cout<<"\nPlease enter the birds sighted on Saturday:\n";
               cin>>SightingSaturday[i];
               if(SightingSaturday[i]<0 || SightingSaturday[i]>250)
               {
                   cout<<"\nInvalid input , wanna enter again? , press 'y' for yes or 'n' for no :- ";
                   cin>>ch;
                   if(ch=='y')
                   {
                       continue;
                   }
                   else
                   {
                       flag2=2;
                       break;
                   }
               }
               else
               {
                   flag2=1;
                   break;
               }
           }
           if(flag2==2)
           {
               flag3=2;
               break;
           }
           else
           {
               ch='y';
   //this while loop is again for validating that the sightings quantity is between 0 and 250 or not .And flag2 helps in checking this.
               while(ch!='n')
               {
                   cout<<"\nPlease enter the birds sighted on Sunday:\n";
                   cin>>SightingSunday[i];
                   if(SightingSunday[i]<0 || SightingSunday[i]>250)
                   {
                       cout<<"\nInvalid input , wanna enter again? , press 'y' for yes or 'n' for no :- ";
                       cin>>ch;
                       if(ch=='y')
                       {
                           continue;
                       }
                       else
                       {
                           flag3=2;
                           break;
                       }
                   }
                   else
                   {
                       flag3=1;
                       break;
                   }
               }
           }
           if(flag3==2)
           {
               break;
           }
       }
      
       if(flag3==2)
       {
           cout<<"\nGoodbye";
       }
       else
       {
           cout<<"\n------------------------------------------------PWC Birdwatching Statistics---------------------------------------------\n";
           cout<<"\nBird Watcher SaturdaySighings Sunday Sightings Total Sightings";
           for(i=0;i<n;i++)
           {
               totalSightings[i]=SightingSaturday[i]+SightingSunday[i];
           }
          
           for(i=0;i<n;i++)
           {
               cout<<endl<<BirdWatcherName[i]<<" "<<SightingSaturday[i]<<" "<<SightingSunday[i]<<" "<<totalSightings[i];
           }
          
           //calling for the calcAverage() function for calculating the average of total sightings
           cout<<"\nAverage Sightings "<<calcAverage(totalSightings,n)<<endl;
          
           //this part is for finding maximum total sighting and its index
           int index;
           float maximum;
           maximum=totalSightings[0];
           for(i=0;i<n;i++)
           {
               if(totalSightings[i]>maximum)
               {
                   maximum=totalSightings[i];
                   index=i;
               }
           }
          
           cout<<"\nThe Bird Watcher with the most sightings of "<<totalSightings[index]<<" is "<<BirdWatcherName[index];
          
           //program over
           cout<<"\n----------------------------------------------Thanks For Using Our Program---------------------------------";
       }
      
   }
   return 0;
  
}

Solutions

Expert Solution

please check out the converted code and the screen sort of the output given below... please let me know for any doubt.... thank you...

code...

import java.util.*;
public class BirdWatching
{
        public static double calcAverage(double totalSightings[],int n)
        {
                int i;
                double sum=0.0;
                for(i=0;i<n;i++)
                        sum=sum+totalSightings[i];
                return sum/n;
        }
        public static void main(String args[])
        {
                Scanner inp = new Scanner(System.in);  //creating object of scanner class
                // n is no. of bird watchers
                //flag , flag2 and flag3 are for validating using while loops
                int n,i,flag,flag2=2,flag3=2;
                //ch also helps in validating
                char ch;
                System.out.println("------------------Welcome to the Prince William County Bird Watching weekend tracking system---------------------");
                while(true)
                {
                        System.out.print("Please enter the number of bird watchers: ");
                        n=inp.nextInt();
                        if(n==0)
                        {
                                flag=1;
                                break;
                        }
                        else if(n>0 && n<100)
                        {
                                flag=2;
                                break;
                        }
                        else
                        {
                                System.out.print("invalid input, want to enter again , if yes press 'y' else 'n' for no : ");
                                ch=inp.next().charAt(0);
                                if(ch=='y')
                                        continue;
                                else
                                {
                                        flag=1;
                                        break;
                                }
                        }//end of else line 36
                }//end of while line 22
                if(flag==1)
                        System.out.println("Goodbye");
                else if(flag==2)
                {
                        String[] BirdWatcherName = new String[n];
                        double []SightingSaturday = new double[n];
                        double []SightingSunday = new double[n];
                        double []totalSightings = new double[n];
                        for(i=0;i<n;i++)
                        {
                                System.out.println("-------------------------Bird Watcher "+(i+1)+"-------------------------");
                                System.out.print("Please enter the bird watcher’s Name: ");
                                BirdWatcherName[i]=inp.nextLine();   //this statement required 2 times for clearing the buffer
                                BirdWatcherName[i]=inp.nextLine();
                                ch='y';
                                //this while loop is for validating that the sightings quantity is between 0 and 250 or not .And flag2 helps in checking this.
                                while(ch!='n')
                                {
                                        System.out.print("Please enter the birds sighted on Saturday: ");
                                        SightingSaturday[i]=inp.nextDouble();
                                        if(SightingSaturday[i]<0 || SightingSaturday[i]>250)
                                        {
                                                System.out.print("nvalid input , wanna enter again? , press 'y' for yes or 'n' for no :- ");
                                                ch=inp.next().charAt(0);
                                                if(ch=='y')
                                                        continue;
                                                else
                                                {
                                                        flag2=2;
                                                        break;
                                                }
                                        }//end of if line 69
                                        else
                                        {
                                                flag2=1;
                                                break;
                                        }
                                }//end of while line 65
                                if(flag2==2)
                                {
                                        flag3=2;
                                        break;
                                }
                                else
                                {
                                        ch='y';
                                        //this while loop is again for validating that the sightings quantity is between 0 and 250 or not .And flag2 helps in checking this.
                                        while(ch!='n')
                                        {
                                                System.out.print("Please enter the birds sighted on Sunday: ");
                                                SightingSunday[i]=inp.nextDouble();
                                                if(SightingSunday[i]<0 || SightingSunday[i]>250)
                                                {
                                                        System.out.print("nvalid input , wanna enter again? , press 'y' for yes or 'n' for no :- ");
                                                        ch=inp.next().charAt(0);
                                                        if(ch=='y')
                                                                continue;
                                                        else
                                                        {
                                                                flag3=2;
                                                                break;
                                                        }
                                                }//end of if line 100
                                                else
                                                {
                                                        flag3=1;
                                                        break;
                                                }
                                        }//end of while line 96
                                }//end of else line 92
                                if(flag3==2)
                                {
                                        flag3=2;
                                        break;
                                }
                        }//end of for loop line 57
                        if(flag3==2)
                                System.out.println("GoodBye");
                        else
                        {
                                System.out.println("------------------------------------------------PWC Birdwatching Statistics---------------------------------------------");
                                System.out.println("Bird Watcher Saturday Sighings Sunday Sightings Total Sightings");
                                for(i=0;i<n;i++)
                                        totalSightings[i]=SightingSaturday[i]+SightingSunday[i];
                                for(i=0;i<n;i++)
                                        System.out.println(BirdWatcherName[i] +"  "+ SightingSaturday[i] + "  " + SightingSunday[i] +"  "+ totalSightings[i]);
                                System.out.println("\nAverage Sightings " + calcAverage(totalSightings,n) );
                                //this part is for finding maximum total sighting and its index
                                int index=0;
                                double maximum;
                                maximum=totalSightings[0];
                                for(i=0;i<n;i++)
                                {
                                        if(totalSightings[i]>maximum)
                                        {
                                                maximum=totalSightings[i];
                                                index=i;
                                        }
                                }//end of for line 140
                                System.out.println("The Bird Watcher with the most sightings of "+ totalSightings[index] +" is "+BirdWatcherName[index]);
                                System.out.println("----------------------------------------------Thanks For Using Our Program---------------------------------");
                        } //end of else line 127
                }//end of else if line 51
        
        }//end of main line 13
}//end of class line 3
                                

output...


Related Solutions

C++ I took 7/20 =( code: #include <iostream> #include<string.h> using namespace std; // function to calculate...
C++ I took 7/20 =( code: #include <iostream> #include<string.h> using namespace std; // function to calculate number non white space characters int GetNumOfNonWSCharacters(string str) { int i = 0; int count = 0; while(str[i] != '\0') { if(str[i] != ' ') { count += 1; } i++; } return count; } // function to calculate numbers of words int GetNumOfWords(string str) { int i = 0; int count = 1; while(str[i] != '\0') { if(str[i] == ' ' && str[i-1]...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function name: main Purpose:                   main function In parameters: b,r,i Out paramters: trun,error,total,value Version:                   1.0 Author: ********************************************************************************/ void main() {    int i;//declaring this variable to get value for quitting or calaculating series    do {//do while loop to calaculate series until user quits        cout << "Enter 1 to evaluate the series." << endl;       ...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell {...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell { int val; Cell *next; }; int main() { int MAX = 10; Cell *c = NULL; Cell *HEAD = NULL; srand (time(NULL)); for (int i=0; i<MAX; i++) { // Use dynamic memory allocation to create a new Cell then initialize the // cell value (val) to rand(). Set the next pointer to the HEAD and // then update HEAD. } print_cells(HEAD); }
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; //...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; // Structure for storing the process data struct procData { char pname[5]; // Name of a process int arrivt; //Arrival time of a process int pburst; // Burst time of a process int endtime; // Exit time/ Leaving time of a process int remburst; // Remaining burst time of a process int readyFlag; // boolean, Flag for maintaining the process status }; // Global variable...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat = 'y'; for (;repeat == 'y';){ char emplyeename[35]; float basic_Salary,EPF, Dearness_Allow, tax, Net_Salary , emplyee_id; cout << "Enter Basic Salary : "; cin >> basic_Salary; Dearness_Allow = 0.40 * basic_Salary; switch (01) {case 1: if (basic_Salary <= 2,20,00) EPF = 0; case 2: if (basic_Salary > 28000 && basic_Salary <= 60000) EPF = 0.08*basic_Salary; case 3: if (basic_Salary > 60000 && basic_Salary <= 200000)...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std;...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std; void leapYear(int x); int main() { int x; cout << "Enter a year: "; cin >> x; leapYear (x);   return 0; } void leapYear(int x ) {    if (x % 400 == 0)    {    cout << "This is a leap Year";}    else if    ((x % 4 == 0) && (x % 100 != 0))    {    cout <<...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
Complete the code provided to add the appropriate amount to totalDeposit. #include <iostream> using namespace std;...
Complete the code provided to add the appropriate amount to totalDeposit. #include <iostream> using namespace std; int main() { enum AcceptedCoins {ADD_QUARTER, ADD_DIME, ADD_NICKEL, ADD_UNKNOWN}; AcceptedCoins amountDeposited = ADD_UNKNOWN; int totalDeposit = 0; int usrInput = 0; cout << "Add coin: 0 (add 25), 1 (add 10), 2 (add 5). "; cin >> usrInput; if (usrInput == ADD_QUARTER) { totalDeposit = totalDeposit + 25; } /* Your solution goes here */ else { cout << "Invalid coin selection." << endl;...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std;...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std; float series(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum += r[i];    return sum; } float parallel(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum = sum + (1 / r[i]);...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must be very basic. Please don't use math sqrt. Assume that the user does not input anything less than 0. For example: the integer square root of 16 is 4 because 4 squared is 16. The integer square root of 18 is 5 because 4 squared is 16 and 5 squared is 25, so 18 is bigger than 16 but less than 25.  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT