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

Project 09 Description For this lab you will write a Java program that manipulat

ID: 3601210 • Letter: P

Question

Project 09 Description

For this lab you will write a Java program that manipulates numbers. The program will ask the user to enter a binary number (or an empty line to quit) and then convert that number from base 2 into base 10 (decimal) and display the result. The program will loop until the user enters an empty line to end the program. See Project 08 for more discussion on binary numbers and their relationship to decimal numbers.

For this assignment you must start with the following "skeleton" of Java code. Import this into your Eclipse workspace and fill in the methods as directed. Feel free to add any methods you find useful, but make sure that you add comments indicating what they do following the form of the rest of the comments in the code.

Project09.java

Converting from Binary to Decimal

To convert from a binary to a decimal representation, your code will need to take into account the place of each digit in the binary string. One algorithm for performing this conversion is:

Start with a value of 0 for your decimal number

Assign a value n to be the power of the leftmost digit in your representation (HINT: this will be the length of the string - 1)

For each digit in your binary representation, working from left to right

Multiply the digit by your 2n

Add that result to your decimal total

Subtract 1 from n

Repeat until all digits have been processed

Here's an example of the number 1011 converted to a decimal number by applying the algorithm above

Start with result = 0, n = 3

At index 3

Multiply 1 by 23 = 8

result = 0 + 8 = 8

n = 2

At index 2

Multiply 0 by 22 = 0

result = 8 + 0 = 8

n = 1

At index 1

Multiply 1 by 21 = 2

result = 8 + 2 = 10

n = 0

At index 0

Multiply 1 by 20 = 1

result = 10 + 1 = 11

n = -1

Our final result is 11

Verify this by using your program from the previous project (or computing it by hand, using the algorithm given in the previous project).

Project 09 Sample Output

This is a sample transcript of what your program should do. Items in bold are user input and should not be put on the screen by your program.  

Converting from Binary to Decimal
Enter a binary value (empty line to quit): 111011
The binary value 111011 is 59 in decimal (base 10).

Enter a binary value (empty line to quit): 1101
The binary value 1101 is 13 in decimal (base 10).

Enter a binary value (empty line to quit): 1011
The binary value 1011 is 11 in decimal (base 10).

Enter a binary value (empty line to quit): 1d231
ERROR - invalid binary value

Enter a binary value (empty line to quit): 10
The binary value 10 is 2 in decimal (base 10).

Enter a binary value (empty line to quit):
Goodbye


Note that your output depends on the choices made by the user. You are required to check that the user has input a valid binary value (i.e. a string consisting only of 1s and 0s) or an empty line. See the project skeleton for more details.

NOTE: For this assignment you will need to use the Character.getNumericValue() method. This method takes a character value such as '2' and converts it to the appropriate integer value (in this case the number 2).

char input='2';
int inputInt = Character.getNumericValue(input); // inputInt now has a value of 2

Explanation / Answer

Given below is the Program09. Since the skeleton code mentioned in the question is not provided, I have written the code new. Please use it methods as appropriate to fill your skeleton code.

To indent code in eclipse, press Ctrl+A and tehn Ctrl+ i

Please don't forget to rate the answer if it helped. Thank you.


import java.util.Scanner;
public class Program09 {
private static boolean isValidBinary(String bin)
{
for(int i = 0; i < bin.length(); i++)
{
char ch = bin.charAt(i);
if(ch != '0' && ch != '1')
return false;
}
return true;
}
private static int binaryToDecimal(String bin)
{
int n = bin.length() - 1;
int decimal = 0;
for(int i = 0; i < bin.length(); i++)
{
char ch = bin.charAt(i);
int digit = Character.getNumericValue(ch);
decimal += digit * Math.pow(2, n);
n--;
}
return decimal;
}
public static void main(String[] args) {
Scanner keybd = new Scanner(System.in);
String bin;
while(true)
{
System.out.print("Enter a binary value (empty line to quit): ");
bin = keybd.nextLine().trim();
if(bin.isEmpty())
break;
if(isValidBinary(bin))
{
int decimal = binaryToDecimal(bin);
System.out.printf("The binary value %s is %d in decimal (base 10). ", bin, decimal);
}
else
System.out.println("ERROR - invalid binary value");
}
System.out.println("Goodbye");
}
}

output

Enter a binary value (empty line to quit): 111011
The binary value 111011 is 59 in decimal (base 10).

Enter a binary value (empty line to quit): 1101
The binary value 1101 is 13 in decimal (base 10).

Enter a binary value (empty line to quit): 1011
The binary value 1011 is 11 in decimal (base 10).

Enter a binary value (empty line to quit): 1d231
ERROR - invalid binary value
Enter a binary value (empty line to quit): 10
The binary value 10 is 2 in decimal (base 10).

Enter a binary value (empty line to quit):
Goodbye