Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a java program which: 1. Asks the user to enter a binary number ,example:

ID: 3801923 • Letter: W

Question

Write a java program which:

1. Asks the user to enter a binary number ,example: 1011011

2. Validates that the entry is a binary number. Nothing but a binary number should be allowed.

3. The program converts and displays the binary number to its base 10 equivalent, Example 1112 = 710

4. The user must be asked if he/she wants to continue entering numbers or quit.

5. Keeps a record in a txt file named outDataFile.txt with the history of all numbers entered and the associated results, in the following format:

*no using break statements to exit loops

Explanation / Answer

code:
package test;
import java.util.*;
public class binarydecimal {
public static void main(String[] args) {
long binary1,bin,decimal1=0,i=1,remainder;
boolean decision = false;
Scanner s = new Scanner(System.in);
// Running the loop if the user wants to continue with the program
while(!decision)
{
System.out.println("Enter any Binary value:");
binary1 = s.nextLong();
// Call to the method IsBinary to check whether the given input is binary or not,if yes it continues
boolean result = IsBinary(binary1);
if(result == true)
{
bin = binary1;
//Loop to convert the binary value into the decimal value
while(binary1 != 0)
{
remainder = binary1%10;
decimal1 = decimal1 + (remainder * i);
i = i * 2;
binary1 = binary1/10;
}
System.out.println("Decimal value of " + bin +" is: " + decimal1 );
}
else
{
System.out.println("Enter binary value!!");
}
//Asking if the user is still intrested to give another binary value
Scanner ss = new Scanner(System.in);
System.out.println("Do you wish to continue?(yes or no):");
String st = ss.nextLine();
if(st.equals("no"))
{
decision = true;
}
}
}
//Method to check whether the entered value by the user is binary or not
public static boolean IsBinary(long num)
{
long input = num;
while(input != 0)
{
if(input%10 >1)
{
return false;
}
input = input/10;
}
return true;
}
}

output:
Enter any Binary value:
110100101
Decimal value of 110100101 is: 421
Do you wish to continue?(yes or no):
yes
Enter any Binary value:
100110100111
Decimal value of 100110100111 is: 1265573
Do you wish to continue?(yes or no):
yes
Enter any Binary value:
121
Enter binary value!!
Do you wish to continue?(yes or no):
no

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote