3. Tricky Arithmetics 1. In Java, write the following method: public static int
ID: 3871444 • Letter: 3
Question
3. Tricky Arithmetics 1. In Java, write the following method: public static int decToBin(int decimal) The method accepts a decimal integer and returns its binary equivalent. (You have to do the conversion mathematically like we did in class. You can not use strings, vectors, arrays and the like.) 2. Why is writing the above method utterly meaningless, and it can be argued that the assignment is not very well worded? 3. Given: int num=-500; System.out.print(num+ "n"+(byte) num); What is printed? Given what you have learned in class about the number representation including the two's complement, how can you explain the output? Put the answers in the comments in your file.Explanation / Answer
//Create a class Decimal to Int
1)
import java.util.Scanner;
public class DecimalToInt
{
//function which takes a decimal number as input and return
//the binary equivalent of the decimal number
public static int decToBin(int decimal)
{
//initialise two variables
int rem = 0; // holds the remainder of the number when divided by 2
int pro = 0, pro_count = 0; // count's the number of 1's coming in the remainder
int count = 0; //counts the initial number of zero's
while(decimal > 0)
{
rem = decimal % 2;
pro = pro * 10 + rem;
if(pro != 0)
{
pro_count ++;
}
if(pro ==0 && pro_count == 0)
count++; //holds the number of zero's coming in the remainders initially before the first "1"
decimal = decimal / 2;
}
if (decimal == 0)
{
while(count > 0) //the number of zero's initially in remainder, multiply by the number of that much 10's
{
pro = pro * 10;
count--;
}
}
return pro;
}
//call the main function
public static void main(String[] args)
{
// user will enter the input decimal number from console
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
//create object of the DecimalToInt class to call decToBin function
DecimalToInt obj = new DecimalToInt();
System.out.print("Binary representation of "+ String.valueOf(input)+" is :"+String.valueOf(obj.decToBin(input)));
}
}
2) Writing the above method is utterly meaningless as we can make use of built in methods available in java like Integer.toBinaryString(int num); which takes a number as input and returns the binary format of the number. Apart from that, the above method requires the return type of decToBin to be int. Having the manipulations in the function decToBin in the above program in int requires a number of conditions to be met along with introduction of lot of temporary variables making the program chaotic. We need to have a count on the number of zero's obtained initially before the first 1 is obtained in our variable rem. Having the count helps to multiply variable pro, by that many 10's to get the value. This problem will not arise if we have return type of decToBin function as String or Void. So, the assignment is not well worded.
3)
int num = -500;
System.out.print(num +" "+(byte)num);
Output:-
500
12
Java's int datatype is a 32-bit signed 2's complement integer with values in range (-2,147, 483,648 to +2,147,483,647) . As -500 falls in range of these numbers, so the first output is -500.
While byte data type is a 8-bit signed two's complement integer. And if we represent -500 in 2's complement form. It is represented as 111000001100, counting of the bits comes to 12, hence the second output is 12.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.