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

Using Eclipse, create a Main class with the regular main method. In the class, t

ID: 3536181 • Letter: U

Question

Using Eclipse, create a Main class with the regular main method.

In the class, there should be a method that converts a binary

number represented as a string into a decimal integer. The method

has the following signature:


public static int binaryToDecimal(String

binaryString)


For example, the binary string 1001 is 9 (1 x 2^3 + 0 x 2^2 + 0

x 2^1 + 1 x 2^0). So,

binaryToDecimal(“1001â€) returns 9.

Note that Integer.parseInt(“1001â€,

2) parses a binary string to a decimal value, but you cannot use

that method in this exercise.



The main method should prompt the user for a binary number and

print out the corresponding decimal integer. Use javadoc to

document to program.


Note: Javadoc must be used, or no points awarded


Explanation / Answer

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

String binary = getBinaryString();

while (!isBinary(binary)){

System.out.println("Input is not a binary String");

binary = getBinaryString();

}

System.out.println("Decimal is " + binarytodecimal(binary));

}

// Prompt user for Binary String input

private static String getBinaryString() {

Scanner scan = new Scanner(System.in);

System.out.print("Enter the binary string: ");

String input = scan.nextLine();

return input;

}

//Check to ensure it is a binary String

private static boolean isBinary(String binaryString){

for(int i = 0; i < binaryString.length(); i++){

char c = binaryString.charAt(i);

if(c != '1' && c != '0'){

return false;

}

}

return true;

}

//Change from binary to decimal

private static int binarytodecimal(String binaryString){

int decimal = Integer.parseInt(binaryString, 2);

return decimal;

}

}

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