Write a program that will convert Decimal (Base 10) number to Its cor.docx
1. Write a program that will convert Decimal (Base 10) number to
Its corresponding Binary (Base 2) value, and converts a Binary
(Base 2) value to Its corresponding Decimal (Base 10) value.
The tricky part to this one is that you should be able to Input
either value type (Binary or Decimal) and the program should
automatically know what to convert It to. Unless otherwise
Indicated there should be no prompt indicating or asking for the
convention type. You are required to write two functions, one to
convert decimal to binary and one to convert binary to decimal
(10 marks each). The program should Indicate to the user the
convention type chosen and the correct conversion value.
Develop a function that will convert any base value to another
base value, for example Base 10 to Base 3. This is the only time
a user con be prompted for the base conversion type. If the
input looks like it is con be either binary or decimal format -e.g.
1100 then prompt the user is It Binary to Dermal or Decimal to
Binary - there should be an appropriate messages to the user
indicating the ambiguity However we must note that on binary
values should be inputted and outputted In four bit format There
will be an additional 10% bonus If you can convert negative
numbers using the Two's complementary approach Two's
complement: For any number x, negating its binary
representation and adding 1 yields binary representation of -x.
This means that:
Solution
import java.util.Scanner; public class baseconverterr { public
static void main(String[] args) { // Read the conversion
choice from the user System.out.println( "Choose 1 or 2 or
3:"); System.out.println( "1: conversion from base 10 to
2. base 2 "); System.out.println( "2: conversion from base 10
to base 8"); System.out.println("3: conversion from base
10 to base 16"); // do you want 1, 2 , or 3? you have your
choice Scanner in = new Scanner(System.in); int
choice = in.nextInt(); String string = in.nextLine(); //
Read in the number to be converted and do the conversion
String output= ""; System.out.println("Please enter the
number to be converted:"); int input = in.nextInt(); if
(choice == 1) // if the user chooses choice #1, it will
convert from base 10 to base 2 output =
Integer.toString(input, 2); else if (choice == 2)
output = Integer.toString(input, 8); // if the user chooses
choice #2, it will convert from base 10 to base of 8 else if
(choice == 3) output = Integer.toString(input, 16); // if
the user chooses choice #3, it will convert from base 10 to base
16 else System.out.println( "invalid entry"); //
everything else, it is invalid System.out.println( "final
output=" + output); // this prints the final output.