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

This is in Java. The goal of this program is to take binary numbers in groups of

ID: 3881400 • Letter: T

Question

This is in Java. The goal of this program is to take binary numbers in groups of 8 and return there decimal value. "processUsernumbers" should be called in the main. This method should collect user input into a string and then call "Validatenumber" to check if the strings are of length 8 and consists of only 0's and 1's for example "10110011". Validatenumber must return a boolean. "processUsernumbers" should then call the method "Evaluatenumber" on the validated numbers. Evalutenumber should take a validated number and calculate its decimal value; assume the binary number is unsigned. The output for the program should look like this:


string: 00110000
status: valid
decimal value: 48

string: 1111
status: invalid

string: 12341234
status: invalid

string: 999
status: invalid

As of right now my Validatenumber method is not working and is only returning true and only doing so once. How do I fix this? Also I need help completing Evalutenumber. Please show me how to complete Validatenumber and Evalutenumber and achieve the output above. Here is my current output followed by my current program.

h1 First enter the amount of numbers you plan to test.(consider array length) Want to add? Enter 0 index of array: 101010101 Want to add? Enter 1 index of array: yes Process finished with exit code 0

Explanation / Answer

/*I used mostly your syntax and at some places i modified

* So please if you are not able to understand at any point please let me know in comment section

* To convert binary to decimal, the explaination is given in function itself

* Sample Input/Output:

2

enter string:

10101010

status:Valid

Decimal Value: 170

enter string:

1111

status:inValid*/

//Java Program to convert binary to decimal

import java.util.*; // ArrayList, Random

import java.util.Scanner;

public class Binary {

//To store instance to Scanner

private Scanner keyboard;

Boolean Validate;

public Binary () {

//Instantiating Scanner

keyboard = new Scanner(System.in);

}

public static void PrintInfo(){

System.out.println("hi");

}

public void processUsernumbers(){

System.out.println("First enter the amount of numbers you plan to test.(consider array length)");

int n=keyboard.nextInt();

String[] userInput = new String[n];

int[] numValues=new int[n];

int i=0;

//It will iterate until you enters n valid strings

// you can see the value of i in Increamenting only when you enters valid stirng

while(i<n)

{

System.out.println("enter string:");

String number=keyboard.next();

boolean isValid=Validatenumber(number);

//If string is valid then only going to do calculation

if(isValid)

{

System.out.println("status:Valid");

//Calling method to find decimal value of string

int intNum=Evaluatenumber(number);

//printing the values

System.out.println("Decimal Value: "+intNum);

//Storing string to array

userInput[i]=number;

//Storing numerical value to the array

numValues[i]=intNum;

i++;

}

else //To print invalid status

{

System.out.println("status:inValid");

}

}

//Printing final arrays

/*

for(i=0;i<n;i++)

{

System.out.print(userInput[i]+" "+numValues[i]);

}*/

}

public boolean Validatenumber(String number) {

//Checking if length of string is 8 and string contains only 0 and 1

if (number.length() == 8 && number.matches("[0-1]+"))

return true;

else

return false;

}

public int Evaluatenumber(String number){

/*To convert binary to decimal:

* Traverse string from back and

* multiply current bit with 2 to the power c as c is 0 in starting and increament one-by-one

* For more understanding please see how to convert binary to decimal

* ex:

* 1 0 1 0

* 2^3 2^2 2^1 2^0

* 1*8 + 0*4+ 1*2 +0*1

* 10

* */

  

int num=0;

int p=2;

int c=0;

for(int i=number.length()-1;i>=0;i--)

{

//Converting character to decimal as ascii value of '0' is 48 so subtracting 48

int x=(int)number.charAt(i)-48;

num =num+x*(int)Math.pow(p,c);

c++;

}

return num;

}

public static void main(String[] args) {

Binary.PrintInfo();

Binary test = new Binary();

test.processUsernumbers();

}

}

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