Question

I have C++ code. How I can change to Java code #include <iostream> using namespace std;...

I have C++ code. How I can change to Java code

#include <iostream>

using namespace std;

int initialNumber[4];

int main()

{

  

void ShowResult();

  

  

//initialization

  

int NuberOfpage[20]={1,0,7,1,0,2,1,2,3,0,3,2,4,0,3,0,2,1,0,7},i,j,f12[3];

  

int num1=0,num2=0,countr=0,framsize=3;

  

int inital,current,current1;

  

for(i=0;i<3;i++)

{

initialNumber[i]=-1;

}

for(j=0;j<20;j++)

{

num1=0,num2=0;

  

for(i=0;i<3;i++)

{

  

if(initialNumber[i]==NuberOfpage[j])

{

  

num1=1;

  

num2=1;

  

break;

}

}

  

if(num1==0)

  

{

for(i=0;i<3;i++)

  

{

if(initialNumber[i]==-1)

  

{

  

initialNumber[i]=NuberOfpage[j];

  

num2=1;

  

break;

}

}

}

  

if(num2==0)

  

{

  

for(i=0;i<3;i++)

  

f12[i]=0;

  

for(current=j-1,current1=1;current1<=framsize-1;current1++,current--)

  

{

for(i=0;i<3;i++)

  

{

  

if(initialNumber[i]==NuberOfpage[current])

  

f12[i]=1;

  

}

  

}

  

  

for(i=0;i<3;i++)

  

  

{

  

  

if(f12[i]==0)

  

inital=i;

  

  

  

}

  

  

initialNumber[inital]=NuberOfpage[j];

  

countr++;

}

  

  

int i;

  

cout<<endl;

  

for(i=0;i<3;i++)

  

cout<<initialNumber[i]<<" ";

}

  

  

cout<<endl<<" The Number of page the faults: "<<endl<<countr<<endl;

  

return 0;

  

  

}

The outcome should printout

1 -1 -1

1 0 -1

1 0 7

1 0 7

1 0 7

1 0 2

1 0 2

1 0 2

1 3 2

0 3 2

0 3 2

0 3 2

4 3 2

4 0 2

4 0 3

4 0 3

2 0 3

2 0 1

2 0 1

7 0 1

The Number of page the faults: 9

0 0
Add a comment Improve this question Transcribed image text
Answer #1

///hey please rate the answer...please rate it

public class Myclass1 {
static int initialNumber[]=new int[4];
public static void main(String args[])
{
//initialization
int NuberOfpage[]={1,0,7,1,0,2,1,2,3,0,3,2,4,0,3,0,2,1,0,7};
//int i,j;
int f12[]=new int[3];
int num1=0,num2=0,countr=0,framsize=3;
int inital=0,current,current1;
for(int i=0;i<3;i++)
{
initialNumber[i]=-1;
}
for(int j=0;j<20;j++)
{
num1=0;num2=0;
for(int i=0;i<3;i++)
{
if(initialNumber[i]==NuberOfpage[j])
{
num1=1;
num2=1;
break;
}
}
if(num1==0)
{
for(int i=0;i<3;i++)
{
if(initialNumber[i]==-1)
{
initialNumber[i]=NuberOfpage[j];
num2=1;
break;
}
}
}
if(num2==0)  
{
for(int i=0;i<3;i++)
f12[i]=0;
for(current=j-1,current1=1;current1<=framsize-1;current1++,current--)
{
for(int i=0;i<3;i++)
{
if(initialNumber[i]==NuberOfpage[current])
f12[i]=1;
}
}
for(int i=0;i<3;i++)
{
if(f12[i]==0)
inital=i;
}
initialNumber[inital]=NuberOfpage[j];
countr++;
}
int i;
System.out.println();
for(i=0;i<3;i++)
System.out.print(initialNumber[i]+" ");
}
System.out.println("\nThe Number of page the faults: "+countr+"\n");
}
  
}

codechef - NetBeans IDE 8.1 File Edit View Navigate Source Refactor Run Debug Profile Team Tools Window Heljp Search (Ctri+)

