i actually learned java only a day ago and we have to submit assignment after two days.
i wanted to figure out what i did here with class and function definition is correct.

if correct can you help me out with the compilation error.
in fact all are similar. it will be of great help if you can tell what does first error imply.
it will be of great help..
this is what compiler shows:-

nchy@ubuntu:~/Desktop$ javac pc.java
pc.java:4: error: The public type branchpredictor must be defined in its own file
public class branchpredictor
^^^^^^^^^^^^^^^
pc.java:9: error: Syntax error on token ";", { expected after this token
int[] bimpht=new int[(int)Math.pow(2,10)]; //initializes pattern history table
^
pc.java:28: error: Syntax error on token "(", ; expected
public String predictor(String s4)
^
pc.java:28: error: Syntax error on token ")", ; expected
public String predictor(String s4)
^
pc.java:263: error: Syntax error on token "(", ; expected
public void trainer(String s1,String s2)
^
pc.java:263: error: Syntax error on token ",", ; expected
public void trainer(String s1,String s2)
^
pc.java:263: error: Syntax error on token ")", ; expected
public void trainer(String s1,String s2)
^
pc.java:477: error: Syntax error, insert "}" to complete ClassBody
}
^
8 problems (8 errors)

import java.io.*;
//import java.util.*;

public class branchpredictor
{
	
	int selector=2,decimalpc1,decimalpc;
	
	int[] bimpht=new int[(int)Math.pow(2,10)];    //initializes pattern history table
 	for(int l1=0;l1<10;l1++)
 	{
 		bimpht[l1]=2;
 	}
	
	
	int[] gpht=new int[(int)Math.pow(2,12)];    //initializes pattern history table
 	for(int l=0;l<4096;l++)
 			{
 				gpht[l]=2;
 			}
	
	char[] bhr=new char[6];
 	for(int m=0;m<6;m++)
 	{
 	 	bhr[m]='0';
 	}
	
