Here we are trying to create a java project where you will first let user choose what conversion they want to perform.
Allow user to enter value in the required format and then perform the conversion. On the completion of the process print the converted number format.
Note: The project is in progress. If you want to create on your own you can use the template and write code to perform other conversions as well that is missing here.
Controller.java
package NumberSystem;
import java.util.Scanner;
public class Controller {
public static void main(String[] args) {
System.out.println("Enter your choice");
System.out.println("1: To convert decimal to binary ");
System.out.println("2: To convert binary to decimal");
System.out.println("3: To convert ........"); // More to be added soon
Scanner scNum = new Scanner(System.in);
int choice = scNum.nextInt();
//System.out.println("You enetered: "+ i);
switch(choice){
case 1:
System.out.println("Enter decimal Number");
Scanner sc = new Scanner(System.in);
int decNum = sc.nextInt();
DecimalToBinary dtb = new DecimalToBinary();
dtb.decToBinary(decNum);
break;
case 2:
System.out.println("Enter decimal Number");
Scanner sc1 = new Scanner(System.in);
int binNum = sc1.nextInt();
BinaryToDecimal binDec = new BinaryToDecimal();
binDec.binToDec(binNum);
break;
case 3:
break;
default: System.out.println("Wrong choice");
}
}
}
DecimalToBinary.java
package NumberSystem;
public class DecimalToBinary {
public void decToBinary(int dec){
if(dec==0){
System.out.println(0);
return;
}
String binaryString = "";
while(dec>0){
int rem = dec%2;
binaryString = rem + binaryString;
dec = dec/2;
}
System.out.println(binaryString);
}
}
BinaryToDecimal.java
package NumberSystem;
public class BinaryToDecimal {
public void binToDec(int binNum){
int decimalForm = 0;
int power = 0;
if(binNum==0){
System.out.println(0);
return;
}
while(binNum>0){
int tmp = binNum%10;
decimalForm += tmp*Math.pow(2, power);
binNum = binNum/10;
power++;
}
System.out.println(decimalForm);
}
}
Allow user to enter value in the required format and then perform the conversion. On the completion of the process print the converted number format.
Note: The project is in progress. If you want to create on your own you can use the template and write code to perform other conversions as well that is missing here.
Controller.java
package NumberSystem;
import java.util.Scanner;
public class Controller {
public static void main(String[] args) {
System.out.println("Enter your choice");
System.out.println("1: To convert decimal to binary ");
System.out.println("2: To convert binary to decimal");
System.out.println("3: To convert ........"); // More to be added soon
Scanner scNum = new Scanner(System.in);
int choice = scNum.nextInt();
//System.out.println("You enetered: "+ i);
switch(choice){
case 1:
System.out.println("Enter decimal Number");
Scanner sc = new Scanner(System.in);
int decNum = sc.nextInt();
DecimalToBinary dtb = new DecimalToBinary();
dtb.decToBinary(decNum);
break;
case 2:
System.out.println("Enter decimal Number");
Scanner sc1 = new Scanner(System.in);
int binNum = sc1.nextInt();
BinaryToDecimal binDec = new BinaryToDecimal();
binDec.binToDec(binNum);
break;
case 3:
break;
default: System.out.println("Wrong choice");
}
}
}
DecimalToBinary.java
package NumberSystem;
public class DecimalToBinary {
public void decToBinary(int dec){
if(dec==0){
System.out.println(0);
return;
}
String binaryString = "";
while(dec>0){
int rem = dec%2;
binaryString = rem + binaryString;
dec = dec/2;
}
System.out.println(binaryString);
}
}
BinaryToDecimal.java
package NumberSystem;
public class BinaryToDecimal {
public void binToDec(int binNum){
int decimalForm = 0;
int power = 0;
if(binNum==0){
System.out.println(0);
return;
}
while(binNum>0){
int tmp = binNum%10;
decimalForm += tmp*Math.pow(2, power);
binNum = binNum/10;
power++;
}
System.out.println(decimalForm);
}
}
No comments:
Post a Comment