Question: 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)
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.java as 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?
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
