0% found this document useful (0 votes)
18 views11 pages

Lab 1

This lab focuses on data representation and conversions between Decimal, Binary, Octal, and Hexadecimal in Java. It includes manual conversion methods, built-in Java functions, and exercises for students to practice and modify their code. The lab also emphasizes setting up the necessary tools and creating a menu-driven program for conversions.

Uploaded by

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

Lab 1

This lab focuses on data representation and conversions between Decimal, Binary, Octal, and Hexadecimal in Java. It includes manual conversion methods, built-in Java functions, and exercises for students to practice and modify their code. The lab also emphasizes setting up the necessary tools and creating a menu-driven program for conversions.

Uploaded by

Junaid Afzal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Lab 1: Data Representation and Conversions in Java

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.

Theory: Manual Conversion Methods


1. Decimal to Binary
To convert a decimal number to binary, follow these steps:
1. Divide the decimal number by 2.
2. Record the remainder (either 0 or 1).
3. Continue dividing the quotient by 2 until you get 0.
4. The binary number is the reversed order of remainders.
Example (Convert 45 to Binary)
45 ÷ 2 = 22, remainder = 1
22 ÷ 2 = 11, remainder = 0
11 ÷ 2 = 5, remainder = 1
5 ÷ 2 = 2, remainder = 1
2 ÷ 2 = 1, remainder = 0
1 ÷ 2 = 0, remainder = 1
Binary = **101101**

Manual Conversion: Decimal to Binary in Java

While loop execute

Using Built-in Java Function: Decimal to Binary


2. Decimal to Octal
To convert decimal to octal:
1. Divide the decimal number by 8.
2. Record the remainder (0-7).
3. Continue until the quotient is 0.
4. The octal number is the reversed order of remainders.
Example (Convert 45 to Octal)
45 ÷ 8 = 5, remainder = 5
5 ÷ 8 = 0, remainder = 5
Octal = **55**

Manual Conversion: Decimal to Octal in Java


Using Built-in Java Function: Decimal to Octal

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**

Manual Conversion: Decimal to Hexadecimal in Java

Using Built-in Java Function: Decimal to Hexadecimal

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

Manual Conversion: Binary to Decimal in Java

Using Built-in Java Function: Binary to Decimal


5. Octal to Decimal
To convert octal to decimal:
1. Multiply each octal digit by 8^position.
2. Sum the results.
Example (Convert 55 to Decimal)
(5 × 8¹) + (5 × 8⁰)
= 40 + 5
= 45

Manual Conversion: Octal to Decimal in Java


Using Built-in Java Function: Octal to Decimal

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

Manual Conversion: Hexadecimal to Decimal in Java

Using Built-in Java Function: Hexadecimal to Decimal


Exercises for Students After Completing the Lab
Exercise 1: Manual Conversion Practice
Convert the following numbers manually (without using a calculator or program). Show all
steps for conversion.
1. Convert decimal 98 to binary.
2. Convert decimal 144 to octal.
3. Convert decimal 255 to hexadecimal.
4. Convert binary 1101101 to decimal.
5. Convert hexadecimal 1F to decimal.

Exercise 2: Modify the Code


Modify the manual conversion programs you wrote earlier to:
1. Accept negative numbers and convert them using two’s complement representation.
2. Allow multiple conversions in a loop instead of just one.
3. Accept floating-point numbers for decimal to binary conversion.

Exercise 3: Debugging Challenge


The following Java code is intended to convert decimal to binary manually, but it has a bug.
Find and fix the error.
public class DecimalToBinaryBug {
public static void main(String[] args) {
int decimal = 25;
String binary = "";

while (decimal > 0) {


binary += decimal % 2;
decimal = decimal / 2;
}

System.out.println("Binary equivalent: " + binary);


}
}
Task: Fix the error and explain what went wrong.

Exercise 4: Implement Reverse Conversions


Write a Java program to perform the following reverse conversions manually:
1. Convert binary to decimal (without using built-in functions).
2. Convert octal to decimal (without using built-in functions).
3. Convert hexadecimal to decimal (without using built-in functions).

Exercise 5: Create a Menu-Driven Converter


Write a single Java program that:
1. Displays a menu with options for:
✓ Decimal to Binary
✓ Decimal to Octal
✓ Decimal to Hexadecimal
✓ Binary to Decimal
✓ Octal to Decimal
✓ Hexadecimal to Decimal
2. Asks the user to choose an option and input a number.
3. Converts the number using manual methods (not built-in functions).
4. Displays the result and asks the user if they want to perform another conversion.

You might also like