Question: Task 7 (20 pts.) The program task7.java given below is an incomplete program, whose goal is to allow the user to add and subtract binary
Task 7 (20 pts.)
The program task7.java given below is an incomplete program, whose goal is to allow the user to add and subtract binary numbers and see the results in binary. The user enters text such as "010101+100" or "10011-10011110", and the program prints out the result of that operation in binary. The user input can only contain two binary numbers, and one operator symbol. Function checkValid(String text) is already provided for you, and ensures that the user input is valid.
Complete the program, by defining the following five functions, which all take as argument the text that the user enters:
Function int findOperatorPosition(String text) returns the position in text where the operator symbol (+ or -) occurs. For example, findOperatorPosition("1011- 10010") returns 4.
Function int findOperatorSymbol(String text) returns the operator symbol in text, which is either the character '+' or the character '-'. For example, findOperatorSymbol("1011-10010") returns '-'.
Function String getFirstNumber(String text) returns the first binary number in text. For example, getFirstNumber("1011-10010") returns "1011".
Function String getSecondNumber(String text) returns the second binary number in text. For example, getSecondNumber("1011-10010") returns "10010".
Function String computeBinaryOperation(String text) returns the result of the operation described by text, as a binary number. For example, getSecondNumber("1011- 10010") returns "-111".
Hints:
The main goal of this task is to get you to write function computeBinaryOperation. The other four functions essentially solve smaller tasks that function computeBinaryOperation needs to perform. My solutions for getFirstNumber and getSecondNumber call
function findOperatorPosition to figure out which part of text contains the number they want to extract. My solution for computeBinaryOperation goes through these steps:
o It calls functions getFirstNumber and getSecondNumber to get the two binary numbers as strings.
o It calls function binaryToDecimal to convert each binary number to a decimal integer (which is a regular Java int).
o It calls function findOperatorSymbol to find whether the operator is addition or subtraction.
o Depending on the operator, it performs addition or subtraction of the two numbers. o It calls function decimalToBinary to convert the result of the operation into a binary
number.
Also, note that the functions binaryToDecimal and decimalToBinary that are provided to you have been extended to also handle negative numbers.
IMPORTANT: You are NOT allowed to modify in any way the main function.
This is an example run of the complete program:
Please enter your input, or q to quit: hello Error: invalid input, please try again.
Please enter your input, or q to quit: 123+0101 Error: invalid input, please try again.
Please enter your input, or q to quit: 101+100010 operator position = 3 operation = + the first number is 101
the second number is 100010 result = 100111
Please enter your input, or q to quit: 101-100010 operator position = 3 operation = - the first number is 101
the second number is 100010 result = -11101
Please enter your input, or q to quit: 100010-101 operator position = 6 operation = - the first number is 100010
the second number is 101 result = 11101
Please enter your input, or q to quit: q Exiting...
Task7.java
import java.util.Scanner;
public class task8 { public static int binaryToDecimal(String text) { // handle minus sign at the front. int sign = 1; if (text.charAt(0) == '-') { sign = -1;
text = text.substring(1); }
int result = 0; String digits = "01"; for (int i = 0; i < text.length(); i++) { String c = text.substring(i, i+1); int digit = digits.indexOf(c); if (digit == -1)
{ System.out.printf("Error: invalid binary number %s, exiting... ", text); System.exit(0);
} int power = (int) (Math.pow(2, text.length() - i - 1)); result = result + digit * power;
}
// if the first character of text was a minus, then negate the result.
result = sign * result;
return result; }
public static String decimalToBinary(int number) { // handle case where number is negative String start = ""; if (number < 0) { start = "-";
number = -number; }
String result = ""; while(true) { int remainder = number % 2; String digit = Integer.toString(remainder); result = digit + result; number = number / 2; if (number == 0) { break; }
}
// if number is negative, put a minus sign in front of the result. result = start + result; return result;
}
public static boolean checkValid(String text) { // should have nonzero length if (text.length() == 0) { return false; }
// should only have characters 0, 1, +, -. int counter = 0; // will count occurrences of + and - characters for (int i = 0; i < text.length(); i++)
{ char c = text.charAt(i); if ("01+-".indexOf(c) < 0) { return false; }
if ("+-".indexOf(c) >= 0) { counter++; }
}
// should only have one occurrence of a + or - character if (counter != 1) { return false; }
// the operator (+ or - character) should not be at the beginning // or end of the string char start = text.charAt(0); char end = text.charAt(text.length() - 1);
if ("01".indexOf(start) < 0) { return false; }
if ("01".indexOf(end) < 0) { return false; }
return true; }
public static int findOperatorPosition(String text) { } public static char findOperatorSymbol(String text) { } public static String getFirstNumber(String text) { } public static String getSecondNumber(String text) { } public static String computeBinaryOperation(String text) { return result;
}
public static void main(String[] args) { Scanner in = new Scanner(System.in);
while (true) { System.out.printf("Please enter your input, or q to quit: "); String text = in.nextLine(); if (text.toLowerCase().equals("q")) { break; }
if (checkValid(text) == false) { System.out.printf("Error: invalid input, please try again. "); continue; }
int position = findOperatorPosition(text); char symbol = findOperatorSymbol(text); String first = getFirstNumber(text); String second = getSecondNumber(text); String result = computeBinaryOperation(text); System.out.printf("operator position = %d ", position); System.out.printf("operation = %c ", symbol); System.out.printf("the first number is %s ", first); System.out.printf("the second number is %s ", second); System.out.printf("result = %s ", result); }
System.out.println("Exiting..."); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
