1

So, it's one of our labs at Computers Systems Architecture, in which we are required to illustrate the process of cache(ArrayList of string) working with main memory(2 dimensional array of int) each [i][j] of that contains 4 int elements(cyan, magenta, yellow, black) of class Color. Therefore, we must show number of hits, misses, hit/miss-ratio and total accessed array cells.

I have mistake in my boolean cacheCheck() function, it never returns true! Was looking for error hours long, my will became weak, Help me! XD`

my function:

public static boolean cacheCheck(int i, int j, int color, ArrayList<String>     
cache){
boolean Check = false;

if(cache.size() >= 3) {
String firstElement = cache.get(cache.size()-3);
secondElement = cache.get(cache.size()-2);
String thirdElement = cache.get(cache.size()-1);
if(firstElement.equals(String.valueOf(i)) &&     
secondElement.equals(String.valueOf(j))){
if(thirdElement.equals(String.valueOf(mainMemory[i][j].c)) ||
thirdElement.equals(String.valueOf(mainMemory[i][j].m)) || 
thirdElement.equals(String.valueOf(mainMemory[i][j].y)) ||
thirdElement.equals(String.valueOf(mainMemory[i][j].k)))
                                Check = true;
}
}
return Check;
}

full java code

package cacheprocess;

import java.util.ArrayList;
import java.util.Scanner;


public class CacheProcess {

public static Color[][] mainMemory;
public static ArrayList<String> Cache = new ArrayList<>();


public static void main(String[] args) {


}

public static void init(){
int N,M,K;
float mRate, hRate;

Scanner s = new Scanner(System.in);
int algorithmNum;

do {
System.out.println("Enter N");
N = s.nextInt();
System.out.println("Enter M");
M = s.nextInt();
} while (!(((M*N)%2) == 0));

do {
System.out.println("Enter number of K blocks :");
K = s.nextInt();
} while (!((K%2) == 0) && K > (N * M / 4));
do {
System.out.println("Choose algorithm:");
algorithmNum = s.nextInt();  
int [] h;

switch(algorithmNum) {
case 1: 
h=Algorithm1(N,M,K);
break;
//case 2: 
//    h=Algorithm2(N,M,K);
//  break;
// case 3: 
//    h=Algorithm3(N,M,K);
// System.out.println("Y Total:  " + h[3]);
//   System.out.println("Y Miss:   " + h[4]);
//break;
default: 
h=Algorithm1(N,M,K);
break;
}
mRate=(float)h[1]/(float)h[0];
hRate=(float)h[2]/(float)h[0];
System.out.println("Total access: " + h[0]);
System.out.println("Miss:  " + h[1]);
System.out.println("Hit:   " + h[2]);
System.out.println("Miss Rate:   " + mRate);
System.out.println("Hit Rate:    " + hRate +"\n\n");
}while(true);                     
}


public static int [] Algorithm1(int N,int M, int K){

int[] result = new int[3];

mainMemory = new Color[N][M];

int miss = 0,  totalAccess = 0, cellsFilled = 0;

for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
mainMemory[i][j] = new Color();
mainMemory[i][j].c = 0;
mainMemory[i][j].m = 0;
mainMemory[i][j].y = 1;
mainMemory[i][j].k = 0;
}
}


for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {


Cache.add( String.valueOf(i) + String.valueOf(j)+ mainMemory[i][j].c);
cellsFilled++;                                                        
if(cellsFilled == K){
Cache.clear();
}                                                        

if(!cacheCheck(i,j,mainMemory[i][j].y, Cache)){                            
miss++;
totalAccess++;
}


Cache.add(String.valueOf(i)+String.valueOf(j)+mainMemory[i][j].m);
cellsFilled++;
if(cellsFilled == K){
Cache.clear();
}

if(!cacheCheck(i,j,mainMemory[i][j].y, Cache)){                            
miss++;
totalAccess++;
}



Cache.add((String.valueOf(i)+String.valueOf(j)+mainMemory[i][j].y));
cellsFilled++;
if(cellsFilled == K){
Cache.clear();
}
if(!cacheCheck(i,j,mainMemory[i][j].y, Cache)){                            
miss++;
totalAccess++;
}


Cache.add((String.valueOf(i)+String.valueOf(j)+mainMemory[i][j].k));
cellsFilled++;
if(cellsFilled == K){
Cache.clear();
}

if(!cacheCheck(i,j,mainMemory[i][j].y, Cache)){                            
miss++;
totalAccess++;

}



}
}
result[0]=totalAccess;
result[1]=miss;
result[2]=totalAccess-miss;
return result;
}


public static boolean cacheCheck(int i, int j, int color, ArrayList<String>     
cache){
boolean Check = false;

if(cache.size() >= 3) {
String firstElement = cache.get(cache.size()-3);
secondElement = cache.get(cache.size()-2);
String thirdElement = cache.get(cache.size()-1);
if(firstElement.equals(String.valueOf(i)) &&     
secondElement.equals(String.valueOf(j))){
if(thirdElement.equals(String.valueOf(mainMemory[i][j].c)) ||
thirdElement.equals(String.valueOf(mainMemory[i][j].m)) || 
thirdElement.equals(String.valueOf(mainMemory[i][j].y)) ||
thirdElement.equals(String.valueOf(mainMemory[i][j].k)))
                                Check = true;
}
}
return Check;
}
Shams
  • 3,637
  • 5
  • 31
  • 49
  • Hi Bekzat, and welcome to Stack Overflow. A couple comments: first, please fully indent your code. I know it can be a bit of a pain, but it helps readability a lot. It's great that you gave us your code and told us what's going wrong. Please help us read it :) And second, you should know that Stack Overflow tags are generally less-is-more: a tag should only be applied if your question **directly** relates to it. E.g. the "Eclipse" tag should be used only if your problem relates to Eclipse, not any time you have a problem and Eclipse is the IDE you are using – MyStackRunnethOver Oct 19 '18 at 18:20
  • Hi, thanks for advice, will keep it in mind next time asking! :) How can I edit my question? – Bekzat Kambar Oct 19 '18 at 18:49
  • You're welcome, thank you! If you look at the question on the page, there is an `edit` button at the bottom, right under the list of tags – MyStackRunnethOver Oct 19 '18 at 18:50

0 Answers0