Java exercise with ArraysList and returning a boolean expresion

By | October 30, 2018

I’m stack doing this exercise. I don’t know how to implement the code inside the boolean teach() and check if teacher can teach programmer. Here is the question and the code:

Create a class Programmer with:

  • public method Collection<String> getLanguages –> return all languages that the programmer knows

  • public method void addLanguage(String Language) –> adds a new language to the list of languages

Create a class ProgrammerTeacher that inherits Programmer and has one additional public method:

  • boolean teach(Programmer programmer, String Language) –> If ProgrammerTeacher knows the required languages, it teaches Programmer and returns true, otherwise it returns false.

Classes should be package private (no explicit modifier). For example, the following code shows how ProgrammerTeacher teaches Programmer
the new Language("Java"):

ProgrammerTeacher teacher = new ProgrammerTeacher();
Programmer programmer = new Programmer();
teacher.teach(Programmer, "Java")
for(String language : programmer.getLanguages())

//Here is my answer so far

    import java.util.*;

    class Programmer {
        Collection<String> Languages = new ArrayList<String>();     //Creating a Collection of languages(Strings) for the programmer
        public Collection<String> getLanguages(){
            return Languages;
        public void addLanguage(String Language){
    class ProgrammerTeacher extends Programmer{  //ProgrammerTeacher inherits Programmer
        public boolean teach(Programmer programmer, String Language){
         //Here is my problem
                return true;        //If ProgrammerTeacher knows the required languages it teaches Programmer and returns true
            return false; //If not return false
        public static void main(String[] args) {
          ProgrammerTeacher teacher = new ProgrammerTeacher();

          Programmer programmer = new Programmer();
          teacher.teach(programmer, "Java");

          for(String language : programmer.getLanguages())

Leave a Reply

Your email address will not be published. Required fields are marked *