Write a program which: 1. Asks the user to enter a binary number ,example: 10110
ID: 3864357 • Letter: W
Question
Write a 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: please write in the programming language Java.
Explanation / Answer
BinaryToDecimal.java
import java.io.File;
import java.io.FileWriter;
import java.text.DecimalFormat;
import java.util.Scanner;
public class BinaryToDecimal {
public static void main(String[] args) {
//Creating the reference of File and FileWrite classes
File f;
FileWriter fw = null;
//Declaring variables
int binaryNum,temp,decimalNum = 0, power = 0,bnum;
String filePath="D:\outDataFile.txt";
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
boolean bool;
/* This loop continues to execute until
* the user enters a valid binary number
* */
do
{
//Getting the Binary Number entered by the user
System.out.print("Enter the Binary Number :");
binaryNum=sc.nextInt();
bnum=binaryNum;
/* calling the method by passing
* the user entered binary number as argument
*/
bool=isBinaryNumber(binaryNum);
if(!bool)
{
//Displaying the error message
System.out.println("** Invalid.Input Must be Binary number **");
}
}while(bool==false);
//Converting the binary number to decimal
do{
if(binaryNum != 0){
temp = binaryNum%10;
decimalNum += temp*Math.pow(2, power);
binaryNum = binaryNum/10;
power++;}
}while(binaryNum!=0);
//Writing the data to the file
try {
f = new File(filePath);
//If file already exists then write data to the existing file
if(f.exists()) {
fw=new FileWriter(f,true);
fw.write("Binary Number ="+bnum+" Decimal Number ="+decimalNum+" ");
System.out.println("Data Written to File");
fw.close();
}
else
{
//If no file already exists.Create new File
f.createNewFile();
fw=new FileWriter(f);
fw.write("Binary Number ="+bnum+" Decimal Number ="+decimalNum+" ");
System.out.println("Data Written to File");
fw.close();
}
} catch (Exception e) {
System.out.println("Exception "+e);
}
}
//this method will check whether the user entered number is binary or not
private static boolean isBinaryNumber(int binaryNum) {
int digit;
while(binaryNum>0)
{
digit= binaryNum%10;
if(digit!=1 && digit!=0)
{
return false;
}
binaryNum = binaryNum/10;
}
return true;
}
}
_____________________
Output:
Enter the Binary Number :100020
** Invalid.Input Must be Binary number **
Enter the Binary Number :1010101
Data Written to File
____________________
Output2:
Enter the Binary Number :1111
Data Written to File
___________________
outDataFile.txt (We can see this file under D Drive.As we specified the path of the output file as D:\outDataFile.txt )
Binary Number =1010101 Decimal Number =85
Binary Number =1111 Decimal Number =15
____________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.