In: Computer Science
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;
}
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...