0% found this document useful (0 votes)
0 views

Computer project

Uploaded by

e9yliuuq8
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Computer project

Uploaded by

e9yliuuq8
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Algorithm module 1

### Algorithm for `input` Method

**Step 1**: Start

**Step 2**: Create a `Scanner` object named `in` to read user input.

**Step 3**: Display the message "Enter the Number System".

**Step 4**: Display the message "B-Binary, D-Decimal, O-Octal, H-Hexadecimal".

**Step 5**: Read the next character input by the user and store it in the variable `C`.

**Step 6**: Display the message "Enter a Number".

**Step 7**: Check the value of `C`:

- **If `C` is 'B'**:

- **Step 7.1**: Read the next input as a string and store it in `BNo`.

- **If `C` is 'D'**:

- **Step 7.2**: Read the next input as a string and store it in `DNo`.

- **If `C` is 'O'**:

- **Step 7.3**: Read the next input as a string and store it in `ONo`.

- **If `C` is 'H'**:

- **Step 7.4**: Read the next input as a string and store it in `HNo`.

**Step 8**: End

### Decimal To Binary

1. **Start**

2. Input the decimal number in `DNo`.

3. Initialize an empty string `BNo` to store the binary number.

4. Initialize a variable `a` to store the remainder.

5. Repeat the following steps until `DNo` is not equal to 0:

