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

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

ID: 3810297 • 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. Can you we re write this program in java following these steps? 1. No infinite loops, examples include: a. for(;;) b. while(1) c. while(true) d. do{//code}while(1); 2. No break statements to exit loops , let me know. Thanks. i need the code done in the programming language java. please, no infinite loops

Explanation / Answer

Here is the code for you:

import java.io.*;
import java.util.*;
import java.lang.Math;
public class BinaryToDecimalConversion
{
    public static void main (String [] args) throws FileNotFoundException
    {
       Scanner keyIn = new Scanner(System.in);
       String again;
       PrintWriter pw = new PrintWriter(new File("BinaryToDecimalConversionLog.txt"));
       //int response;
       // program until user decides to quit.
       do
       {
          System.out.print("Enter a binary number:");
          String binary = keyIn.next();
          System.out.println("");
          if(isValid(binary))
          {
             System.out.println(binary + " converted into a decimal is: " + binaryToDecimal(binary));
             pw.write(binary + " converted into a decimal is: " + binaryToDecimal(binary) + " ");
          }  
          else
              System.out.println("Invalid binary number entered.");  
          System.out.print("Do you want to convert another number (Y)es/(N)o: ");
          again = keyIn.next();
       }while(again.equals("Y") || again.equals("y"));
       pw.close();      
    }
    //method to convert from binary to decimal
    public static int binaryToDecimal(String a)
    {
       double j = 0;
       for(int i = 0; i<a.length(); i++)
       {
          if(a.charAt(i)== '1')
              j=j+ Math.pow(2, a.length()-1-i);
       }
       return (int) j;
    }
    //Validates the binary number
    public static boolean isValid(String binary)
    {
       for(int i = 0; i < binary.length(); i++)
           if(binary.charAt(i) != '0' && binary.charAt(i) != '1')
               return false;
       return true;      
    }
}

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