IntegerPairs Solution


Category: Tag:



The project name of this exercise is **IntegerPairs**.

The purpose of this assignment is to give you practice parsing strings and using some combination of if statements, parallel if statements, switch statements, and Boolean expressions.

Problem Description

You are to write a program that will take one string of four positive, single-digit integers (e.g. “0 1 1 2”) and will test to see if there are two pairs present in the string. This will be implemented by the **IntegerPairs** object. This object will have a constructor that takes no arguments and the following two methods:

– “`java

public void setPairString(String str)


– This method will take a string as an argument and then store it in an instance variable.

– “`java

public boolean hasTwoPairs()


– This will return true when there are only two pairs present in the string.


– You can use a **Scanner** on a string as well as ****. It works the same way but the constructor takes a string rather than ****.

– After you are done with the scanner be sure to call **{your scanner variable name}.close()**;

– Don’t be afraid to make local variables. You can use them to hold the numbers you get from the string.

– You do NOT need nested ifs and you definitely do NOT need loops to solve this

– Think about using the && (logical and) operator to test conditions

– Depending on what method you use to convert strings (e.g. Integer.parseInt), you may want to use an try…catch block (covered in class) to handle a parsing exception. See the Java API documentation for what type of exception an the parseInt method throws

Getting Started

Using the techniques shown on the instruction page [How to Start Every Java Project in this Course](, create a new project from the GitHub classroom assignment.

Create the following files in the **src/main/java/edu/sbcc/cs105** folder and add the appropriately named classes to them



Open up each class file and create the class structure. Add the standard header to the file. The template for this header is available at:

**You’ll have to add the proper Javadoc documentation** as well as the proper code to solve the problem. You will have to add instance variables, constants, method stubs, and code to get this class defined properly. Read the problem carefully to understand what the problem is and how you will solve it.

You will need to add test code to **** to test the **** source code.

Once you’ve written your code run the code by right clicking on **** in the file explorer and selecting **Run** from the context menu or using the debug tool from the **Activity Bar**. Examine the output. Does it do what you want? If not, how can you modify the code to do what you want?

Running Unit Tests

Don’t forget to run unit tests before submitting your assignment.

**YOUR UNIT TESTS MUST PASS BEFORE SUBMITTING THE FILE OR YOU GET NOTHING (Reference to the original Charlie and the Chocolate Factory).**

UML Diagram for the Project

Create a UML diagram that show both the **Program** and **IntegerPairs** class (and dependency relationship). For the **IntegerPairs** class, include the class name, instance variables, and public methods. Any private methods you choose to create do not need to be show. Export the UML project as an XMI document and place in the root folder of the java project (same folder as the pom.xml). Do not zip / compress this file.

Submitting Your Assignment

Follow the standard instructions for submitting a Java assignment: [How to Submit Assignments]( Once you have submitted your assignment, it is a good idea to check the branches section of GitHub a few minutes later to see if your unit tests passed on the build server (green checkmark is good, red X is bad)