SHARE
TWEET

Untitled

a guest Nov 14th, 2019 116 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. public class Lab04 {
  3.     public static void main(String[] args) {
  4.         String d_s = new String();
  5.         String b_s = new String();
  6.        
  7.        
  8.         d_s = decimalTobinary(args[0]);// reads the first command line argument
  9.         b_s = binaryTodecimal(args[1]);// reads the second command line argument
  10.        
  11.        
  12.         System.out.printf("%n%s%s%s%n%s",
  13.         "The conversion of positive decimal number "
  14.         , args[0], " to unsigned binary is", d_s);
  15.         System.out.printf("%n%s%s%s%n%s",
  16.         "The conversion of unsigned binary number "
  17.         , args[1], " to positive decimal is", b_s);
  18.        
  19.     }//end main
  20.    
  21.     public static String decimalTobinary(String decimal){
  22.        
  23.         int decRem = Integer.parseInt(decimal);// converts the string from the first command line argument to an Int
  24.         String temp = "";// write the binary number here
  25.         while(decRem > 0) {
  26.             temp = decRem % 2 + temp;
  27.             decRem = decRem / 2;
  28.            
  29.         }
  30.         return temp;
  31.        
  32.     }// end decimal to binary
  33.  
  34.     public static String binaryTodecimal(String binary) {
  35.        
  36.         int binNum = Integer.parseInt(binary);// converts the string fromt the second command line argument to Int
  37.         int temp = 0;
  38.         int rem = 0;
  39.         int num = 1;
  40.         while (binNum > 0) {
  41.             rem = binNum % 10;
  42.             temp = temp + rem * num;
  43.             num = num * 2;
  44.             binNum = binNum / 10;
  45.            
  46.            
  47.            
  48.         }
  49.         String binRem = Integer.toString(temp);
  50.         return binRem;
  51.     }// end binary to decimal
  52.  
  53. }// end class
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top