Can you make it more efficient and paraphrase it in Java   import java.lang.Math; import java.util.*; import javax.xml.stream.events.EndDocument; public class KiK {     private static int boardSize = 12;     private static int rand;          public static Integer [] randomGenotype()     {         Integer [] genotype = new Integer [boardSize];         for(int i=0; i rand1)             {                 for(int i = rand2; i > rand1+1; i-- )                 {                     int temp = genotype[i-1];                     genotype[i-1] = genotype[i];                     genotype[i] = temp;                  }             }             else             {                 for(int i = rand1; i > rand2+1; i-- )                 {                     int temp = genotype[i-1];                     genotype[i-1] = genotype[i];                     genotype[i] = temp;                  }             }                      }         return genotype;     }          public static Integer[][] recombine(Integer[] parent0, Integer[] parent1)     {         Integer [][] children = new Integer [2][boardSize];         int HALFWAY= boardSize/2;         for(int i=0; i

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Can you make it more efficient and paraphrase it in Java

 

import java.lang.Math;
import java.util.*;

import javax.xml.stream.events.EndDocument;


public class KiK
{
    private static int boardSize = 12;
    private static int rand;
    

    public static Integer [] randomGenotype()
    {
        Integer [] genotype = new Integer [boardSize];
        for(int i=0; i<genotype.length; i++)
        {
            genotype[i] = 0;
        }
        
        rand = (int)(Math.random()*boardSize +1);

        for(int i=0; i<genotype.length; i++)
        {
            while(checkArray(genotype, rand))
            {
                rand = (int)(Math.random()*boardSize +1);
            }
            genotype[i] = rand;
        }
        
        return genotype;
    }


    private static boolean checkArray(Integer[] arr, int num)
    {
        for(int i=0; i<arr.length; i++)
        {
            if(arr[i] == num) {return true;}
        }
        return false;
    }
    

    public static Integer[] insertMutate(Integer[] genotype, double p)
    {
        double pVal = Math.random();
        if(pVal <= p)
        {
            int rand1 = (int)(Math.random()*boardSize);
            int rand2 = (int)(Math.random()*boardSize);
            while(rand1 == rand2)
            {
                rand1 = (int)(Math.random()*boardSize);
                rand2 = (int)(Math.random()*boardSize);
            }

            if(rand2 > rand1)
            {
                for(int i = rand2; i > rand1+1; i-- )
                {
                    int temp = genotype[i-1];
                    genotype[i-1] = genotype[i];
                    genotype[i] = temp; 
                }
            }
            else
            {
                for(int i = rand1; i > rand2+1; i-- )
                {
                    int temp = genotype[i-1];
                    genotype[i-1] = genotype[i];
                    genotype[i] = temp; 
                }
            }
            
        }
        return genotype;
    }
    

    public static Integer[][] recombine(Integer[] parent0, Integer[] parent1)
    {
        Integer [][] children = new Integer [2][boardSize];
        int HALFWAY= boardSize/2;

        for(int i=0; i<boardSize; i++)
        {
            children[0][i] = 0;
            children[1][i] = 0;
        }
        
        for(int i=0; i<HALFWAY; i++)
        {
            children[0][i] = parent0[i];
            children[1][i] = parent1[i];
        }

        for(int i=HALFWAY; i<boardSize; i++)
        {
            int index = HALFWAY;

            while(checkArray(children[0], parent1[index]))
            {
                index++;
                if(index == boardSize) {index = 0;}
            }
            children[0][i] = parent1[index];

            index = HALFWAY;

            while(checkArray(children[1], parent0[index]))
            {
                index++;
                if(index == boardSize) {index = 0;}
            }
            children[1][i] = parent0[index];
        }
        
        return children;
    }
    

    private static int isThreatened(Integer [] genotype, int index)
    {
        int count = 0;
        int val = genotype[index];
        for(int i = 1; i<= index; i++)
        {
            int threat = genotype[index-i];
            if(threat == val) count++;
            if(threat == val - i) count++;
            if(threat == val + i) count++;
        }
        return count;
    }


    public static int measureFitness(Integer [] genotype)
    {

        int fitness = (int) (0.5 * boardSize * (boardSize - 1));
        
        for(int i=0 ; i<boardSize; i++)
        {
            for(int j=0; j< isThreatened(genotype, i); j++)
            {
                fitness--;
            }
        }

        return fitness;
    }
}

Expert Solution
Step 1

In this question, it is asked to improve a given java code.

For improvements, better comments are used for understanding.

Better functions are used in the java program to do tasks like filling of array more efficiently.

 

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Files and Directory
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education