Computer Applications
Write a menu driven program using a method Number() to perform the following tasks:
- Accept a number from the user and display it in its Binary Equivalents.
For example:
Sample Input: (21)10
Sample Output: (10101)2 - Accept a binary number from the user and display it in its Decimal Equivalents.
For example:
Sample Input: (11101)2
Sample Output: (29)10
Java
User Defined Methods
34 Likes
Answer
import java.util.Scanner;
public class KboatBinary
{
long decimal2Binary(int n) {
String str = "";
int t = n;
while (t > 0) {
int d = t % 2;
str = d + str;
t /= 2;
}
long b = Long.parseLong(str);
return b;
}
long binary2Decimal(long n) {
long decimal = 0;
int base = 1;
long t = n;
while (t > 0) {
int d = (int)(t % 10);
decimal += d * base;
base *= 2;
t /= 10;
}
return decimal;
}
void number() {
Scanner in = new Scanner(System.in);
System.out.println("Enter 1 to display number in Binary Equivalent");
System.out.println("Enter 2 to display number in Decimal Equivalent");
System.out.print("Enter your choice: ");
int c = in.nextInt();
switch (c) {
case 1:
System.out.print("Enter a decimal number: ");
int num = in.nextInt();
long binNum = decimal2Binary(num);
System.out.println("Binary Equivalent");
System.out.println(binNum);
break;
case 2:
System.out.print("Enter a binary number: ");
long ipNum = in.nextLong();
long decNum = binary2Decimal(ipNum);
System.out.println("Decimal Equivalent");
System.out.println(decNum);
break;
default:
System.out.println("Incorrect Choice");
break;
}
}
}
Variable Description Table
Program Explanation
Output


Answered By
7 Likes
Related Questions
Design a class to overload a method series( ) as follows:
- double series(double n) with one double argument and returns the sum of the series.
sum = (1/1) + (1/2) + (1/3) + ………. + (1/n) - double series(double a, double n) with two double arguments and returns the sum of the series.
sum = (1/a2) + (4/a5) + (7/a8) + (10/a11) + ………. to n terms
- double series(double n) with one double argument and returns the sum of the series.
Design a class to overload the method display(…..) as follows:
- void display(int num) — checks and prints whether the number is a perfect square or not.
- void display(String str, char ch) — checks and prints if the word str contains the letter ch or not.
- void display(String str) — checks and prints the number of special characters present in the word str.
Write a suitable main( ) function.
Design a class to overload the method display(…..) as follows:
- void display(String str, char ch) — checks whether the word str contains the letter ch at the beginning as well as at the end or not. If present, print 'Special Word' otherwise print 'No special word'.
- void display(String str1, String str2) — checks and prints whether both the words are equal or not.
- void display(String str, int n) — prints the character present at nth position in the word str.
Write a suitable main() method.
Design a class to overload a method volume( ) as follows:
- double volume(double r) — with radius (r) as an argument, returns the volume of sphere using the formula:
V = (4/3) * (22/7) * r * r * r - double volume(double h, double r) — with height(h) and radius(r) as the arguments, returns the volume of a cylinder using the formula:
V = (22/7) * r * r * h - double volume(double 1, double b, double h) — with length(l), breadth(b) and height(h) as the arguments, returns the volume of a cuboid using the formula:
V = l*b*h ⇒ (length * breadth * height)
- double volume(double r) — with radius (r) as an argument, returns the volume of sphere using the formula: