0% found this document useful (0 votes)
16 views20 pages

Java OOP Lab Exercises Report

Uploaded by

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

Java OOP Lab Exercises Report

Uploaded by

sudipevil7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Kathmandu College of Technology

Lokanthali, 16 Bhaktapur

Lab Reports On
OOP IN JAVA
(CACS-204)
Faculty of Humanities and Social Sciences
Tribhuvan University
Kirtipur, Nepal

Date: 2079/09/15 Total No. of Experiment: 6

Submitted By: Submitted To:

Name: Sakar Prasad Mainali Department of BCA


Roll No: 16(Sixteen) -
Faculty: Bachelor in Computer Application - Narayan Chalise
Year//Part: 2nd Year, 2077 // 3rd Semester (Lecturer/Supervisor)

TABLE OF CONTENTS
Lab TOPIC
No.
Lab 1 Lab Exercise 1

Lab 2 Lab Exercise 2

Lab 3 Lab Exercise 3

Lab 4 Lab Exercise 4


Lab 5 Lab Exercise 5
Lab 6 Lab Exercise 6
Kathmandu College of Technology
Lokanthali, 16 Bhaktapur

Lab Report On
OOP IN JAVA
(Lab Exercise 1)
(Using IntelliJ-IDEA Software with JDK)

Date: 2079/05/19 No. of Experiment: # 01

Submitted By: Submitted To:

Name: Sakar Prasad Mainali Department of BCA


Roll No: 15 (Fifteen) - Narayan Chalise
Faculty: Bachelor in Computer Application (Lecturer/Supervisor)
Year//Part: 2nd Year, 2077 // 3rd Semester
TABLE OF CONTENTS
1. Write a java program to find greater number among three numbers.
2. Write a java program to print odd and even numbers up to 100.
3. Write a java program to print the prime numbers up to 100.
4. Write a java program to print Fibonacci series up to 100.
5. Write a java program to perform arithmetic operator uses.
6. Write a java program to swap two numbers without using third variable.
7. Write a java program to print:-

*********
*******
*****
***
*

8. Write a java program to create jagged array and use it.


9. Write a java program to perform matrix addition.
10. Write a java program to perform matrix multiplication.
11. Write a java program to use jumping statements i.e. break and continue.
12. Write a java program to use command line arguments.
13. Write a java program to sort the number in descending order.
14. Write a java program to print:-

11111
2222
333
44
5
66
777
8888
99999

15. Write a java program to perform typecasting.


Lab Exercise 1
1. Write a java program to find greater number among three numbers.

Program:-
public class Max {
public static void main(String[] args)
{
int a = 3; b = 2; c = 4;
if (a > b)
{
if (a > c)
[Link]("The greatest number is " + a);

else
[Link]("The greatest number is " + c);
}

else if (b > c)
[Link]("The greatest number is " + b);

else
[Link]("The greatest number is " + c);
}
}

Output:-
2. Write a java program to print odd and even numbers up to 100.

Program:-
public class OddEven {
public static void main(String[] args)
{
[Link]("The even number up to 100:");
for (int i = 0; i <= 100; i++)
{
if (i % 2 == 0)
[Link](i + ", ");
}
[Link]("\n");
[Link]("\nThe odd number up to 100:");
for (int i = 0; i <= 100; i++)
{
if (i % 2 != 0)
[Link](i + " ");
}
}
}

Output:-
3. Write a java program to print the prime numbers up to 100.

Program:-

public class Prime


{
public static void main(String[] args) {
int a=1,b=100;
for(int i=a;i<=b;i++){
if(checkPrime(i)){
[Link](i+" " );
}
}
}
public static boolean checkPrime(int num){
if(num<2){
return false;
}
else{
int x= num/2;
for(int i=2;i<x;i++){
if(num%i==0){
return false;
}
}
}
return true;
}
}

Output:-
4. Write a java program to print Fibonacci series up to 100.

Program:-

public class Series {


public static void main(String[] args)
{
int n1 = 0, n2 = 1, num, i = 0;
[Link]("Fibonacci Series up to 100");
while (i >= 0) {
num = n1 + n2;
[Link](n1 + " ");
n1 = n2;
n2 = num;
if (n1 >= 100) {
break;
}
i++;
}
}
}

Output:-
5. Write a java program to perform arithmetic operator uses.

Program:-
public class Arithmetic {
public static void main(String []args) {

int a = 15, b = 7;

// addition operator
[Link]("a + b = " + (a + b));

// subtraction operator
[Link]("a - b = " + (a - b));

// multiplication operator
[Link]("a * b = " + (a * b));

// division operator
[Link]("a / b = " + (a / b));

// modulo operator
[Link]("a % b = " + (a % b));
}
}