- while (`DNo` != 0) {
- `a = DNo % 2`;

- `BNo = a + BNo`;

- `DNo /= 2`;

6. Output the binary number (`BNo`).

7. **End**

### Decimal To Hexadecimal

1. **Start**

2. Input the decimal number in `DNo`.

3. Initialize an empty string `HNo` to store the hexadecimal number.

4. Initialize a variable `a` to store the remainder.

5. Repeat the following steps until `DNo` is not equal to 0:

- while (`DNo` != 0) {

- `a = DNo % 16`;

- if (`a < 10`) {

- `HNo = a + HNo`;

- } else {

- `HNo = (char)(a - 10 + 'A') + HNo`;

- `DNo /= 16`;

6. If `HNo` is empty, set it to "0".

7. Output the hexadecimal number (`HNo`).

8. **End**

### Decimal To Octal

1. **Start**

2. Input the decimal number in `DNo`.

3. Initialize an empty string `ONo` to store the octal number.

4. Initialize a variable `a` to store the remainder.

5. Repeat the following steps until `DNo` is not equal to 0:


- while (`DNo` != 0) {

- `a = DNo % 8`;

- `ONo = a + ONo`;

- `DNo /= 8`;

6. Output the octal number (`ONo`).

7. **End**

### Binary To Decimal

1. **Start**

2. Input the binary number in `BNo`.

3. Initialize an integer `DNo` to 0 to store the decimal number.

4. Initialize an integer `base` to 1 (base value for the conversion).

5. Repeat the following steps until `BNo` is not equal to 0:

- while (`BNo` != 0) {

- `a = BNo % 10`;

- `DNo = DNo + a * base`;

- `BNo = BNo / 10`;

- `base = base * 2`;

6. Output the decimal number (`DNo`).

7. **End**

### Algorithm for Hexadecimal to Decimal Conversion

**Step 1**: Start

**Step 2**: Input the hexadecimal number in `hexadecimalNumber`.

**Step 3**: Initialize `decimalNumber` to 0 to store the decimal number.

**Step 4**: Initialize `base` to 1 (representing 16^0).


**Step 5**: Repeat the following steps for each character in `hexadecimalNumber` from right to left:

- If the character is a digit (0-9):

- Convert the character to its integer value.

- Multiply the integer value by `base` and add it to `decimalNumber`.

- If the character is a letter (A-F):

- Convert the character to its corresponding integer value (A=10, B=11, ..., F=15).

- Multiply the integer value by `base` and add it to `decimalNumber`.

- Multiply `base` by 16 for the next character.

**Step 6**: Output `decimalNumber`.

**Step 7**: End

### Algorithm for `OtoD` Method

**Step 1**: Start

**Step 2**: Find the index of the decimal point in `ONo` and store it in `pointIndex`.

- If there is no decimal point, set `pointIndex` to the length of `ONo`.

**Step 3**: Initialize `result` to 0 to store the decimal equivalent of the octal number.

**Step 4**: Convert the integer part of the octal number to decimal:

- Repeat the following steps for each character in `ONo` from the start to `pointIndex` (excluding `pointIndex`):

- Multiply `result` by 8.

- Add the integer value of the current character (subtract '0' from the character) to `result`.

**Step 5**: Convert the fractional part of the octal number to decimal:

- Repeat the following steps for each character in `ONo` from `pointIndex + 1` to the end of `ONo`:

- Subtract `pointIndex` from the current index `i` and compute `8` raised to this power.

- Divide the integer value of the current character (subtract '0' from the character) by this computed value.

- Add the result to `result`.

**Step 6**: Convert `result` to a string and store it in `DNo`.


**Step 7**: End

###

Algorithm module 2
Algorithm for Constructor

Step 1: Start

Step 2: Initialize bin1 to an empty string.

Step 3: Initialize bin2 to an empty string.

Step 4: Initialize choice to 0.

Step 5: Initialize result to an empty string.

Step 6: End

Algorithm for input Method

Step 1: Start

Step 2: Prompt the user to enter the first binary number and store it in bin1.

Step 3: Prompt the user to enter the second binary number and store it in bin2.

Step 4: Display the operation choices to the user:


1. Addition

2. Subtraction

3. Multiplication

4. Division

Step 5: Prompt the user to enter their choice (1/2/3/4) and store it in choice.

Step 6: End

Algorithm for binaryToInt Method

Step 1: Start

Step 2: Input the binary string binary.


Step 3: Initialize number to 0.

Step 4: Initialize base to 1 (representing 2^0).

Step 5: Repeat the following steps until all bits in binary are processed:

 For each bit from right to left:


o If the bit is '1', add base to number.
o Multiply base by 2 for the next bit.

Step 6: Return number.

Step 7: End

Algorithm for intToBinary Method

Step 1: Start

Step 2: Input the integer number.

Step 3: If number is 0, set binary to "0" and return binary.

Step 4: Initialize an empty StringBuilder binary.

Step 5: Repeat the following steps until number is 0:

 Append the remainder of number divided by 2 to binary.


 Divide number by 2.

Step 6: Reverse binary.

Step 7: Return binary as a string.

Step 8: End

Algorithm for binaryAddition Method

Step 1: Start

Step 2: Convert bin1 to an integer num1 using binaryToInt.

Step 3: Convert bin2 to an integer num2 using binaryToInt.

Step 4: Calculate the sum of num1 and num2.

Step 5: Convert the sum to a binary string and store it in result.

Step 6: End

Algorithm for binarySubtraction Method

Step 1: Start

Step 2: Convert bin1 to an integer num1 using binaryToInt.


Step 3: Convert bin2 to an integer num2 using binaryToInt.

Step 4: Calculate the difference between num1 and num2.

Step 5: Convert the difference to a binary string and store it in result.

Step 6: End

Algorithm for binaryMultiplication Method

Step 1: Start

Step 2: Convert bin1 to an integer num1 using binaryToInt.

Step 3: Convert bin2 to an integer num2 using binaryToInt.

Step 4: Calculate the product of num1 and num2.

Step 5: Convert the product to a binary string and store it in result.

Step 6: End

Algorithm for binaryDivision Method

Step 1: Start

Step 2: Convert bin1 to an integer num1 using binaryToInt.

Step 3: Convert bin2 to an integer num2 using binaryToInt.

Step 4: If num2 is 0, set result to "Error: Division by zero".

Step 5: Otherwise, calculate the quotient of num1 divided by num2.

Step 6: Convert the quotient to a binary string and store it in result.

Step 7: End

Algorithm for performOperation Method

Step 1: Start

Step 2: Use a switch statement to check the value of choice:

 If choice is 1, call binaryAddition().


 If choice is 2, call binarySubtraction().
 If choice is 3, call binaryMultiplication().
 If choice is 4, call binaryDivision().
 If choice is invalid, set result to "Invalid choice!".

Step 3: End
Algorithm for print Method

Step 1: Start

Step 2: Output the value of result.

Step 3: End

Algorithm for main Method

Step 1: Start

Step 2: Create an instance of BinaryArithmetic.

Step 3: Call the input method to get user input.

Step 4: Call the performOperation method to execute the chosen operation.

Step 5: Call the print method to display the result.

Step 6: End
Screenshots
1

Conclusion
The project on number base conversion and a binary arithmetic calculator was successfully completed, covering
essential concepts and practical applications. The project comprised modules for converting numbers between
different bases, specifically Binary, Octal, Decimal, and Hexadecimal. These conversions were implemented using
handwritten algorithms to ensure a deep understanding of the underlying processes. Each algorithm was carefully
developed and tested, and program printouts were taken directly from the Integrated Development Environment
(IDE) to provide a clear and accurate record of the code.

Additionally, the project included modules for performing binary arithmetic operations: addition, subtraction,
multiplication, and division. These operations were chosen for their fundamental importance in computer science
and digital electronics. The binary arithmetic modules were tested extensively to verify their accuracy and efficiency.
Screenshots of the output were included at the end of the project to visually demonstrate the functionality and
correctness of each module.

Overall, the project provided valuable insights into number base conversions and binary arithmetic. By combining
theoretical knowledge with practical implementation, the project enhanced our understanding and skills in these
critical areas of computer science. The successful completion of the project also showcased the importance of
attention to detail and thorough testing in developing reliable software solutions.

You might also like