Question

In: Computer Science

Write a JAVA program called Convertor that has two methods. The first one converts an input...

  • Write a JAVA program called Convertor that has two methods. The first one converts an input binary into its equivalent decimal number. The second method converts a decimal number into its equivalent binary.​
  • binary2Decimal, decimal2Binary are the names for those methods.​
  • binary2Decimal receives a binary number as a string and returns a decimal number as an integer. And vice versa for the decimal2Binary method.​

Solutions

Expert Solution

input code:

output:

code:

import java.util.*;
import java.lang.*;
class Convertor
{
/*make method for getting a decimal*/
public static String decimal2Binary(int n)
{
/*declare the String*/
String str1="";
/*take a loop*/
while(n>0)
{
/*find reminder*/
int r=n%2;
n=n/2;
/*add into binary*/
str1=(char)(r+'0')+str1;
}
/*return binary*/
return str1;
}
/*make method for gettomg decimal*/
public static int binary2Decimal(String bin)
{
/*declare the variable*/
int dec=0;
/*take a loop*/
for(int i=0;i<bin.length();i++)
{
/*calculate decimal*/
dec+=Character.getNumericValue(bin.charAt(i))*Math.pow(2,bin.length()-i-1);
}
/*return decimal*/
return dec;
}
  
}
/*driver class*/
public class Main
{
public static void main(String[] args)
{
/*call a function*/
System.out.println("Output of 110 to decimal= "+Convertor.binary2Decimal("110"));
System.out.println("Output of 5 to binary= "+Convertor.decimal2Binary(5));
}
}


Related Solutions

Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
Write a program in java that uses methods to input data for calculation, calculate, and display...
Write a program in java that uses methods to input data for calculation, calculate, and display the results of the calculations. That is, there are at least three methods. The problem is to write a program that calculates the area of a rectangle. This action should be repeatable.
Write a program in Java that will take as input two sets A and B, and...
Write a program in Java that will take as input two sets A and B, and returns a list of all functions from A to B.
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread prints all capital letters 'A' to'Z'. The other thread prints all odd numbers from 1 to 21.
Java Write a program that will only accept input from a file provided as the first...
Java Write a program that will only accept input from a file provided as the first command line argument. If no file is given or the file cannot be opened, simply print “Error opening file!” and stop executing. A valid input file should contain two lines. The first line is any valid string, and the second line should contain a single integer. The program should then print the single character from the string provided as the first line of input...
Write a JAVA program that compares two strings input to see if they are the same?
Write a JAVA program that compares two strings input to see if they are the same?
Write a Java program that implements the Depth-First Search (DFS) algorithm. Input format: This is a...
Write a Java program that implements the Depth-First Search (DFS) algorithm. Input format: This is a sample input from a user. 3 2 0 1 1 2 The first line (= 3 in the example) indicates that there are three vertices in the graph. You can assume that the first vertex starts from the number 0. The second line (= 2 in the example) represents the number of edges, and following two lines are the edge information. This is the...
1. Write a Java program that asks the user to input a positive integer n first,...
1. Write a Java program that asks the user to input a positive integer n first, then create an array of size n. Fill n random integers between 5 and 555, inclusively, into the created array. Output the sum of all the integers in the array and the average of all the integers in the array. 2 .Find the output of the following Java program and explain your answer
Program Specifications The built-in Java Math methods make some calculations much easier. Write a program called...
Program Specifications The built-in Java Math methods make some calculations much easier. Write a program called "DoTheMath" that accepts as input three floating-point numbers x, y, and z (define them as double) and outputs several calculations: x to the power of y x to the power of (y to the power of z) The absolute value of x The square root of (x*y to the power of z). Sample Run: Enter the values for x, y, z: -3.7 -3 5...
Please write a java program that has the following methods in it: (preferably in order)   a...
Please write a java program that has the following methods in it: (preferably in order)   a method to read in the name of a University and pass it back a method to read in the number of students enrolled and pass it back a method to calculate the tuition as 20000 times the number of students and pass it back a method print the name of the University, the number of students enrolled, and the total tuition Design Notes: The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT