I308 C307 Data/Information Representation

I308 C307 Homework 1

Due Date: Tuesday, January 18, 2022.

Make sure to complete Lab 1 before doing the homework.

This homework is made of two parts. In the first part, we will continue the implementation of the Converter class started in the lab and write a conversion that goes from binary to decimal. The second part you will do some conversions by hand and verify the results using the program.

Ex. 1. Conversion from Binary

We will input a binary number as a string, populate the array based on its content, and then perform the conversion to decimal.

a. String to Binary

Add a method setBinary that receives a string parameter, presumed to be made of 1s and 0s, and convert set the values of the binary array based on it. For this, the size of the array will be equal to the length of the string. Then for each character in the string, check if the character is the digit '1' and if so, set the corresponding element in the binary array to 1, otherwise to 0. You can use a for each loop to traverse the string, and you'll need an index in the binary array that starts from the size-1 and is decreased after each iteration. This way, the least significant digit in the string will end up in binary[0].

b. Binary to decimal

Add another method that converts the binary array into a decimal number, store it in the attribute decimal, and then output the data in the object using the method written in the lab.

To convert the binary array to decimal, declare the resulting value as 0 and use an index i to traverse the binary array in reverse order starting from size-1 and going down to o (similar to the loop in the output method), where the size is the one stored in the attribute and not the actual length of the array. For each iteration, multiply the value by 2, and then add the element binary[i] to it. At the end, store this value in the decimal attribute and output the object.

c. Interface

In the main, add a third option to the interface to convert a number from binary to decimal. For this, use the function next from the Scanner class to input a string, call the method setBinary from the converter object with this string, and then call the converting method to convert it to decimal and output the result. Note that you will likely need to call scan.nextLine(); before inputting the binary number because otherwise the function next() will simply return a string containing the end of line after the last integer that was input.

Ex. 2. Written Part

Perform the following conversions by hand then use the program to verify the results. You will need to show all the steps you took in the conversion. You don't need to show the results of the program, just mention if it was the same result or not.

For this part, you can type it directly as a text or word file, or you can do it on paper and scan it or take a photo of it.

a. Convert the following numbers to binary:

292, 35, 195, 201

b. Convert the following numbers to decimal:

10011001 11101 1000110 1101101

Homework Submission

Upload the files Converter.java and any files needed for Exercise 2, to Canvas, Assignments, Homework 1.