You are on page 1of 15

SOURCE CODE

OUTPUT
SOURCE CODE
OUTPUT
Algorithm

Step 1: Define a class named `BinOdd`.

Step 2: Define a method named `input`:

1. Declare variables `lr` (lower range) and `ur` (upper range).

2. Create a Scanner object (`sc`) for user input.

3. Use a do-while loop:

a. Print "Enter lower and upper range".

b. Read and store values for `lr` and `ur` from the user.

c. Continue looping until `lr` is less than `ur`.

4. Call the `display` method with `lr` and `ur` as arguments.

Step 3: Define a method named `display` that takes two integers `a` and `b`:

1. Declare a variable `bin`.

2. Use a loop to iterate from `a` to `b`:

a. Calculate the binary representation of the current number using the `binary` method.

b. Check if the binary representation satisfies the condition using the `check` method.

c. If the condition is true, print the current number.

Step 4: Define a method named `binary` that takes an integer `n` and returns an integer:

1. Initialize variables `bin` and `b` to 0.

2. Use a while loop until `n` becomes zero:

a. Calculate the remainder (`r`) when `n` is divided by 2.

b. Calculate the power of 10 (`c`) using `b`.

c. Update `bin` by adding `(r * c)` to it.

d. Update `n` by dividing it by 2.

e. Increment `b`.

3. Return the calculated binary representation (`bin`).


Step 5: Define a method named `check` that takes an integer `n` and returns a boolean:

1. Initialize variables `c` and `p` to 0.

2. Use a loop to calculate the number of digits in `n` and store them in an array `ar`.

3. Use a loop to check the condition for each digit in the array:

a. If the position (`i+1`) is even and the digit is 1, increment `f`.

4. If `f` is greater than 0, return false; otherwise, return true.

Step 6: Define the `main` method:

1. Create an instance of the `BinOdd` class (`ob`).

2. Call the `input` method on the `ob` object.

Step 7: The program execution completes.


Variable Listing:

Data member: Type Purpose Scope


lr int Lower range input input()
ur int Upper range input input()
bin int Binary representation of a number display()
a int Temporary variable for lower range display()
b int Temporary variable for binary representation binary()
n int Temporary variable for iterating through digits binary(), check()
r int Temporary variable for storing remainder binary()
c int Temporary variable for the power of 10 in binary conversion binary()
ar int[] Array to store digits of binary representation check()
p int Temporary variable for indexing array check()
f int Counter for checking if there is at least one '1' in even positions check()

Method Listing:

Method Purpose Type


input() Takes user input for lower and upper range, then calls the display method void
display(int a, int Displays numbers in the range [a, b] that satisfy the condition specified by the
b) check method void
binary(int n) Converts a decimal number to binary int
check(int n) Checks if there is at least one '1' in even positions of the binary representation boolean
main(String
args[]) Creates an instance of the BinOdd class and invokes the input method void

Class Listing:
Class Purpose
Represents a class for finding and displaying numbers in a given range whose binary representation has at least
one '1' in even positions. The program takes user input for the lower and upper range and uses methods like
binary and check to perform the required operations. The main method creates an object of this class and
BinOdd invokes the necessary methods.
Algorithm

Step 1: Define a class named `Date_`.

Step 2: Declare instance variables:


- `dd` and `mm` to represent day and month respectively.
- `d2` and `m2` to store temporary day and month values during calculations.
- An array `days` to store the number of days in each month.
- `daynum` to store the day number.
- A Scanner object `sc` for user input.

Step 3: Define a default constructor `Date_()`:


- Initialize `daynum`, `d2`, and `m2` to 0.

Step 4: Define a parameterized constructor `Date_(int d, int m)`:


- Initialize `dd` with the provided day `d` and `mm` with the provided month
`m`.

Step 5: Define a method `DateToDateNumber` that takes a `Date_` object `dt`


and returns an integer:
1. Initialize variables `x` and `y` with the day and month from the provided
`dt`.
2. Initialize `totd` to 0.
3. Use a loop to calculate the total days (`totd`) until the provided month (`y-
1`).
4. Add the day (`x`) to `totd`.
5. Return `totd`.
Step 6: Define a method `DateNumberToDate` that takes an integer `dn` and
returns a `Date_` object:
1. Create a new `Date_` object `nd`.
2. Set `d2` in `nd` to the provided day number (`dn`).
3. Use a loop to determine the month and remaining day in the month.
4. Return the modified `nd`.

Step 7: Define a method `accept`:


- Prompt the user to enter the day number.
- Read and store the day number in `daynum`.

Step 8: Define a method `display`:


1. Create a new `Date_` object `d` using `DateNumberToDate(daynum)`.
2. Print the formatted date in the format "dd-mm-2023".

Step 9: Define the `main` method:


1. Create a Scanner object `sc1` for user input.
2. Use a do-while loop to ensure valid input for the day and month.
3. Create a `Date_` object `ob1` using the provided day and month.
4. Display the day number for the given date using `DateToDateNumber`
method.
5. Create another `Date_` object `ob2`.
6. Call `accept` and `display` methods on `ob2`.
Step 10: The program execution completes.
Variable Listing:

Data
member: Type Purpose Scope
Default constructor, Parameterized
dd int Day of the date constructor
Default constructor, Parameterized
mm int Month of the date constructor
Default constructor, DateNumberToDate
d2 int Day used in the DateNumberToDate method method
Month used in the DateNumberToDate Default constructor, DateNumberToDate
m2 int method method
Array representing the number of days in
days int[] each month Default constructor
Day number used in accept and display Default constructor, accept method,
daynum int methods display method
sc Scanner Scanner object for input Default constructor

Method Listing:

Method Purpose Type


Default constructor Initializes daynum, d2, m2 Constructor
Parameterized constructor Initializes dd and mm Constructor
DateToDateNumber(Date_
dt) Converts a Date object to day number int
DateNumberToDate(int dn) Converts a day number to Date object Date_
accept() Takes user input for daynum void
display() Displays the date corresponding to daynum void
Takes user input for a date, calculates and displays the day number,
main(String args[]) accepts another day number, and displays the corresponding date void

Class Listing:

Class Purpose
Represents a class for handling dates. It includes methods to convert between a date and a day
Date_ number. The main method takes user input for a date, calculates and displays its day number, accepts
Serial Date Assignment Name Teacher’s Signature
No.
11 25.09.2023 EvenSeries
12 29.09.2023 Transpose
13 03.10.2023 Words
14 06.10.2023 Catalan
15 09.10.2023 Rome
16 13.10.2023 Book
17 16.10.2023 Lucky
18 16.10.2023 Ass18
19 06.11.2023 Mathrev
20 17.11.2023 Assotw
21 08.12.2023 Cr
22 13.12.2023 BinOdd
23 15.12.2023 Ass23
24 03.01.2024 Date
25 10.01.2024 Encrypt

You might also like