Question SOLVED: C++ to Java

$30.00

Category:

Description

Question SOLVED: C++ to Java

C++ to Java

Convert this code to java?

//Main.cpp

#include <iostream>

#include <iostream>

#include “Station.h”
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
using namespace std;

bool randomBool() {
return rand() % 2 == 1;
}

int main()
{
Station *s1, *s2, *s3;
float r1, r2, r3, r4;
//R5 is not being used
bool carrierStatus;
for(int i=0; i<500; i++){
for(int j=0; j<3; j++){
carrierStatus = randomBool();
r1 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
s1 = new Station(r1, carrierStatus);
carrierStatus = randomBool();
r1 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
s2 = new Station(r1, carrierStatus);
carrierStatus = randomBool();
r1 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
s3 = new Station(r1, carrierStatus);
cout<<“Sending message form s1 to s2″<<endl<<endl;
if(s1->getCarrierStatus() && s2->getCarrierStatus()){
cout<<“Collision occur! Transmiss stop!”<<endl<<endl;
r4 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
usleep(r4);
}
else if(s1->getCarrierStatus()==false && s2->getCarrierStatus() == false){
r2 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
s2->setMessage(r2);
cout<<“Data has been transmited successfully!”<<endl<<endl;
}
else{
cout<<“s1 or s2 is looking busy!”<<endl<<endl;
r3 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
usleep(r3);
}

cout<<“Sending message form s2 to s3″<<endl<<endl;
if(s2->getCarrierStatus() && s3->getCarrierStatus()){
cout<<“Collision occur! Transmiss stop!”<<endl<<endl;
r4 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
usleep(r4);
}
else if(s2->getCarrierStatus()==false && s3->getCarrierStatus() == false){
r2 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
s3->setMessage(r2);
cout<<“Data has been transmited successfully!”<<endl<<endl;
}
else{
cout<<“s2 or s3 is looking busy!”<<endl<<endl;
r3 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
usleep(r3);
}

cout<<“Sending message form s3 to s1″<<endl<<endl;
if(s3->getCarrierStatus() && s1->getCarrierStatus()){
cout<<“Collision occur! Transmiss stop!”<<endl<<endl;
r4 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
usleep(r4);
}
else if(s3->getCarrierStatus()==false && s1->getCarrierStatus() == false){
r2 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
s1->setMessage(r2);
cout<<“Data has been transmited successfully!”<<endl<<endl;
}
else{
cout<<“s3 or s1 is looking busy!”<<endl<<endl;
r3 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
usleep(r3);
}

cout<<“Sending message form s3 to s2″<<endl<<endl;
if(s3->getCarrierStatus() && s2->getCarrierStatus()){
cout<<“Collision occur! Transmiss stop!”<<endl<<endl;
r4 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
usleep(r4);
}
else if(s3->getCarrierStatus()==false && s2->getCarrierStatus() == false){
r2 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
s2->setMessage(r2);
cout<<“Data has been transmited successfully!”<<endl<<endl;
}
else{
cout<<“s3 or s2 is looking busy!”<<endl<<endl;
r3 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
usleep(r3);
}
}
}
}

//Station.cpp

#include “Station.h”

Station :: Station(int message, int status){

_message = message;

_carrierStatus = status;

}

int Station::getMessage(){

return _message;

}

bool Station::getCarrierStatus(){

return _carrierStatus;

}

int Station::getReceivedMessage(){

return _receivedMessage;

}

void Station::setMessage(int message){

_message=message;

}

void Station::setCarrierStatus(bool status){

_carrierStatus = status;

}

void Station::setReceivedMessage(int message){

_receivedMessage = message;

}

//Sation.h

#include <iostream>

using namespace std;

class Station{

private:

int _message;

bool _carrierStatus;

int _receivedMessage;

public:

Station(int message, int carrierStatus);

int getMessage();

bool getCarrierStatus();

int getReceivedMessage();

void setMessage(int message);

void setCarrierStatus(bool status);

void setReceivedMessage(int receivedMessage);

};


error: Content is protected !!