ronreiter / interactive-tutorials

Interactive Tutorials
Apache License 2.0
4.08k stars 2.6k forks source link

Issue in the exercise at tutorials/learnjavaonline.org/en/Using Generics #614

Closed DavideTedesco closed 3 years ago

DavideTedesco commented 3 years ago

The solution for the exercise is incorrect, here is the correct one:

import java.util.*;
public class Main{
    public static class FavoriteClasses<ClassA,ClassB,ClassC>{
        private ClassA favorite1;
        private ClassB favorite2;
        private ClassC favorite3;
        FavoriteClasses(ClassA fav1, ClassB fav2, ClassC fav3){
            this.favorite1=fav1;
            this.favorite2=fav2;
            this.favorite3=fav3;
        }
        public ClassA getFav1(){
            return(this.favorite1);
        }
        public ClassB getFav2(){
            return(this.favorite2);
        }
        public ClassC getFav3(){
            return(this.favorite3);
        }
    }
    public static void main(String[] args){
        List<Double> r=new ArrayList<>();     //can also be double or any other that supports decimals
        r.add(6.3);
        r.add(5.9);
        FavoriteClasses<String, Integer, Double> a=new FavoriteClasses<>("Hello",67,r.get(0)); //same with int
        System.out.println("My favorites are " + a.getFav1() + ", "+ a.getFav2() + ", and " + a.getFav3() + ".");
    }
}
ronreiter commented 3 years ago

can you please issue a pull request?

DavideTedesco commented 3 years ago

done!