Question

In: Computer Science

Please assist with writing a program that does conversion but cannot use the built-in conversion in...

Please assist with writing a program that does conversion but cannot use the built-in conversion in java.
• Please use switch case
• Please include comments to understand
• Write in Java
• Please use JOptionPane

a) Binary to decimal
b) Decimal to Binary
c) Hex to Decimal
d) Decimal to Hex
e) Binary to Hex
f) Hex to Binary

Solutions

Expert Solution


Given below is the code for the question. Please do rate the answer if it helped. Thank you


import javax.swing.JOptionPane;

public class BaseConversion {
  
   //checks if a given num is valid in the specified base
   private static boolean isValid(int base, String num) {
       char c;
       for(int i = 0; i < num.length(); i++) {
           c = num.charAt(i);
           switch(base) {
           case 2:
               if(c != '0' || c != '1')
                   return false;
           case 10:
               if(c <= '0' || c >= '9')
                   return false;
           case 16:
               if(!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')))
                       return false;
           }
       }
       return true;
   }
   private static String convert(int src, String num, int dest){
       long deci = 0;
       int digit;
       char c;
       int rem;
       String str = "";
      
       //first convert to decimal
       for(int i = 0, j = num.length() - 1; j >= 0; j--, i++){
           c = num.charAt(j);
          
           if(c >= '0' && c <= '9')
               digit = c - '0';
           else if(c >= 'A' && c <= 'F')
               digit = c - 'A'+ 10;
           else if(c >= 'a' && c <= 'f')
               digit = c - 'a' + 10;
           else
               digit = 0;
          
           deci = deci + digit * (int)Math.pow(src, i);
       }
       //from decimal now convert to dest
       while(deci > 0){
           rem = (int) (deci % dest);
           deci /= dest;
          
           if(rem >= 10)
               c = (char)('A' + rem - 10);
           else
               c = (char)('0' + rem);
              
           str = c + str;
       }
      
       if(str.equals(""))
           str = "0";
       return str;
      
   }
   public static void main(String[] args) {
       int src, dest;
       String num;
       String s;
      
       s = JOptionPane.showInputDialog("From which base do you want to convert (2, 10 or 16): ");
       src = Integer.parseInt(s);
      
       s = JOptionPane.showInputDialog("To which base do you want to convert (2, 10 or 16): ");
       dest = Integer.parseInt(s);
      
       s = JOptionPane.showInputDialog("Enter a number in base " + src + ": ");
       num = s;
      
       if(isValid(src, num))
           JOptionPane.showMessageDialog(null, "The number in base " + dest + " is " + convert(src, num, dest));
       else
           JOptionPane.showMessageDialog(null, num + " is not a valid number in base "+ src);
   }
}


Related Solutions

This is for Java programming. Please use ( else if,) when writing the program) Write a...
This is for Java programming. Please use ( else if,) when writing the program) Write a java program to calculate the circumference for the triangle and the square shapes: The circumference for: Triangle = Side1 + Side2 +Sid3 Square = 4 X Side When the program executes, the user is to be presented with 2 choices. Choice 1 to calculate the circumference for the triangle Choice 2 to calculate the circumference for the square Based on the user's selection the...
You can only use built in Lisp functions and you cannot use setq function. Write a...
You can only use built in Lisp functions and you cannot use setq function. Write a function in Lisp called f1 that counts the number of lists in a list. Example: (f1 ‘(a (a b (b c)) c d (e))) returns 2
urgent!!! code in c++ - cannot use vector - please use inheritance -please identify the .h...
urgent!!! code in c++ - cannot use vector - please use inheritance -please identify the .h and .cpp files and add tester program and screenshot of output! -please complete all parts I will upvote thank you!!! Define the following classes to manage the booking of patients in a medical clinic. a) Define a class Date that has the following integer data members: month, day and year. b) Define a class AppointmentTime that has the following data members: day (string), hour...
urgent!!! code in c++ - cannot use vector - please use inheritance -please identify the .h...
urgent!!! code in c++ - cannot use vector - please use inheritance -please identify the .h and .cpp files and add tester program and screenshot of output! -please complete all parts I will upvote thank you!!! Define the following classes to manage the booking of patients in a medical clinic. a) Define a class Date that has the following integer data members: month, day and year. b) Define a class AppointmentTime that has the following data members: day (string), hour...
I'm writing a program that requires that I use the check sum technique with a for...
I'm writing a program that requires that I use the check sum technique with a for loop. A student enters their seven digit ID number. The seventh digit is determined from the other digits by this formula: 7th digit = (1 *(1st digit) + 2 * (2nd digit) + ... + 6 * (6th digit)) %10. The program should prompt users to enter a 7-digit number, and print valid if the actual 7th digit matches the computed 7th digit. Basicaly,...
TEXT ONLY PLEASE (PLEASE NO PDF OR WRITING) C++ CODE Instructions Write a program that implements...
TEXT ONLY PLEASE (PLEASE NO PDF OR WRITING) C++ CODE Instructions Write a program that implements the algorithm given in Example 1 - 3 (Chapter 1), which determines the monthly wages of a salesperson. The instructions for Example 1 - 3have been posted below for your convenience. Example 1 - 3 Every salesperson has a base salary. The salesperson also receives a bonus at the end of each month, based on the following criteria: If the salesperson has been with...
A shopping center project is proposed to the city council. It cannot be built unless a...
A shopping center project is proposed to the city council. It cannot be built unless a zoning change is approved by the council. The planning board must first make a recommendation, for or against the zoning change, to the council. Let A1 = city council approves the zoning change A2 = city council disapproves the change The prior is: Pr(A1) = .7,   Pr(A2) = .3 Let B denote the event of a negative recommendation by the planning board. Past history...
A multiplicative Cobb-Douglas Production Function is writing as Q=AKaLB. We cannot use the Ordinary Least Squares...
A multiplicative Cobb-Douglas Production Function is writing as Q=AKaLB. We cannot use the Ordinary Least Squares method (OLS) in Excel to estimate the above multiplicative Cobb-Douglas Production Function since the independent variables are not linear. Hence, by transforming the above Cobb-Douglas production function into natural logs, we make the independent variables into linear. Then we can use the OLS technique. InK InL InQ 3.6889 3.6889 3.4675 3.6889 4.7875 3.8069 3.6889 5.2983 3.7564 3.6889 5.7683 4.0918 4.382 3.6889 3.5946 4.382 4.382...
PLEASE DO NOT USE EXCEL, as we cannot use excel on our test. The Wagner Company...
PLEASE DO NOT USE EXCEL, as we cannot use excel on our test. The Wagner Company currently uses an injection-molding machine that was purchased 2 years ago. This machine is being depreciated on a straight-line basis, and it has 6-years of remaining life. Its current book value is $2,100, and it can be sold for $2,500 at this time. Thus, the annual depreciation expense is $350 [($2,100/6) = $350 per year]. If the old machine is not replaced, it can...
please use keyboard writing Please give a unique answer Q1/What are the roles of a project...
please use keyboard writing Please give a unique answer Q1/What are the roles of a project sponsor and the approval committee during the different SDLC phases? part 2 Assume the following scenario: A small company needs to develop an information system for the Finance and Accounting Department. As an analyst which process model would you prefer and why?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT