Lab 1
Lab 1
Objective
This lab focuses on understanding data representation and how to manually convert numbers
between different number systems (Decimal, Binary, Octal, and Hexadecimal) before using
built-in Java methods for conversions.
Lab Setup
Before starting the lab, ensure that you have the necessary tools installed and configured.
Primary Recommended IDE: NetBeans
• Download and install NetBeans IDE (recommended for this lab).
• Ensure Java Development Kit (JDK) is installed.
• Configure NetBeans to work with Java projects.
Alternative Options
If NetBeans is not available, you can use:
1. Eclipse – A powerful Java IDE with great debugging features.
2. IntelliJ IDEA – A smart IDE for Java development.
3. VS Code – Lightweight and extensible, but requires Java extensions.
Lab Tasks
1. Convert a decimal number to binary, octal, and hexadecimal.
2. Convert a binary number to decimal, octal, and hexadecimal.
3. Convert an octal number to decimal, binary, and hexadecimal.
4. Convert a hexadecimal number to decimal, binary, and octal.
5. Implement a menu-driven Java program that allows the user to enter a number and
select the desired conversion.
3. Decimal to Hexadecimal
To convert decimal to hexadecimal:
1. Divide the decimal number by 16.
2. Record the remainder (0-15, where 10=A, 11=B, ..., 15=F).
3. Continue until the quotient is 0.
4. The hexadecimal number is the reversed order of remainders.
Example (Convert 45 to Hexadecimal)
45 ÷ 16 = 2, remainder = 13 (D in hex)
2 ÷ 16 = 0, remainder = 2
Hexadecimal = **2D**
4. Binary to Decimal
To convert binary to decimal:
1. Multiply each binary digit by 2^position (starting from right).
2. Sum the results.
Example (Convert 101101 to Decimal)
(1 × 2⁵) + (0 × 2⁴) + (1 × 2³) + (1 × 2²) + (0 × 2¹) + (1 × 2⁰)
= 32 + 0 + 8 + 4 + 0 + 1
= 45
6. Hexadecimal to Decimal
To convert hexadecimal to decimal:
1. Multiply each hex digit by 16^position.
2. Sum the results.
Example (Convert 2D to Decimal)
(2 × 16¹) + (D × 16⁰) → (D = 13 in decimal)
= (2 × 16) + (13 × 1)
= 32 + 13
= 45