	public String predictor(String s4)
    {	
	int ihex1;
 	int result1;
 	
 	
 	
 	//for(int k=0;k<i;k++)
 	//{
 	//System.out.println("entered for loop");
 	//String []s4=line[k].split(" ");						
	//System.out.println(s4);
	//System.out.println(s4[2]);
 	
 	char[] arr1=new char[s4.length()-2];
 	s4.getChars(2,s4.length(),arr1,0);

 	String s51=new String(arr1);
 	System.out.println(s51);
 	//if(s4[2].equals(Integer.toString(0)) || s4[2].equals(Integer.toString(1)))
 	//{
 	
 	                           //variable to keep take of bp accuracy
 		String orgpc1;	
 	
		ihex1=Integer.parseInt(s51,16);
		String bin1=Integer.toBinaryString(ihex1);
		System.out.println(bin1);
		if(bin1.length()-10 > 0)
			orgpc1=bin1.substring(bin1.length()-10,bin1.length());
		else
			orgpc1=new String(bin1);
		decimalpc1= Integer.parseInt(orgpc1,2);
 	
 	
 		//ihex1=Integer.parseInt(s5,16);     //ihex1 stores integer equivalent of hexadecimal
 		System.out.println(decimalpc1);
 	
 	
 	
 	
 	
 	
 	
    		if(bimpht[decimalpc1]>1)
     			result1=1;
    		else
     			result1=0;
	
		/*if(s4[2].equals(Integer.toString(result1)))
	 	
	 	{
	 		correct=correct+1;
	 	 	System.out.println("correct=");
	 	 	System.out.println(correct);
	 	}*/
	
		/*if((s4[2].equals(Integer.toString(0)))&&(bimpht[decimalpc]>0))
	 		bimpht[decimalpc]--;
		else if((s4[2].equals(Integer.toString(1)))&&(bimpht[decimalpc]<3))
		 	bimpht[decimalpc]++;*/
	
	
	
	/*System.out.println("exited from for loop");
	float bpacc=correct/total;
	System.out.println(correct);
	System.out.println(total);
	System.out.println(bpacc);*/

	
	
 	
 	int ihex;
 	int result;
 	
 	
 	//for(int k=0;k<i;k++)
 	//{
 		/*System.out.println("entered for loop");
 		System.out.println(line[k]);
 		String []s4=line[k].split(" ");
		System.out.println(s4);
		System.out.println(s4[2]);*/
 	
 		char[] arr=new char[s4.length()-2];
 		s4.getChars(2,s4.length(),arr,0);

 		String s5=new String(arr);
 		System.out.println(s5);
 		
 		/*if(s4[2].equals(Integer.toString(0)) || s4[2].equals(Integer.toString(1)))
 		{
 			total++;*/
 	                           //variable to keep take of bp accuracy
 			String orgpc;	
 	
			ihex=Integer.parseInt(s5,16);
			System.out.println(ihex);
			String bin=Integer.toBinaryString(ihex);
			System.out.println(bin);
	
			if(bin.length()-12 > 0)
				orgpc=bin.substring(bin.length()-12);
			else
				orgpc=new String(bin);
			System.out.println(orgpc);
	
			char[] org=new char[12];
			for(int r=0;r<12;r++)
			{
				org[r]='0';
			}
		
			if(orgpc.length()==12)
				orgpc.getChars(0,12,org,0);
			else if(orgpc.length()<12)
				orgpc.getChars(0,orgpc.length(),org,12-orgpc.length());
	
			char[] bhr1=new char[12];
			int n;
	
			for(int z=0;z<12;z++)
			{
			 System.out.println("org=");
			 System.out.println(org[z]);
			}
			
			
			
			for(n=0;n<6;n++)
	 			{bhr1[n]='0';}
			
			for(n=6;n<12;n++)
	 			{bhr1[n]=bhr[n-6];  }
	 
			for(int z=0;z<12;z++)
			{
			 System.out.println("bhr1=");
			 System.out.println(bhr1[z]); 
			}
			
			char[] xoredpc=new char[12];
	
			//System.out.println(org);
			//System.out.println(bhr1);
	
			for(int p=0;p<12;p++)
			{
	 			int a=org[p];
	 			int b=bhr1[p];
	 			int check=a^b;
	 			
	 			if(check==1)
	 				xoredpc[p]='1';
	
	 			else
	 				xoredpc[p]='0';
	 			System.out.println("XORED="+xoredpc[p]);
			}
	
			String xored=new String(xoredpc);
			System.out.println("XORED="+xored);
	
			decimalpc= Integer.parseInt(xored,2);









//int decimalpc= Integer.parseInt(xored string,2);
 	
 	
 	//ihex=Integer.parseInt(s5,16);     //ihex stores integer equivalent of hexadecimal
 	//System.out.println(decimalpc);
 	
 			
 	
 	
 			//System.out.println(decimalpc);
 			//System.out.println(gpht[decimalpc]);
 	
    		if(gpht[decimalpc]>1)
     			result=1;
    		else
     			result=0;
	
			int result2;
			if(selector>1)
				result2=result;
			else
				result2=result1;
				
			String finresult=Integer.toString(result2) + Integer.toString(result1) + Integer.toString(result1);
			
			return finresult;
			//System.out.println(result);
			
			/*if(s4[2].equals(Integer.toString(result)))
	 		{
	 			correct=correct+1;
	 	 		//System.out.println("correct=");
	 	 		//System.out.println(correct);
	 	 	}
	
			if((s4[2].equals(Integer.toString(0)))&&(gpht[decimalpc]>0))                
	 			gpht[decimalpc]--;                                                     
			else if((s4[2].equals(Integer.toString(1)))&&(gpht[decimalpc]<3))
	 			gpht[decimalpc]++;
	
			
			
			for(int y=0;y<5;y++)
			{
			 	bhr[y+1]=bhr[y];
			}
			bhr[0]=s4[2].charAt(0);*/
		}
	//System.out.println(bhr[0]);
	//System.out.println(k);
	//System.out.println(i);
	
	
		/*System.out.println("exited from for loop");
	
		float bpacc=correct/total;
		
		//System.out.println(correct);
		//System.out.println(total);
		System.out.println(bpacc);*/

	public void trainer(String s1,String s2)
	{
		if((s1.charAt(1)==s2.charAt(0))&&(s1.charAt(2)!=s2.charAt(0)))	
			selector--;
		else if((s1.charAt(1)!=s2.charAt(0))&&(s1.charAt(2)==s2.charAt(0)))
			selector++;
		
		if((s2.equals(Integer.toString(0)))&&(bimpht[decimalpc1]>0))
	 		bimpht[decimalpc1]--;
		else if((s2.equals(Integer.toString(1)))&&(bimpht[decimalpc1]<3))
		 	bimpht[decimalpc1]++;
		 	
		if((s2.equals(Integer.toString(0)))&&(gpht[decimalpc]>0))
	 		gpht[decimalpc]--;
		else if((s2.equals(Integer.toString(1)))&&(gpht[decimalpc]<3))
		 	gpht[decimalpc]++;
		 	
		for(int y=0;y<5;y++)
			{
			 	bhr[y+1]=bhr[y];
			}
			bhr[0]=s2.charAt(0);
	}






}



	
	
	
	
	
	
	
	
	
	



