public class Main{
public static void main (String args[]){
NumberGroup range1 = new Range(-3, 2);
for (int i : range1.getRange()){
System.out.print(i);
}
MultipleGroups multiple1 = new MultipleGroups();
multiple1.addGroup(new Range(-3,2));
multiple1.addGroup(new Range(3,4));
multiple1.addGroup(new Range(-1,6));
System.out.println();
System.out.println(multiple1.contains(-4));
}
}
public interface NumberGroup{
public boolean contains(int num);
public ArrayList<Integer> getRange();
}
public class Range implements NumberGroup{
private ArrayList<Integer> range = new ArrayList<Integer>();
public Range(int min, int max){
for (int i = min; i <= max; i ++){
this.range.add(i);
}
}
public boolean contains(int num){
return this.range.contains(num);
}
public ArrayList<Integer> getRange(){
return this.range;
}
}
public class MultipleGroups implements NumberGroup{
// private List<NumberGroup> groupList = new List<NumberGroup>();
private List<NumberGroup> groupList = new ArrayList<NumberGroup>();
public MultipleGroups(){
}
public ArrayList<Integer> getRange(){
return groupList.get(1).getRange();
}
public void addGroup(NumberGroup range){
this.groupList.add(range);
}
public boolean contains(int num){
for (NumberGroup range : groupList){
if (range.contains(num)){
return true;
}
}
return false;
}
}
Main.main(null);
-3-2-1012
false
Learnings:
- Declaration and usage of an interface, an interface is an empty abstract class with only related methods. The methods are defined without curly brackets. To use an interface you need to use the implements keyword.
- When implementing an interface and defining an object to be of the type of the interface you can only use methods defined within the interface. ‘
Reflection
The key algorithm in this blog is the functionality of an interface. While I understand the concept and how to use an interface, I think that due to my inexperience it’s still difficult for me to see the usefulness. I looked into it a little online and understood that it was beneficial for security reasons, in case you only want to show the methods and not the implementation. This still confused me a little, as I just wasn’t exactly sure how this would work and how it benefits security. Overall though I understand the concept and feel confident I could tackle these kinds of FRQs if they hadn’t been removed from the exam.
Learnings:
- Declaration and usage of an interface, an interface is an empty abstract class with only related methods. The methods are defined without curly brackets. To use an interface you need to use the implements keyword.
- When implementing an interface and defining an object to be of the type of the interface you can only use methods defined within the interface. ‘
Reflection
The key algorithm in this blog is the functionality of an interface. While I understand the concept and how to use an interface, I think that due to my inexperience it’s still difficult for me to see the usefulness. I looked into it a little online and understood that it was beneficial for security reasons, in case you only want to show the methods and not the implementation. This still confused me a little, as I just wasn’t exactly sure how this would work and how it benefits security.