Translate below code from C++to Java #include<iostream> using namespace std; voi
ID: 2247340 • Letter: T
Question
Translate below code from C++to Java
#include<iostream>
using namespace std;
void ShowTable(char[][3]);
void ResetTable(char[][3]);
char roomA[][3] = { 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd' };
char roomB[][3] = { 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd' };
int alldirt = 0;
int allenergy = 0;
char *direction;
//---------- THIS FUNCTION SHOW THE ROOM ---------
// this function resposable for drawing the room
void ShowTable(char M[][3])
{
cout << "##############" << endl;
int i, j;
for (i = 0; i<3; i++)
{
cout << "# ";
for (j = 0; j<3; j++)
{
cout << M[i][j] << " ";
}
cout << "# " << endl << endl;
}
}
void room_disp()
{
ShowTable(roomA);
ShowTable(roomB);
}
//--------------------------------------------------
// marker array refer to either roomA or roomB
void suck_dirt(char Marker[][3], char player_move, int &dirt,int &energy)
{
for (int C = 0; C<3; C++)
{
if (C == 0 || C == 2)
{
if (C == 2){ cout << "the vacuum cleaner has moved to right" << endl; }
direction = "south";
for (int R = 0; R<3; R++)
{
if (Marker[R][C] == 'd')
{
Marker[R][C] = 'c';
energy += 2;
room_disp();
//ShowTable(Marker);
//ShowTable(Marker2);
cout << "the vacum cleaner is going " << direction << endl;
cout << "please enter [c] to clean the room" << endl;
cin >> player_move;
dirt++;
}
else
energy += 1;
}
}
else if (C == 1)
{
for (int R = 2; R >= 0; R--)
{
if (R == 2)
{
direction = "right";
}
else { direction = "north"; }
if (Marker[R][C] == 'd')
{
energy += 2;
Marker[R][C] = 'c';
room_disp();
//ShowTable(Marker);
//ShowTable(Marker2);
cout << "please enter [c] to clean the room" << endl;
cout << "the vacum cleaner has moved to " << direction << endl;
cin >> player_move;
dirt++;
}
else
energy += 1;
}
}
}
}
int main(void)
{
int averageScore;
char a='n';
int num = 0;
while (a != 'y')
{
num++;
int NumDirt = 0;
int consumedEnergy = 0;
char symbol;
cout << "Please enter the environment of room A, " << endl
<<"'c' represent clean and 'd' represent dirty." << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cin >> roomA[i][j];
}
}
cout << "Please enter the environment of room B, " << endl
<< "'c' represent clean and 'd' represent dirty." << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cin >> roomB[i][j];
}
}
ShowTable(roomA);
ShowTable(roomB);
cout << "**** Wellcome to start cleaning type [c]****" << endl;
cin >> symbol;
suck_dirt(roomA, symbol,NumDirt,consumedEnergy);
cout << endl << "*********************************" << endl;
cout << "Cleaner is now moving to room B" << endl;
cout << "*********************************" << endl;
suck_dirt(roomB, symbol,NumDirt,consumedEnergy);
cout << "The amount of dirt cleaned up: " << NumDirt << endl;
cout << "The amount of energy: " << consumedEnergy << endl;
alldirt += NumDirt;
allenergy += consumedEnergy;
cout << "Do you want to quit? Enter y or n." << endl;
cin>>a;
}
cout << "The avergy of the amount of dirt cleaned up is " << alldirt / num << endl;
cout << "The avergy of the amount of energy consumed is " << allenergy / num << endl;
}
Explanation / Answer
Here is your c++ converted code to java:
Room class:
public class Room {
Scanner input;
char roomA[][]={{'d','d','d'},{'d','d','d'},{'d','d','d'}};
char roomB[][]={{'d','d','d'},{'d','d','d'},{'d','d','d'}};
int alldirt = 0;
int allenergy = 0;
String direction;
void ShowTable(char[][] M)
{
System.out.println("##############");
int i, j;
for (i = 0; i<3; i++)
{
System.out.println("# ");
for (j = 0; j<3; j++)
{
System.out.println(M[i][j] + " ");
}
System.out.println( "# ");
}
}
void room_disp()
{
ShowTable(roomA);
ShowTable(roomB);
}
void suck_dirt(char Marker[][], char player_move, int dirt,int energy)
{
Scanner input=new Scanner(System.in);
for (int C = 0; C<3; C++)
{
if (C == 0 || C == 2)
{
if (C == 2)
{ System.out.println("the vacuum cleaner has moved to right"); }
direction = "south";
for (int R = 0; R<3; R++)
{
if (Marker[R][C] == 'd')
{
Marker[R][C] = 'c';
energy += 2;
room_disp();
//ShowTable(Marker);
//ShowTable(Marker2);
System.out.println("the vacum cleaner is going "+ direction);
System.out.println("please enter [c] to clean the room");
player_move=input.next().charAt(0);
dirt++;
}
else
energy += 1;
}
}
else if (C == 1)
{
for (int R = 2; R >= 0; R--)
{
if (R == 2)
{
direction = "right";
}
else { direction = "north"; }
if (Marker[R][C] == 'd')
{
energy += 2;
Marker[R][C] = 'c';
room_disp();
//ShowTable(Marker);
//ShowTable(Marker2);
System.out.println("please enter [c] to clean the room");
System.out.println("the vacum cleaner has moved to " + direction);
player_move=input.next().charAt(0);
dirt++;
}
else
energy += 1;
}
}
}
}
public static void main(String[] args)
{
Room ob=new Room();
Scanner input=new Scanner(System.in);
int averageScore;
char a='n';
int num = 0;
while (a != 'y')
{
num++;
int NumDirt = 0;
int consumedEnergy = 0;
char symbol;
System.out.println( "Please enter the environment of room A, ");
System.out.println("'c' represent clean and 'd' represent dirty.");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
ob.roomA[i][j]=input.next().charAt(0);
}
}
System.out.println( "Please enter the environment of room B, ");
System.out.println("'c' represent clean and 'd' represent dirty." );
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
ob.roomB[i][j]=input.next().charAt(0);
}
}
ob.ShowTable(ob.roomA);
ob.ShowTable(ob.roomB);
System.out.println( "**** Wellcome to start cleaning type [c]****");
symbol=input.next().charAt(0);
ob.suck_dirt(ob.roomA, symbol,NumDirt,consumedEnergy);
System.out.println("*********************************");
System.out.println("Cleaner is now moving to room B" );
System.out.println("*********************************");
ob.suck_dirt(ob.roomB, symbol,NumDirt,consumedEnergy);
System.out.println("The amount of dirt cleaned up: " + NumDirt);
System.out.println( "The amount of energy: " + consumedEnergy );
ob.alldirt += NumDirt;
ob.allenergy += consumedEnergy;
System.out.println("Do you want to quit? Enter y or n." );
a=input.next().charAt(0);
}
System.out.println( "The avergy of the amount of dirt cleaned up is "+ ob.alldirt / num );
System.out.println( "The avergy of the amount of energy consumed is " + ob.allenergy / num);
}
}
I hope this solves your problem
Comment if you have any problem in above code
And please give it a thumbs up if it solves your problem :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.