Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4123318
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:35:46+00:00 2026-05-20T23:35:46+00:00

I’m quite new to java OOP concept. I have some problems in implementing some

  • 0

I’m quite new to java OOP concept. I have some problems in implementing some stuffs about genetics in java. Here’s the scenario.

A human cell contains 23 pairs of chromosomes. Each chromosome in each pair has thousands of genes. Each copy of a gene is called an allele. These alleles determine the different traits of a human(eye color, height, hair color, genetic diseases etc). An allele can either be dominant or recessive.

A dominant allele is always expressed in the genome of its bearer. However, if information from one allele is not expressed when a dominant allele of the same gene is present, it is a recessive allele. The peculiarity of the recessive allele of a gene is that it can be present in the genome and transmitted over several generations without it is expressed in the phenotype its bearers. If there is no dominant allele, two copies of the gene have the same recessive allele (homozygous recessive) then the recessive character is expressed.

So now, on a pair of chromosome of the cell of a child, one chromosome comes from the mother and the other from the father. So lets say on position 1 of the father chromosome contains an allele (either dominant or recessive) of the gene responsible for eye color, on the same position of the mother chromosome must contain another allele (either dominant or recessive) of the same gene.
So how can i represent these classes allele, gene correctly? Here’s what i have so far

Allele.java
    /**
 * 
 */
package project_try;

/**
 * @author mkab
 *
 */
public class Allele {

    private String trait;
    private int type;

    public Allele(String trait, int type) {
        this.trait = trait;
        this.type = type;
    }

    public boolean equals(Allele a){
        if(this.getTrait().equals(a.getTrait()) && this.getType()==a.getType())
            return true;

        return false;
    }

    /**
     * @return the trait
     */
    public String getTrait() {
        return trait;
    }

    /**
     * @param trait the trait to set
     */
    public void setTrait(String trait) {
        this.trait = trait;
    }

    /**
     * @param type the type to set
     */
    public void setType(int type) {
        this.type = type;
    }

    /**
     * @return the type either recessive(0) or dominant(1)
     */
    public int getType() {
        return type;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Allele [trait=" + trait + ", type=" +
        (this.getType()==0?"recessive":"dominant")+ "]";
    }
}

Gene.java

    /**
 * 
 */

package project_try;

import java.util.Random;

/**
 * @author mkab
 *
 */
public class Gene implements Cloneable {

    private Allele allele;

    public Gene(){
        super();
    }

    public Gene(Allele allele){
        super();
        this.allele = allele;
    }

    /**
     * Randomly selects a trait from trait1 or trait2 and returns a new Gene with that trait
     * @param trait1
     * @param trait2
     * 
     * @return a new Gene
     */
    public Gene randomAllele(Allele trait1, Allele trait2){
        Allele allele = null;
        Random rand = new Random();
        int i = rand.nextInt(2);// generate between 0 and 2: only 2 possibilities: 0 or 1

        switch(i){
        case 0:
            allele = trait1;
            break;

        case 1:
            allele = trait2;
            break;
        }

        return new Gene(allele);
    }

    /**
     * Clones a gene
     */
    public Gene clone() throws CloneNotSupportedException{
        Gene g;
        g = (Gene) super.clone();
        return g;
    }

    public boolean equals(Gene g){
        if(this.getAllele().equals(g.getAllele()) && 
                this.getAlleleType() == g.getAlleleType() )
            return true;
        return false;
    }

    /**
     * @param allele the allele to set
     */
    public void setAllele(Allele allele) {
        this.allele = allele;
    }

    /**
     * 
     * @return - the allele 
     */
    public Allele getAllele() {
        return allele;
    }

    /**
     * 
     * @return the trait of the gene or allele
     */
    public String getAlleleTrait(){
        return allele.getTrait();
    }

    /**
     * 
     * @return allele type. Either recessive or dominant
     */
    public int getAlleleType(){
        return allele.getType();
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Gene [" + allele +"]";
    }
}

How can I also represent the class chromosome? I’m thinking about using an arraylist of gene types. But the problem using this is that I cannot define for example the 1st position of the array should be used for eye color, the second position for height etc.
Any suggestions please? Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:35:47+00:00Added an answer on May 20, 2026 at 11:35 pm

    I don’t see problems with

    List<Chromosomes> chromosomes = new ArrayList<Chromosomes>();
    

    define

    class Chromosome {
        // values
    }
    
    class EyeColourChromosome extends Chromosome {
        // define specific values
    }
    
    
    class HeightChromosome extends Chromosome {
        // define specific values
    }
    

    Or…

    you can use factory pattern for creating specific chromosomes like

    class Chromosome {
        // values
        public static Chromosome chromosomeFactory(String type) {
            switch(type) {
            case EYE_COLOUR:
                return new EyeColourChromosome(...);
                break;
            case HEIGHT:
                return new HeightChromosome(...);
                break;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
i want to parse a xhtml file and display in UITableView. what is the
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.