0

Im trying to make a program that allows the client to input a String. The string length should have 3 characters only and should contain the letters .

My program have to pass through this table and check what this string refers to..

Let's say the client passed this String "AUG", my program should show the name of this String which is "Met".

I made a code, and it worked but it has more then 15 if else-if condition.

My question is : Is there any other way to do it without using if else-if (or switch). And does polymorphism work in this case ?

3 Answers3

2

Have a look at HashMap

You can build your table with:

Map<String, String> table = new HashMap<>();
table.put("AUG", "Met");
table.put(...);

Then access your table using the user's input:

if(table.containsKey(input)){
    return table.get(input);
}
Ewan Brown
  • 143
  • 1
  • 9
  • if I use HashMap it will be even longer because there are 64 possibilities. example for Ala : `table.put("GCU", "Ala"); table.put("GCC", "Ala"); table.put("GCA", "Ala"); table.put("GCG", "Ala");` –  Feb 11 '20 at 01:43
  • You'd be looking for [inline initialization of a HashMap](https://stackoverflow.com/q/6802483/5037905). You could also try a simple data serialization technique like just [importing from a CSV](https://stackoverflow.com/a/20068534/5037905) (standard library). – Eric Reed Feb 11 '20 at 01:53
  • 1
    If you're going to be accessing the table multiple times in the program, filling a hashmap and running get methods is going to be more efficient than running 16 if-statements for every user input. The fact is, you have 64 pieces of data that your program is going to use. You can either save that in memory using a hashmap, or work out the mapping every time using if-statements. I'm afraid there isn't a nice way to do it, to the best of my knowledge. – Ewan Brown Feb 11 '20 at 01:53
0

I think I'd go about it with an enum personally (provided performance wasn't a significant concern):

public enum Abbreviations {
    Ala("GCU", "GCC", "GCA", "GCG"),
    Arg("CGU", "CGC", "CGA", "CGG", "AGA", "AGG")
    // ...
    ;

    private final List<String> codons;

    private Abbreviations(final String... codons) {
        this.codons = Arrays.asList(codons);
    }


    public boolean contains(final String codon) {
        return this.codons.contains(codon);
    }
}

And then you can find their matching from the String using something like:

public String find(final String codon) {
    for (final Abbreviations abb : Abbreviations.values()) {
        if (abb.contains(codon)) {
            return abb.name();
        }
    }

    throw new IllegalArgumentException("Unknown codon: '" + codon + "'");
}
BeUndead
  • 3,463
  • 2
  • 17
  • 21
  • (I would go further and have a `Nucleotides` `enum`, and a `Codons` `enum` which had a constructor taking three of the `Nucleotides`, and then make your `Abbreviations` class accept a vararg array of `Codons` to go a fully object-oriented approach... But that seemed overkill for a quick answer...) – BeUndead Feb 11 '20 at 02:28
  • what if I had an ArrayList, each element corresponds to a codon (ex : "AUG") what will the method find look like ? Im trying to change its type and parameter but its giving me error –  Feb 11 '20 at 03:26
  • @Beee: I don't understand the question. A `List` is already generated for each of the `Abbreviations` here, so the search method would look as it does here. – BeUndead Feb 11 '20 at 09:37
0

You could try an Object Oriented Aproach:

//This is your representation of Codon
//Which has a name e.g. Alanine and an Abreviation object.
public class Codon {

        private String name;

        private Abreviation abreviation;

        public Codon(String name, Abreviation abreviation) {

            this.name = name;
            this.abreviation = abreviation;
            this.abreviation.addCodon(this);

        }

        @Override
        public String toString() {
            return "Codon [name=" + name + ", abreviation=" + abreviation + "]";
        }


    }

import java.util.ArrayList;
import java.util.List;
// This is a representation of an abreviation object
// Which has an abreviation: ALA;
// and the name of the abreviation "Alanine".
public class Abreviation {

    private String abreviation;
    private String name;
    private List<Codon> codons = new ArrayList<>();

    public Abreviation(String abreviation, String name) {
        super();
        this.abreviation = abreviation;
        this.name = name;

    }

    public boolean addCodon(Codon codon) {
        return this.codons.add(codon);
    }

    @Override
    public String toString() {
        return "Abreviation [abreviation=" + abreviation + ", name=" + name + "]";
    }

}


// Here is your program, where it's being build all the Codons structure with your respective Abbreviation.
public class App {

    public static void main(String[] args) {
        // This is abreviation, it'll will associated with the codon 
        Abreviation alanine = new Abreviation("Ala", "Alanine");
        // Here it's being build the codon CGU, which has abreviation alanine
        Codon GCU = new Codon("GCU", alanine);
        // Then using toString method it prints what have been done 
        System.out.println(GCU);

    }

}

You can put all of your codons into a List, so you can search and retrieve then.

Jorge Duarte
  • 126
  • 7
  • You wanna explain what your code is doing in detail there my dude? Without an explanation, it just looks like a block of code that does... something. – guninvalid Feb 11 '20 at 02:19