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
  • 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...

  • 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; }

  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

  • #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure...

    #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure char start_state, to_state; char symbol_read; }; void read_DFA(struct transition *t, char *f, int &final_states, int &transitions){ int i, j, count = 0; ifstream dfa_file; string line; stringstream ss; dfa_file.open("dfa.txt"); getline(dfa_file, line); // reading final states for(i = 0; i < line.length(); i++){ if(line[i] >= '0' && line[i] <= '9') f[count++] = line[i]; } final_states = count; // total number of final states // reading...

  • Explain the output of the following C++ program. #include <iostream> using namespace std; void Magic(int i=1,...

    Explain the output of the following C++ program. #include <iostream> using namespace std; void Magic(int i=1, int j=2,int k=3, double product =1.0) { i+=2; j*=2; k/=2; product=i*j*k; } void Magic(int& i, int& j, double& product) { i+=2; j=j*2+2; product=i*j; } void Magic(int* i,int* j) { double product; *i+=2; *j=*j*2+2; product=*i * *j; } int main() { double product; int i=0,j=0,k=0; product=i*j*k;    Magic(); cout<<"i, j, k and product in main () after 1st round:"<<endl<<i<<endl<<j<<endl<<k<<endl<<product<<endl;    Magic(2,4); cout<<"i, j, k and...

  • May i ask for help with this c++ problem? this is the code i have for assignment 4 question 2: #...

    may i ask for help with this c++ problem? this is the code i have for assignment 4 question 2: #include<iostream> #include<string> #include<sstream> #include<stack> using namespace std; int main() { string inputStr; stack <int> numberStack; cout<<"Enter your expression::"; getline(cin,inputStr); int len=inputStr.length(); stringstream inputStream(inputStr); string word; int val,num1,num2; while (inputStream >> word) { //cout << word << endl; if(word[0] != '+'&& word[0] != '-' && word[0] != '*') { val=stoi(word); numberStack.push(val); // cout<<"Val:"<<val<<endl; } else if(word[0]=='+') { num1=numberStack.top(); numberStack.pop(); num2=numberStack.top(); numberStack.pop();...

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
ADVERTISEMENT