Home > Enterprise >  How to change/update a method from outside in java?
How to change/update a method from outside in java?

Time:03-23

I have two letters and for each letter there is a word eg: A-apple, B-bus I need to create a method that returns the words if I provide the letter I was thinking that I could do it this way (method1):

public class Main {
  static void method1(String letter) {
    if (letter.equals("A") {
      System.out.println("apple");
    } else if (letter.equals("B") {
      System.out.println("bus");
    }
  }

The problem is that I need another method (method2) that creates another letter and word pair that can be also returned by method1, how can I do this without changing method1? Can I make a dataframe or list that is outside the methods but can be accessed and updated by the methods?

To clarify I need solution for the following task:

  • I have pairs which contain 1 letter and 1 word
    (for example this is a pair: A-Apple)
  • I have to create two methods
    1. method should print/return the word from a pair if we provide the letter, so for example if we provide the letter A, then it should print/return the word 'Apple'
  • 2.method should somehow creat a new pair that can be later accessed the same way as the above example (A-Apple pair)

CodePudding user response:

I'd start with something trivial, a Test class which has a single data member, a Map (a collection suitable to hold pairs of Key/Value).

public class Test {
    // The collection where we'll hold the pairs
    private Map<String, String> myMap;

    // Constructor
    public Test() {
        myMap = new HashMap<String, String>();
    }

Then I'll add two simple methods, one to add an element to the collection and one to retrieve an element from the collection.

    public String get(String letter) {
         return myMap.get(letter);
    }

    public void put(String letter, String word) {
         myMap.put(letter, word);
    }

Finally a main to test it all, allocate the Test object, add a few elements, then retrieve one and print it's value.

    public static void main(String [] args) {
        Test app = new Test();
        app.put("A", "Apple");
        app.put("B", "Bus");

        String value = app.get("A");
        System.out.println(value);
        System.exit(0);
    }
}

CodePudding user response:

As OH GOD SPIDERS metioned, you have to use a Map.

Create an Alphabet class, like this:

public class Alphabet {
// Find why do we use a ConcurentHashMap? What other types are there?
private final Map<Character, String> letterWordMap = new ConcurrentHashMap<>();

public void addWord(Character letter, String word) {
    // Try to replace with .putIfAbsent() and check the return value of this method!
    letterWordMap.put(letter, word);
}

public String getWord(Character letter) {
    // Try to get a non existing letter like 'X'
    return letterWordMap.get(letter);
}}

Test the methods in a main method:

public class Main {
public static void main(String[] args) {
    Alphabet alphabet = new Alphabet();
    // Add letters and words
    alphabet.addWord('A', "Apple");
    alphabet.addWord('B', "Bus");

    // Get the word
    String wordForLetterA = alphabet.getWord('A');
    System.out.println("Word for letter 'A': "   wordForLetterA);

    // Add another letter and word
    alphabet.addWord('J', "Java");

    // Get another word
    String wordForLetterJ = alphabet.getWord('J');
    System.out.println("Word for letter 'J': "   wordForLetterJ);
}}

CodePudding user response:

Not one hundred percent sure what your question is but if it is how to change variables inside objects that are done by setter methods inside of the class of the object. If this is not your question please respond using a comment and I can try help from there

public void setVar(String newValue)
{
    this.oldVar = newVale
}
  • Related