0% found this document useful (0 votes)
2 views15 pages

Change Base Number

The document contains Java code for a utility that reads and validates user input for binary, decimal, and hexadecimal numbers, and performs base conversions between these formats. It includes methods for input validation, conversion logic, and a main class to drive the program with a menu for user interaction. The program utilizes a Scanner for input and BigInteger for handling large numbers during conversions.

Uploaded by

masenwakari79
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views15 pages

Change Base Number

The document contains Java code for a utility that reads and validates user input for binary, decimal, and hexadecimal numbers, and performs base conversions between these formats. It includes methods for input validation, conversion logic, and a main class to drive the program with a menu for user interaction. The program utilizes a Scanner for input and BigInteger for handling large numbers during conversions.

Uploaded by

masenwakari79
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

Scanner;

/**

* Provides methods to read and validate user input.

*/

public class Utility {

/**

* Scanner used to read input from keyboard

*/

Scanner sc = new Scanner([Link]);

/**

* Reads a menu choice within a given range.

* @param min minimum valid choice

* @param max maximum valid choice

* @return valid user choice

*/

public int getChoice(int min, int max) {

while (true) {

try {

[Link]("Enter choice: ");

int choice = [Link]([Link]());


if (choice >= min && choice <= max) {

return choice;

[Link]("Choice must be " + min + " to " + max);

} catch (NumberFormatException e) {

[Link]("Please enter a number!");

/**

* Reads and validates a binary number.

* @return valid binary string

*/

public String getValueBinary() {

while (true) { // repeat until valid input

[Link]("Enter binary number: "); // prompt user

String value = [Link](); // read input

if ([Link]("[0-1]+")) { // check binary format

return value; // return valid binary

[Link]("Binary only includes 0 and 1!");

}
}

/**

* Reads and validates a decimal number.

* @return valid decimal string

*/

public String getValueDecimal() {

while (true) { // repeat until valid input

[Link]("Enter decimal number: "); // prompt user

String value = [Link](); // read input

if ([Link]("[0-9]+")) { // check decimal format

return value; // return valid decimal

[Link]("Decimal only includes 0-9!");

/**

* Reads and validates a hexadecimal number.

* @return valid hexadecimal string

*/

public String getValueHexadecimal() {


while (true) { // repeat until valid input

[Link]("Enter hexadecimal number: "); // prompt user

String value = [Link](); // read input

if ([Link]("[0-9A-F]+")) { // check hex format

return value; // return valid hexadecimal

[Link]("Hexadecimal includes 0-9 and A-F!");

import [Link];

/**

* Handles base conversion between binary, decimal and hexadecimal.

*/

public class ChangeBaseNumber {

/**

* Converts a binary string to a decimal string.

* @param binary binary number as string

* @return decimal number as string

*/

public String convertBinaryToDecimal(String binary) {


// store decimal result

BigInteger decimal = [Link];

// base 2

BigInteger base = new BigInteger("2");

// loop each bit

for (int i = 0; i < [Link](); i++) {

// check bit value

if ([Link](i) == '1') {

// add power of 2

decimal = [Link]([Link]([Link]() - 1 - i)

);

// return decimal result

return [Link]();

/**

* Converts a hexadecimal string to a decimal string.

* @param hexadecimal hexadecimal number as string

* @return decimal number as string


*/

public String convertHexadecimalToDecimal(String hexadecimal) {

// store decimal result

BigInteger decimal = [Link];

// base 16

BigInteger base = new BigInteger("16");

// hex characters

String digits = "0123456789ABCDEF";

// loop each digit

for (int i = 0; i < [Link](); i++) {

// get digit value

int value = [Link]([Link](i));

// add value * 16^position

decimal = [Link](

[Link](value).multiply([Link]([Link]() - 1 - i))

);

// return decimal result

return [Link]();
}

/**

* Converts a decimal string to a binary string.

* @param decimal decimal number as string

* @return binary number as string

*/

public String convertDecimalToBinary(String decimal) {

// decimal value

BigInteger deci = new BigInteger(decimal);

// base 2

BigInteger base = new BigInteger("2");

// binary result

String result = "";

// loop until value is 0

while ([Link]() != 0) {

// add remainder

result = [Link](base).intValue() + result;

// divide by 2

deci = [Link](base);
}

// return binary result

return result;

/**

* Converts a decimal string to a hexadecimal string.

* @param decimal decimal number as string

* @return hexadecimal number as string

*/

public String convertDecimalToHexadecimal(String decimal) {

// decimal value

BigInteger deci = new BigInteger(decimal);

// base 16

BigInteger base = new BigInteger("16");

// hex characters

String digits = "0123456789ABCDEF";

// hex result

String result = "";

// loop until value is 0


while ([Link]() != 0) {

// map digit

result = [Link]([Link](base).intValue()) + result;

// divide by 16

deci = [Link](base);

// return hexadecimal result

return result;

/**

* Converts a binary string to a hexadecimal string.

* @param binary binary number as string

* @return hexadecimal number as string

*/

public String convertBinaryToHexadecimal(String binary) {

// binary → decimal → hex

return convertDecimalToHexadecimal(

convertBinaryToDecimal(binary)

);

}
/**

* Converts a hexadecimal string to a binary string.

* @param hexadecimal hexadecimal number as string

* @return binary number as string

*/

public String convertHexadecimalToBinary(String hexadecimal) {

// hex → decimal → binary

return convertDecimalToBinary(

convertHexadecimalToDecimal(hexadecimal)

);

/**

* Displays conversion menu and returns user choice.

* @param from source base

* @param to1 first target base

* @param to2 second target base

* @return user choice (1 or 2)

*/

public int displayConvert(String from, String to1, String to2) {

// input utility

Utility util = new Utility();


// option 1

[Link]("1. Convert from " + from + " to " + to1);

// option 2

[Link]("2. Convert from " + from + " to " + to2);

// return valid choice

return [Link](1, 2);

/**

* Displays binary conversion based on user choice.

* @param binary binary number as string

*/

public void displayBinaryConversion(String binary) {

int choice = displayConvert("binary", "decimal", "hexadecimal");

if (choice == 1) {

[Link]("Decimal: " + convertBinaryToDecimal(binary));

} else {

[Link]("Hexadecimal: " +
convertBinaryToHexadecimal(binary));

}
/**

* Displays decimal conversion based on user choice.

* @param decimal decimal number as string

*/

public void displayDecimalConversion(String decimal) {

int choice = displayConvert("decimal", "binary", "hexadecimal");

if (choice == 1) {

[Link]("Binary: " + convertDecimalToBinary(decimal));

} else {

[Link]("Hexadecimal: " +
convertDecimalToHexadecimal(decimal));

/**

* Displays hexadecimal conversion based on user choice.

* @param hexadecimal hexadecimal number as string

*/

public void displayHexadecimalConversion(String hexadecimal) {

int choice = displayConvert("hexadecimal", "binary", "decimal");

if (choice == 1) {
[Link]("Binary: " +
convertHexadecimalToBinary(hexadecimal));

} else {

[Link]("Decimal: " +
convertHexadecimalToDecimal(hexadecimal));

/**

* Main class that starts the base conversion program.

*/

public class Main {

/**

* Displays the main menu options.

*/

public void displayOption() {

[Link]("1. Change base Binary");

[Link]("2. Change base Decimal");

[Link]("3. Change base Hexadecimal");

[Link]("4. Exit");

/**
* Program entry point.

* @param args command-line arguments

*/

public static void main(String[] args) {

// handle user input

Utility util = new Utility();

// handle conversion logic

ChangeBaseNumber cb = new ChangeBaseNumber();

// create object Main

Main m = new Main();

while (true) {

[Link]();

int choice = [Link](1, 4);

switch (choice) {

case 1:

String binary = [Link]();

[Link](binary);

break;
case 2:

String decimal = [Link]();

[Link](decimal);

break;

case 3:

String hexadecimal = [Link]();

[Link](hexadecimal);

break;

case 4:

return;

You might also like