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

Hello, Please help write program in JAVA that asks the user to enter a binary nu

ID: 3803559 • Letter: H

Question

Hello,

Please help write program in JAVA that asks the user to enter a binary number ,example: 1011011

Program should do the following:

1) Validates that the entry is a binary number. NOTHING, BUT BINARY NUMBER SHOULD BE ALLOWED.

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

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

4) 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:

You entered 111

Its base 10 equivalent is 7

You entered 1010101

Its base 10 equivalent is 85


5) Code should be commented

GENERAL RESTRICTIONS:

No infinite loops

No break statements to exit loops

You entered 111

Its base 10 equivalent is 7

You entered 1010101

Its base 10 equivalent is 85

Explanation / Answer

BinaryToDecimalTest.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class BinaryToDecimalTest {

  

public static void main(String a[]) throws FileNotFoundException{
Scanner scan = new Scanner(System.in);
File file = new File("D:\outDataFile.txt");
PrintWriter pw = new PrintWriter(file);
  
char ch = 'y';
while(ch=='y' || ch=='Y'){
System.out.println("Enter the binary number: ");
String binary = scan.next();
if(validateInput(binary)) {
   System.out.println("You entered: "+binary);
   String s = "Its base 10 equivalent is "+convertBinaryToBase10(Integer.parseInt(binary));
   System.out.println(s);
   pw.write("You entered: "+binary+" ");
   pw.write(s+" ");
}
else{
   System.out.println("Invalid Input. Input must be binary number.");
}
System.out.println("Do you want to continue?(y or n): ");
ch = scan.next().charAt(0);
}
pw.flush();
pw.close();
}
public static int convertBinaryToBase10(int binary){
   int decimal = 0;
       int power = 0;
       while(true){
           if(binary == 0){
               break;
           } else {
               int tmp = binary%10;
               decimal += tmp*Math.pow(2, power);
               binary = binary/10;
               power++;
           }
       }
       return decimal;
}
public static boolean validateInput(String binary) {
   for(int i=0; i<binary.length(); i++){
       if(binary.charAt(i) != '0' && binary.charAt(i) !='1' ) {
           return false;
       }
   }
   return true;
}
}

Output:

Enter the binary number:
111
You entered: 111
Its base 10 equivalent is 7
Do you want to continue?(y or n):
y
Enter the binary number:
1010101
You entered: 1010101
Its base 10 equivalent is 85
Do you want to continue?(y or n):
y
Enter the binary number:
1112
Invalid Input. Input must be binary number.
Do you want to continue?(y or n):
n

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