public class pc{
public static void main(String[] args) throws Exception
{
	File f= new File("dyna.txt");
	BufferedReader in = new BufferedReader(new FileReader(f));
	String[] line = new String[40000];

	line[0]  = in.readLine();
	int i=0;
	while(line[i]!=null)
	line[++i] = in.readLine();


	File f1= new File("stata.txt");
	BufferedReader in1 = new BufferedReader(new FileReader(f1));
	String[] line1 = new String[40000];

	line1[0]  = in1.readLine();
	int j=0;
	while(line1[j]!=null)
	line1[++j] = in1.readLine();

	String[] mem_add=new String[10000];
	String[] isBranch=new String[10000];
	String[] dynamic_pc=new String[10000];
	for(int k=0;k<i;k++)
	{
 		String[] s4=line[k].split(" ");
 		dynamic_pc[k]=s4[0];
 		mem_add[k]=s4[1];
 		isBranch[k]=s4[2];
 	}
	
 	String[] static_pc=new String[1000];
 	String[] instruction_type=new String[1000];
 	for(int k=0;k<j;k++)
 	{
 		String[] ins=line1[k].split(" ");
 		static_pc[k]=ins[0];
 		instruction_type[k]=ins[1];
 	}


 
 	for(int z=0;z<i;z++)
 	{                                       //just printing all required field
 		System.out.println(dynamic_pc[z]);
 		System.out.println(mem_add[z]);
 		System.out.println(isBranch[z]);
 	}
 	for(int z=0;z<j;z++)
 	{
 		System.out.println(static_pc[z]);
 		System.out.println(" "+instruction_type[z]);
 	}

      
      
      int clock,num=1,correct=0,total=0,flag=0,index,flagbr=0;
      int mem_next=-1;
      int ex_next=-1;
      int id_next=-1;
      int If_next=-1;
      int inst_type=-1;
      //int l1miss=l2miss=0;
      String st1;
      
      branchpredictor bp=new branchpredictor();
      while(num!=mem_next){
      
                  // Wb(mem_next)
                           {
                             if(num==mem_next)
                             	break;
                           }
      
                   //Mem(ex_next)
                  /* {
                       
                       if(ex_next!=-1))
                       {
                        if((inst_type==0)||(inst_type==1))
                       {
                        //call cache function
                       }
                       }
                       mem_next=ex_next;
                   //contact cache part    
                            }*/
                    
                   //Ex(id_next)
                   		if(id_next!=-1)
                   		{
                       	
                       		//train predictor
                       		if(flagbr==1)
                       		{
                       			bp.trainer(st1,isBranch[num]);		//st1 is returned by predictor();
                       			flagbr=0;
                       		}
                       		
                       		ex_next=id_next;
                       		
                       		
                       		
                       		}
                   
                  // Id(If_next)
				
                       		
                       		if(If_next!=-1)
                       		{
                       		for(int l=0;l<j;l++)
                       		{
                       			if(dynamic_pc[If_next]==static_pc[l]);
                       				index=l;
                       		}
                       		
                       		inst_type=instruction_type[index].charAt(0);
                       		
                       		
                       		id_next=If_next;
                                }
                   
                  
                  // If()
                  {
                         if(isBranch[num].charAt(0)!=-1)
                         {
                          	total++;
                          	flagbr=1;
                          	st1=bp.predictor(dynamic_pc[num]);             //result1 bim gshare
                         	if(isBranch[num].charAt(0)==st1.charAt(0))
                         		correct++;
                         }
                          
                          if(flag==1)
                          {
                           	If_next=-1;
                           	flag=0;
                          }
                          else if(isBranch[num].charAt(0)!=st1.charAt(0))
                         	 {
                         	 flag=1;
                         	 If_next=-1;
                         	 }
                         
                         else
                         If_next=num++;
                  }
         
         
         
         
         
         
         clock++;
         }
                                            








}
}

In Java, you should normally put one class in each file.

The branchpredictor class should be in a file called "branchpredictor.java". Move lines 1 through 300 of your example to that file.


(Alternately, you could make it 'private' instead of 'public'. Or make it an inner class. But those are advanced topics.)

i have initialised some variables at start of branchpredictor.
can these be accessed by both functions of class i.e, predictor() and trainer().

i have to submit assignment in one file.
plus compiler shows that private modifier is not allowed for branchpredictor.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.