Output:-
6. Write a java program to swap two numbers without using third variable.

Program:-
public class Swap {
public static void main(String []args) {
int a = 20, b = 15;
[Link]("Before Swap: " + "a: " + a + ", b: " + b);
a = a + b;
b = a - b;
a = a - b;
[Link]("After Swap: " + "a: " + a + ", b: " + b);
}
}

Output:-
7. Write a java program to print-

*********
*******
*****
***
*

Program:-
public class Pattern {
public static void main(String[] args) {
int num = 5;
int i, j;
for(i = num; i >= 1; i--) {

for(j = i; j < num; j++) {


[Link](" ");
}

for(j = 1; j <= (2 * i - 1); j++) {


[Link]("*");
}

[Link](" ");
}
}
}

Output:-

8. Write a java program to create jagged array and use it.


Program:-

public class JaggedArray {


public static void main(String []args) {
int [][]arr = new int[4][];

arr[0] = new int[1];


arr[1] = new int[2];
arr[2] = new int[3];
arr[3] = new int[4];

int count = 0;
for (int i = 0; i < [Link]; i++)
for (int j = 0; j < arr[i].length; j++)
arr[i][j] = count++;

[Link]("Contents of 2D Jagged Array:");


for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < arr[i].length; j++)
[Link](arr[i][j] + " ");
[Link]();
}
}
}

Output:-
9. Write a java program to perform matrix addition.

Program:-

public class MatrixAdd {


public static void main(String []args) {
int [][]a={{1,2,3},{4,5,6},{7,8,9}};
int [][]b={{1,3,4},{2,4,3},{1,2,4}};

int [][]c=new int[3][3];


[Link]("Matrix Addition: ");
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
c[i][j]=a[i][j]+b[i][j];
[Link](c[i][j]+" ");
}

[Link]();
}
}
}

Output:-
10. Write a java program to perform matrix multiplication.

Program:-
public class MatrixMul {
public static void main(String []args) {
int [][]a = {{1,2,3}, {4,5,6}, {7,8,9}};
int [][]b = {{1,3,4}, {2,4,3}, {1,2,4}};

int [][]c = new int[3][3];


[Link]("Matrix Multiplication: ");
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
c[i][j] = 0;
for(int k = 0; k < 3; k++)
{
c[i][j] += a[i][k] * b[k][j];
}

[Link](c[i][j]+" ");
}

[Link]();
}
}
}

Output:-
11. Write a java program to use jumping statements i.e. break and continue.

Program:-

public class Jump {


public static void main(String []args) {
[Link]("Break Statements: ");
for (int i = 0; i < 10; i++) {
if (i == 7)
break;
[Link](i);
}

[Link]("\n");
[Link]("Continue Statements: ");
for (int i = 0; i < 10; i++) {
if (i == 7) {
[Link]("-");
continue;
}
[Link](i);
}
}
}

Output:-
12. Write a java program to use command line arguments.

Program:-

public class CommArgs {

public static void main(String args[]){

for(int i=0;i<[Link];i++)
[Link](args[i]);

}
}

Output:-
13. Write a java program to sort the number in descending order.

Program:-

public class SortDescend {


public static void main(String []args) {
int []arr = {36,69,35,2,12,420,123};
int n = [Link];
int temp;
[Link]("Array Before Bubble Sort:");
for(int i = 0; i < [Link]; i++){
[Link](arr[i] + " ");
}
[Link]();
for(int i = 0; i < n; i++) {
for(int j = 1; j < (n-i); j++) {
if(arr[j-1] < arr[j]) {
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
[Link]("Array After Bubble Sort:");
for(int i=0; i < [Link]; i++){
[Link](arr[i] + " ");
}
}
}

Output:-

14. Write a java program to print-


11111
2222
333
44
5
66
777
8888
99999

Program:-

public class AlphaPattern {


public static void main(String []args) {

for (int i = 1; i <= 5; i++) {


for (int j = 5; j >= i; j--) {
[Link](i + " ");
}
[Link]();
}

for (int i = 6; i <= 9; i++) {


for (int j = 2; j <= (i-3); j++) {
[Link](i + " ");
}
[Link]();
}
}
}

Output:-
15. Write a java program to perform typecasting.

Program:-

public class TypeCasting {


public static void main(String []args) {
double d = 420.69;

long l = (long)d;

int i = (int)l;

float f = (float)i;

[Link]("Before conversion: " + d);

[Link]("Conversion from double to long type: " + l);

[Link]("Conversion from long to int type: " + i);

//lost data in typecasting due to narrowing


[Link]("Conversion from int to float type: " + f);
}
}

Output:-

You might also like