Add a comment
Know the answer?
Add Answer to:
I have C++ code. How I can change to Java code #include <iostream> using namespace std;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How to convert C++ code to C #include<iostream> #include <stdlib.h> using namespace std; void multiplication() {...

    How to convert C++ code to C #include<iostream> #include <stdlib.h> using namespace std; void multiplication() { int num1; int c, num2, ans; cout<<"Enter difficulty level(1/2)\n"; cin>>c; if(c==1) { num1= rand() % 10; num2= rand() % 10; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans while(ans!=(num1*num2)) { if(ans!=(num1*num2)) { cout<< "No. Please try again.\n"; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans; } } } else if(c==2) { num1= rand() % 10+90; num2= rand() % 10+90; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans; while(ans!=(num1*num2)) { if(ans!=(num1*num2)) { cout<< "No....

  • How convert this c++ into C #include<iostream> #include <stdlib.h> using namespace std; void multiplication() { int...

    How convert this c++ into C #include<iostream> #include <stdlib.h> using namespace std; void multiplication() { int num1; int c, num2, ans; cout<<"Enter difficulty level(1/2)\n"; cin>>c; if(c==1) { num1= rand() % 10; num2= rand() % 10; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans while(ans!=(num1*num2)) { if(ans!=(num1*num2)) { cout<< "No. Please try again.\n"; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans; } } } else if(c==2) { num1= rand() % 10+90; num2= rand() % 10+90; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans; while(ans!=(num1*num2)) { if(ans!=(num1*num2)) { cout<< "No. Please...

  • Fix this code so only the function prototype comes before main. #include <iostream> using namespace std;...

    Fix this code so only the function prototype comes before main. #include <iostream> using namespace std; bool isMultiple(int num1, int num2) { return num1 % num2 == 0; } int main() { char ch = 'Y'; int num1, num2; while(ch =='Y') // While ch is equal to Y { cout << "Enter two numbers(largest first): "; cin >> num1; // Getting 1st number cin >> num2; // Getting 2nd number if(isMultiple(num1, num2)) cout << num2 << " " << "IS...

  • #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int...

    #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int num1, num2, result; while(ch == 'Y'){ cout << "Enter first number: "; cin >> num1; while(1){//for handling invalid inputs if(cin.fail()){ cin.clear();//reseting the buffer cin.ignore(numeric_limits<streamsize>::max(),'\n');//empty the buffer cout<<"You have entered wrong input"<<endl; cout << "Enter first number: "; cin >> num1; } if(!cin.fail()) break; } cout << "Enter second number: "; cin >> num2; while(1){ if(cin.fail()){ cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); cout<<"You have entered wrong input"<<endl; cout <<...

  • Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std;...

    Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std; int main(){    int rows = 5; int cols = 5; int x;    int** arr = new int[rows][cols]    cin >> x; arr[x][x] = x; cout << "arr[x][x] = " << arr[x][x];    return 0; } Question 2: Fix the code to initialize the 2D array elements to x #include <iostream> using namespace std; int main(){    int rows; int cols; int x;...

  • Write the missing statements for the following program. #include <iostream> using namespace std; int main(void) {...

    Write the missing statements for the following program. #include <iostream> using namespace std; int main(void) { int Num1; cout << "Enter 2 numbers: ";    cin >> Num2; if (Num1 < Num2) cout << "Smallest number is " << Num1; else cout << "Smallest number is " << Num2;    return 0; }

  • Redo Programming Exercise 7 of Chapter 7 so that your program handles exceptions such as division...

    Redo Programming Exercise 7 of Chapter 7 so that your program handles exceptions such as division by zero and invalid input. Your program should print Denominator must be nonzero and reprompt for a valid denominator when 0 is entered for a denominator. Please specify what should go in the divisionByZero.h file and the changes made to main.cpp Please provide output. Thank you in advance! main.cpp so far #include <iostream> using namespace std; void addFractions(int num1, int num2, int den1, int...

  • #include <iostream> #include <unistd.h> using namespace std; int main() {    // your code goes here...

    #include <iostream> #include <unistd.h> using namespace std; int main() {    // your code goes here    int num1, num2, num3;    int *p_num1 = &num1;    int *p_num2 = &num2;       *p_num1 = 15;    *p_num2 = 10;       num3 = *p_num1 * *p_num2;    return 0; } whats the value of num3

  • Convert the below code into if else selection: #include <iostream> using namespace std; int main() {...

    Convert the below code into if else selection: #include <iostream> using namespace std; int main() { int num; sin. >> num; switch (num) { case 1: cout << "Casel: Value is: << num << endl; break; case 2: break; case 3: cout << "Case3: Value is: " << num << endl; break; default: cout << "Default: Value is: << num << endl; break; } return; }

  • This is C++ code for parking fee management program #include <iostream> #include <iomanip> using namespace std;...

    This is C++ code for parking fee management program #include <iostream> #include <iomanip> using namespace std; void input(char& car, int& ihour,int& imin, int& ohour, int& omin); void time(char car, int ihour, int imin, int ohour, int omin, int& phour, int& pmin, int& round, double& total); void parkingCharge (char car, int round, double& total); void print(char car, int ihour, int imin, int ohour, int omin, int phour, int pmin, int round, double total); int main() { char car; int ihour; int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT