Practical 1- Programs on the Basics of Java
Practical 1- Programs on the Basics of Java
Practical 1
Programs on the Basics of Java
1. Write a program to display “Hello World!!!” Use the command line to run this program.
Logic:
• The `Practical1` package organizes the Java file.
• The `q1` class contains the `main` method, the program's entry point.
• `System.out.println("Hello World!!!");` prints the text to the command line.
• 4. Compile with: javac q1.java
• Run with: java q1
• The warning occurs because module names shouldn’t end with a number.
Code:
Output:
DY22SBBUOBID028 1
Java Journal Date of Performance: 21-01-2025
Logic:
1. Initialize a 2D array (numbers).
2. Create two arrays to store even and odd numbers.
3. Loop through the matrix:
• If a number is even, store it in evenNumbers.
• If a number is odd, store it in oddNumbers.
4. Resize arrays to remove unused spaces.
5. Print results:
• Original matrix, even numbers, odd numbers, and their counts.
Code:
Output:
DY22SBBUOBID028 2
Java Journal Date of Performance: 21-01-2025
Logic:
• The `Practical1` package organizes the Java file.
• The `q3` class contains the `main` method, the program's entry point.
• Two integer variables, `a = 10` and `b = 5`, are declared.
• Basic arithmetic operations (`+`, `-`, `*`, `/`, `%`) are performed.
• The results are printed using `System.out.println()`.
Code:
Output:
DY22SBBUOBID028 3
Java Journal Date of Performance: 21-01-2025
4. Write a program to demonstrate the use of increment and decrement operators. Use both
cases (For example: x++ and ++x)
Logic:
• Post-increment (x++):
• Assign x to y, then increment x.
• Pre-increment (++x):
• Increment x, then assign the new value to y.
• Post-decrement (x--):
• Assign x to y, then decrement x.
• Pre-decrement (--x):
• Decrement x, then assign the new value to y.
Code:
Output:
DY22SBBUOBID028 4
Java Journal Date of Performance: 21-01-2025
5. Write a program to calculate the area of a sphere (4πr2) with a radius of 30 units.
Calculate the area of the trapezium (height*(base1+base2/2)) with the height of 15 units
and bases of 20 units and 25 units respectively. Use appropriate data types.
Logic:
• The radius of the sphere is set to 30 units.
• The area of the sphere is calculated using the formula 4 * π * r², where r is the radius.
• The height of the trapezium is set to 15 units, and the bases are 20 and 25 units.
• The area of the trapezium is calculated using the formula height * (base1 + base2) / 2.
• The program prints the area of the sphere and the trapezium.
Code:
Output:
DY22SBBUOBID028 5
Java Journal Date of Performance: 21-01-2025
6. Write a program to demonstrate the use of compound assignment operators. Use all the
arithmetic operators.
Logic:
• `a` is initialized to 10, and `b` is initialized to 5.
• The compound assignment `a += b` adds `b` to `a` and stores the result in `a`.
• The compound assignment `a -= b` subtracts `b` from `a` and stores the result in `a`.
• The compound assignment `a *= b` multiplies `a` by `b` and stores the result in `a`.
• The compound assignment `a /= b` divides `a` by `b` and stores the result in `a`.
• The compound assignment `a %= b` calculates the remainder when `a` is divided by `b`
and stores the result in `a`.
• Each result is printed after the operation.
Code:
DY22SBBUOBID028 6
Java Journal Date of Performance: 21-01-2025
Output:
DY22SBBUOBID028 7
Java Journal Date of Performance: 21-01-2025
Logic:
• An integer value of 150 is cast to a byte, which results in a value of -106 due to overflow.
• A double value of 45.67 is cast to an integer, which truncates the decimal part, resulting in
45.
• A float value of 25.75 is cast to a byte, which truncates the decimal part, resulting in 25.
• Each conversion result is printed after the respective operation.
Code:
Output:
DY22SBBUOBID028 8
Java Journal Date of Performance: 21-01-2025
DY22SBBUOBID028 9