Results 1 to 4 of 4
  1. #1
    PeterFeng is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default Any comments, can anyone simplify it further?

    Java Code:
    /**
     * @(#)mainMemory.java
     *
     * mainMemory application
     *
     * @author 
     * @version 1.00 2009/1/13
     */
    
    import java.security.SecureRandom;
     
    public class mainMemory {
        
        public static void main(String[] args)
      	{
    	    SecureRandom generator = new SecureRandom();
    	    
    	    //memory Top Blocks
    	    //int memA1 = generator.nextInt(999);
    	    //int memA2 = generator.nextInt(999);
    	    //int memA3 = generator.nextInt(999);
    	    //int memA4 = generator.nextInt(999);
    	    //int memA5 = generator.nextInt(999);
    	    int memA1 = 400;
    	    int memA2 = 200;
    	    int memA3 = 300;
    	    int memA4 = 100;
    	    int memA5 = 500;
    		int[] memA={0, memA1, memA2, memA3, memA4, memA5};
    		
    		//Memory Bottom Blocks
    		int memB1=0, memB2=0, memB3=0, memB4=0, memB5=0;
    		int[] memB={0, memB1, memB2, memB3, memB4, memB5};
    		
    		//Memory C Blocks, to find the real value after allocation
    		int memC1=0, memC2=0, memC3=0, memC4=0, memC5=0;
    		int[] memC={0, memC1, memC2, memC3, memC4, memC5};
    		
    		//Internal and External Frag and Temp mem variable 
    		int iFrag1=0,iFrag2=0,iFrag3=0,iFrag4=0,iFrag5=0; 
    		int eFrag1=0,eFrag2=0,eFrag3=0,eFrag4=0,eFrag5=0;
    		int temp1=0, temp2=0, temp3=0, temp4=0, temp5=0;
    		
    	    //Memory Allocation
    	    //int memAc1 = generator.nextInt(999);
    	    //int memAc2 = generator.nextInt(1999);
    	    //int memAc3 = generator.nextInt(999);
    	    //int memAc4 = generator.nextInt(1);
    	    //int memAc5 = generator.nextInt(1);
    	    int memAc1 = 400;
    	    int memAc2 = 300;
    	    int memAc3 = 200;
    	    int memAc4 = generator.nextInt(1);
    	    int memAc5 = generator.nextInt(1);
    	    
    	    int[] memAc={memAc1,memAc2,memAc3,memAc4,memAc5};
    	    int unallocatedAc1=0, unallocatedAc2=0, unallocatedAc3=0;
    
    	            
    		//Memory Allocation, memAc1: Look for the biggest difference, then allocated memory in
    		int[] myNum={0, memA[1],memA[2],memA[3],memA[4],memA[5]};
    		int maxIndex = findMax(myNum);
    
    			if(memA[maxIndex]>=memAc1)
    			{
    				memB[maxIndex]=memAc1;
    			}
    			else
    		    {
    		    	unallocatedAc1=memAc1;
    		    }
    
    		//The real values after 1st allocation
    		memC[1]=memA[1]-memB[1];
    		memC[2]=memA[2]-memB[2];
    		memC[3]=memA[3]-memB[3];
    		memC[4]=memA[4]-memB[4];
    		memC[5]=memA[5]-memB[5];
    
    		
    		//Start of 2nd allocation
    		if(unallocatedAc1>=0)
    		{	//If 2nd allocated memory is bigger than remaining space in 1st biggest Top memory...
    			if(memC[maxIndex]>=memAc2)
    			{
    				memB[maxIndex]=memB[maxIndex]+memAc2;
    			}
    			
    			else //Slot into the 2nd biggest Top Memory
    			{
    				int[] myNum2={0, memC[1],memC[2],memC[3],memC[4],memC[5]};
    				maxIndex = findMax(myNum2);
    				
    				if(memC[maxIndex]>=memAc2)
    				{
    					memB[maxIndex]=memAc2;
    				}
    				else	//Else it is unlocatted
    			    {
    			    	unallocatedAc2=memAc2;
    			    }
    
    			}
    		}
    		
    		//The real values after 2nd allocation
    		memC[1]=memA[1]-memB[1];
    		memC[2]=memA[2]-memB[2];
    		memC[3]=memA[3]-memB[3];
    		memC[4]=memA[4]-memB[4];
    		memC[5]=memA[5]-memB[5];
    		
    		//Start of 3rd allocation
    		if(unallocatedAc1>=0)
    		{	//If 3rd allocated memory is bigger than remaining space in 2nd biggest Top memory...
    			if(memC[maxIndex]>=memAc3)
    			{
    				memB[maxIndex]=memB[maxIndex]+memAc3;
    			}
    			
    			else //Slot into the 3rd biggest Top Memory
    			{
    				int[] myNum3={0, memC[1],memC[2],memC[3],memC[4],memC[5]};
    				maxIndex = findMax(myNum3);
    				
    				if(memC[maxIndex]>=memAc3)
    				{
    					memB[maxIndex]=memAc3;
    				}
    				else	//Else it is unlocatted
    			    {
    			    	unallocatedAc3=memAc3;
    			    }
    
    			}
    		}
    		
    		//The real values after 3nd allocation
    		memC[1]=memA[1]-memB[1];
    		memC[2]=memA[2]-memB[2];
    		memC[3]=memA[3]-memB[3];
    		memC[4]=memA[4]-memB[4];
    		memC[5]=memA[5]-memB[5];		
    			    		
    	    //Int Fragmentation & Ext Fragmentation
    	    
    	    //1st Block
    	    if(memB[1]!=0)
    	    {
    	    	iFrag1=memA[1]-memB[1];
    	    }
    	    else
    	    {
    	    	eFrag1=memA[1];
    	    }
    	    
    	    //2nd Block
    	    if(memB[2]!=0)
    	    {
    	    	iFrag2=memA[2]-memB[2];
    	    }
    	    else
    	    {
    	    	eFrag2=memA[2];
    	    }
    	    
    	    //3rd Block
    	    if(memB[3]!=0)
    	    {
    	    	iFrag3=memA[3]-memB[3];
    	    }
    	    else
    	    {
    	    	eFrag3=memA[3];
    	    }
    	    
    	    //4th Block
    	    if(memB[4]!=0)
    	    {
    	    	iFrag4=memA[4]-memB[4];
    	    }
    	    else
    	    {
    	    	eFrag4=memA[4];
    	    }
    	    
    	    //5th Block
    	    if(memB[5]!=0)
    	    {
    	    	iFrag5=memA[5]-memB[5];
    	    }
    	    else
    	    {
    	    	eFrag4=memA[5];
    	    }
    	    
    	    //Internal & External Frag Calculation	
    	    int iFragT= iFrag1+iFrag2+iFrag3+iFrag4+iFrag5;
    	    int eFragT= eFrag1+eFrag2+eFrag3+eFrag4+eFrag5;
    	    
    		//Display Output
    		System.out.println("Memory to be allocated: "+memAc1 +"Kb , " +memAc2 +"Kb , " +memAc3 +"Kb , " +memAc4 +"Kb , " +memAc5 +"kb \n");
    		System.out.println("| "+memA[1] +"Kb | " +memA[2] +"Kb | " +memA[3] +"Kb | " +memA[4] +"Kb | " +memA[5] +"kb");
    		System.out.println("| "+memB[1] +"Kb | " +memB[2] +"Kb | " +memB[3] +"Kb | " +memB[4] +"Kb | " +memB[5] +"kb \n\n");
    		System.out.println("| "+memC[1] +"Kb | " +memC[2] +"Kb | " +memC[3] +"Kb | " +memC[4] +"Kb | " +memC[5] +"kb \n\n");
    		System.out.println("Internal Frag: " +iFragT +" Kb");
    		System.out.println("External Fragmentation: " +eFragT+" Kb");
    		System.out.println("Unallocated: " +unallocatedAc1+" Kb, "+unallocatedAc2+" Kb ,"+unallocatedAc3+" Kb");
        }
        
        //Finding Maxing value for 1st Allocation
        public static int findMax(int[]num)
        {
        	int maxNum=num[0];
        	int index=0;
        	
        	for(int i=0; i<num.length;i++)
        	{
    			if(num[i]>maxNum)
    			{
    				maxNum=num[i];
    				index=i;
    			}    		
        	}  return index;  
        }
       
    }
    I wrote the code with my limited understanding of java. It looks noobish, can anyone improve it by shortening it?

    When complied, it creates memory1-5 all random numbers (max 998, random only after you erase the comments.)
    and memory allocated 1-3
    The program will put the 1st allocated into the largest memory in 1-5(top table), 2nd one in the largest memory also, if there is still sufficient space, then 3rd one into the largest memory.

    Internal fragmentation is calculated by memory that is allocated, but has remaining space.
    External fragmentation is calculated by combination of memory that doesn't have allocation.

    The output is like this:
    Memory1|memory2|memory3|memory4|memory5
    Memory1|memory2|memory3|-----0kb|-----0kb <<< allocated
    Last edited by PeterFeng; 01-15-2009 at 04:46 PM.

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    you can improve the code by making it oop and using method calls to replace the if-else statements.

  3. #3
    PeterFeng is offline Member
    Join Date
    Jan 2009
    Posts
    22
    Rep Power
    0

    Default

    erm dun understand...any examples?

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    This is not proper java code, its just a copy-paste of your original codes.

    Java Code:
    import java.security.SecureRandom;
     
    public class mainMemory {
    	// this is a field
    	private int memA1,memA2,memA3,memA4,memA5;
    	private int[] memA;
    	// etc...
        
    	// default constructor
    	public mainMemory(){
    		//memory Top Blocks
    	    memA1 = 400;
    	    memA2 = 200;
    	    memA3 = 300;
    	    memA4 = 100;
    	    memA5 = 500;
    		memA={0, memA1, memA2, memA3, memA4, memA5};
    
    		// etc...
    	}
        
    	public static void main(String[] args)
      	{
    		// etc...
    		
    	    //1st Block
    		compare(memB[1]);
    	    //2nd Block
    		compare(memB[2]);	    
    	    //3rd Block
    		compare(memB[3]);	    
    	    //4th Block
    		compare(memB[4]);	    
    	    //5th Block
    		compare(memB[5]);
    
                   displayOutput();
        }
    
    	public static int compare(int num){
    		if(num!=0)
    	    {
    	    	// etc...
    	    }
    	    else
    	    {
    	    	// etc...
    	    }
    		// etc...
    	}
    	
    	public static void dispalyOutput(){
    			//Display Output
    		System.out.println("Memory to be allocated: "+memAc1 +"Kb , " +memAc2 +"Kb , " +memAc3 +"Kb , " +memAc4 +"Kb , " +memAc5 +"kb \n");
    		System.out.println("| "+memA[1] +"Kb | " +memA[2] +"Kb | " +memA[3] +"Kb | " +memA[4] +"Kb | " +memA[5] +"kb");
    		System.out.println("| "+memB[1] +"Kb | " +memB[2] +"Kb | " +memB[3] +"Kb | " +memB[4] +"Kb | " +memB[5] +"kb \n\n");
    		System.out.println("| "+memC[1] +"Kb | " +memC[2] +"Kb | " +memC[3] +"Kb | " +memC[4] +"Kb | " +memC[5] +"kb \n\n");
    		System.out.println("Internal Frag: " +iFragT +" Kb");
    		System.out.println("External Fragmentation: " +eFragT+" Kb");
    		System.out.println("Unallocated: " +unallocatedAc1+" Kb, "+unallocatedAc2+" Kb ,"+unallocatedAc3+" Kb");
    	}
       
    }

Similar Threads

  1. Javadoc Comments Demo
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:07 PM
  2. How to write code comments
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:06 PM
  3. Comments
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-19-2007, 09:43 AM
  4. Eclipse - comments
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-07-2007, 03:56 PM
  5. Comments in java code
    By lenny in forum New To Java
    Replies: 3
    Last Post: 07-25-2007, 08:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •