Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.Scanner; public class Binary { public static int binaryToDecimal(String text) { // handle minus sign at the front. int sign = 1; if (text.charAt(0)

image text in transcribed 
import java.util.Scanner; public class Binary { 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 = 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)  

image text in transcribed

File Binaryiava contains 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 00110, 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 char findoperatorSymbol(String text) returns the operator symbol in text, which is either the character or the character '-. For example, findoperatorSymbol("1811-18018") returns Function string getFirstNumber (String text) returns the first binary number in text. For example, getFirstNumber( 1011-10810") returns "1011 Function String getsecondNumber(String text) returns the second binary number in text. For example, getSecondNumber("1011-10010) retuns "10010" Function String computeBinaryoperation(String text) returns the result of the operation described by text, as a binary number. For example, getsecondNumber("1011-10810") returns "-111" Hints: The main goa of this task is to get vou to write unction computeBinaryoperation The other our unctions essentia v solve smaller tasks that unction computeBinaryoperation needs to per orm. . 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 getFirstlumber 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+8101 Error: invalid input, please try again Please enter your input, or q to quit: 101+100010 operator position3 operation -+ the first number is 101

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What was your body language? Your facial expression?

Answered: 1 week ago