14 min read

Part a

A number group represents a group of integers defined in some way. It could be empty, or it could contain one or more integers.

Write an interface named NumberGroup that represents a group of integers. The interface should have a single contains method that determines if a given integer is in the group. For example, if group1 is of type NumberGroup, and it contains only the two numbers -5 and 3, then group1.contains(-5) would return true, and group1.contains(2) would return false. Write the complete NumberGroup interface. It must have exactly one method.

import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        NumberGroup group1 = new IntegerList(Arrays.asList(100, 200));
        System.out.println(group1.contains(100));
        System.out.println(group1.contains(300));
    }

    public interface NumberGroup {
        boolean contains(int num);
    }

    public static class IntegerList implements NumberGroup {
        private List<Integer> numbers;

        public IntegerList(List<Integer> numbers) {
            this.numbers = numbers;
        }

        @Override
        public boolean contains(int num) {
            return numbers.contains(num);
        }
    }
}
Main.main(null)
true
false
public interface NumberGroup {
    boolean contains(int value);
}

Part b

A range represents a number group that contains all (and only) the integers between a minimum value and a maximum value, inclusive. Write the Range class, which is a NumberGroup. The Range class represents the group of int values that range from a given minimum value up through a given maximum value, inclusive. For example,the declaration

NumberGroup range1 = new Range(-3, 2);

represents the group of integer values -3, -2, -1, 0, 1, 2.

Write the complete Range class. Include all necessary instance variables and methods as well as a constructor that takes two int parameters. The first parameter represents the minimum value, and the second parameter represents the maximum value of the range. You may assume that the minimum is less than or equal to the maximum.

public class Main {    
    public static void main(String[] args) {
        NumberGroup range1 = new Range(100, 200);
        System.out.println(range1.contains(150));
        System.out.println(range1.contains(175));
        System.out.println(range1.contains(300));
    }

    public interface NumberGroup {
        boolean contains(int num);
    }

    public static class Range implements NumberGroup {
        private int min;
        private int max;

        public Range(int min, int max) {
            this.min = min;
            this.max = max;
        }

        @Override
        public boolean contains(int number) {
            return number >= min && number <= max;
        }
    }
}
Main.main(null)
true
true
false

Part c

The MultipleGroups class (not shown) represents a collection of NumberGroup objects and isa NumberGroup. The MultipleGroups class stores the number groups in the instance variable groupList (shown below), which is initialized in the constructor.

private List<NumberGroup> groupList;

Write the MultipleGroups method contains. The method takes an integer and returns true if and only if the integer is contained in one or more of the number groups in groupList.

For example, suppose multiple1 has been declared as an instance of MultipleGroups and consists of the three ranges created by the calls new Range(5, 8), new Range(10, 12), and new Range(1, 6). The following table shows the results of several calls to contains.

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<NumberGroup> groups = new ArrayList<>();
        groups.add(new Range(100, 200));
        groups.add(new Range(300, 400));
        groups.add(new Range(500, 600));

        MultipleGroups multiple1 = new MultipleGroups(groups);

        System.out.println(multiple1.contains(150)); // true
        System.out.println(multiple1.contains(250)); // false
        System.out.println(multiple1.contains(550)); // true
    }

    // from part A
    public interface NumberGroup {
        boolean contains(int num);
    }

    // from part B
    public static class Range implements NumberGroup {
        private int min;
        private int max;

        public Range(int min, int max) {
            this.min = min;
            this.max = max;
        }

        @Override
        public boolean contains(int num) {
            return num >= min && num <= max;
        }
    }

    public static class MultipleGroups implements NumberGroup {
        private List<NumberGroup> groupList;

        public MultipleGroups(List<NumberGroup> groupList) {
            this.groupList = groupList;
        }

        public boolean contains(int num) {
            for (NumberGroup group : groupList) {
                if (group.contains(num)) {
                    return true;
                }
            }
            return false;
        }
    }
}

Main.main(null);
true
false
true

Summary

This question was very hard and I needed some help just like number 3, it was definitely not as easy as the first part. I think that this question, specifically part was easier thatn it looked, but there were a lot of other parts that needed some change because I had no idea what exactly what I needed to implement within the code cell. This is about classes and learning how to use classes and manipulate control structures. This problem wasn’t as hard as question number 3, but I also had no idea how to attempt parts b and c, so I turned to raunak for some help and he offered me some guidance. This was a part that I did on Sunday night along with FRQ 3 because I didn’t understand when I did them all on Saturday Stats: Time: 42 minutes

Date Completed: February 24th, 2023

CollegeBoard Scoring: 7/9

Personal Ranking: 2 Stats: Time: 37 minutes

Date Completed: February 24th, 2023

CollegeBoard Scoring: /9

Personal Ranking: 3

Scoring

This question was worth 9 points

part a:

  • +1 for interface NumberGroup
  • +1 for boolean contains(int num);

part b:

  • +1 for class Range implements NumberGroup
  • +1 for Declares appropriate private instance variable(s)
  • +1 for Uses correct constructor header
  • +1 for Initializes instance variables within constructor using parameters
  • +1 for Computes and returns correct value from contains

part c:

  • +1 for Calls Contains on elements of groupList in context of loop
  • +1 for Computes and returns correct value

question specific penalties:

  • -1 for inappropriate use of static