Question: Problem Description You are to write a program that will take one string of four positive integers (e.g. 0 1 1 2 or 0 1

Problem Description

You are to write a program that will take one string of four positive integers (e.g. "0 1 1 2" or "0 1 1 0") and will test to see if those integers can form a binary number (all "0"s and "1"s). This will be implemented by the IntegerBinary object. This object will have a constructor that takes no arguments and the following two methods:

  • public void setInputString(String str) - This method will take a string as an argument and then store it in an instance variable. Then it should extract the four digits in that string. For example if the string entered is "0 1 1 2" then the four integers extracted are 0, 1, 1 and 2.
  • public boolean isBinary() - This will return true when all the digits making up the string are either "0" or "1" otherwise return false.

Hints:

  • You can use a Scanner on a string you give it as well as System.in. It works the same way but the constructor takes a string rather than System.in. Like this: Scanner consolReader = new Scanner(str); where str is a String variable. Then you can read the integers in the string str using the method nextInt().
  • After you are done with the scanner you can call {your scanner variable name}.close(); to get rid of the warning.

Getting Started

Using the techniques shown on the web page titled "How to Start Every Project in this Class" create a source file called IntegerBinary.javaas well as a file called Program.java.

Open up the IntegerBinary.java file and replace the code with the code contained in the box below:

package edu.sbcc.cs105; import java.util.*; public class IntegerBinary { public void setInputString(String str) { } } 

You'll notice that only one of the methods are implemented. Use the problem description to figure out which methods and instance variables need to be added to the source file.

Replace the code in Program.java with the code in the grey box below: This is where your test code will go.

package edu.sbcc.cs105; public class Program { public static void main(String[] args) { // TDDO: test the IntegerBinary object here } }

Your Program.java should contain code to test your IntegerBinary object - don't only run unit test, this test code will also be graded. Your test strings should be:

  • "0 1 1 0". Should return true.
  • "1 2 3 4". Should return false.
  • "1 1 1 1". Should return true.

Once you've written your code run the code by single clicking on Program.java in the package explorer and selecting Run->Run from the menu or using the keyboard shortcut. Examine the output. Does it do what you want? If not, how can you modify the code to do what you want?

Using the Unit Test

Next, we are going to use the unit test to test your code. You are supposed to use this unit test after you have written (and passed) the test code that you have written in Program.java.

Create the package by selecting File -> New -> Package in Eclipse. The name of the new package is unittest.cs105. Inside this package will be the unit test that I have written. Download the unit test from the module on canvas. It should be called IntegerBinaryTester.java and is located right underneath this assignment. Once downloaded, single-click on the unittest.cs105 package and then right-click and select Import... Unlike other imports the import source this time is File System. Navigate to the directory where you downloaded IntegerBinaryTester.java and select it. You should see a list of things to import appear. Check the checkbox next to IntegerBinaryTester.java and press Finish .IntegerBinaryTester.java should appear in the unittest.cs105 package.


Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!