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

Pratham

The document contains practical exercises for an OOP with Java course, authored by Pratham Suthar, demonstrating various programming concepts. It includes examples of loops, finding maximum and minimum values in arrays, class and object usage, recursive functions for calculating factorials, and method overloading. Each practical exercise is accompanied by code snippets and their expected outputs.

Uploaded by

gatbackontrack
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)
5 views11 pages

Pratham

The document contains practical exercises for an OOP with Java course, authored by Pratham Suthar, demonstrating various programming concepts. It includes examples of loops, finding maximum and minimum values in arrays, class and object usage, recursive functions for calculating factorials, and method overloading. Each practical exercise is accompanied by code snippets and their expected outputs.

Uploaded by

gatbackontrack
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

Name : Pratham Suthar SUB : OOP with JAVA

Enrollment No : 236420316053 SUB CODE : 4341602

Practical : 6
Aim : Develop program to demonstrate use of
1) for loop
2) ‘while’ and ‘do

Code : 1
class p6
{
public static void main(String args[])
{
int i = 1, n = 5;
while(i<=n)
{
System.out.println(i);
i++;
}
}
}
Output :
Name : Pratham Suthar SUB : OOP with JAVA
Enrollment No : 236420316053 SUB CODE : 4341602

Code : 2.1
class p6
{
public static void main(String[] args) {

// declare
variables int i
= 1, n = 5;

// while loop from 1


to 5 while(i <= n) {
System.out.println(
i); i++;
}
}
}

Output : 2.1
Name : Pratham Suthar SUB : OOP with JAVA
Enrollment No : 236420316053 SUB CODE : 4341602

Code : 2.2
class p6
{
public static void main(String[]

args) { int i = 1, n = 7;

// do...while loop from 1


to 7 do {
System.out.println(
i); i++;
} while(i <= n);
}
}

Output : 2.2
Name : Pratham Suthar SUB : OOP with JAVA
Enrollment No : 236420316053 SUB CODE : 4341602

Practical : 7
Aim : Develop a Java program to find maximum and minimum
numbers from array elements.
Code :
public class p7{
public static void main(String []
args){ int[] arr = {12,5,9,25,3};

int max =
arr[0]; int
min =
arr[0];

for (int i = 1; i < arr.length; i++)

if(arr[i] > max) {

max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
Name : Pratham Suthar SUB : OOP with JAVA
Enrollment No : 236420316053 SUB CODE : 4341602

System.out.println("Maximum number in the


array: " + max);
System.out.println("Minimum number in the array: " +

min);

}
}

Output :
Name : Pratham Suthar SUB : OOP with JAVA
Enrollment No : 236420316053 SUB CODE : 4341602

Practical : 8
Aim : Develop a basic java program that demonstrates the use of
Class & Object.
Code :
class p8{
// A simple attribute for the dog's name
String name;
int age;

// A method to display the dog's name


void bark() {
System.out.println("The dog's name is: " + name);
System.out.println("The dog's age is: " + age);
}
}

public class Main {


public static void main(String[] args) {
// Creating an object of the Dog class
p8 myDog = new p8();

// Assigning a name to the dog


myDog.name = "Buddy";
myDog.age=3;
// Calling the bark method to print the dog's name
Name : Pratham Suthar SUB : OOP with JAVA
Enrollment No : 236420316053 SUB CODE : 4341602

myDog.bark();
}
}

Output :
Name : Pratham Suthar SUB : OOP with JAVA
Enrollment No : 236420316053 SUB CODE : 4341602

Practical : 9
Aim : Develop a java program to find the factorial of a given number
using a recursive function.
Code :
public class p9 {

// Recursive method to calculate factorial


public static long factorial(int n) {
if (n == 0) {
return 1; // Base case: factorial of 0 is 1
} else {
return n * factorial(n - 1); // Recursive case
}
}

public static void main(String[] args) {


// Hard-coded value for which to calculate the factorial
int number = 5; // Change this value to compute the factorial
of a different number

// Check for non-negative input


if (number < 0) {
System.out.println("Factorial is not defined for negative
numbers.");
} else {
long result = factorial(number);
Name : Pratham Suthar SUB : OOP with JAVA
Enrollment No : 236420316053 SUB CODE : 4341602

System.out.println("Factorial of " + number + " is: " +


result);
}
}
}

Output :
Name : Pratham Suthar SUB : OOP with JAVA
Enrollment No : 236420316053 SUB CODE : 4341602

Practical : 10
Aim : Develop a java program that demonstrates method overloading.
Code :

class Adder {
static int add(int a, int b) {
return a + b;
}

static int add(int a, int b, int c) {


return a + b + c;
}
}

class Student {
public static void main(String[] args) {
System.out.println(Adder.add(11, 11));
System.out.println(Adder.add(11, 11, 11));
}
}
Name : Pratham Suthar SUB : OOP with JAVA
Enrollment No : 236420316053 SUB CODE : 4341602

Output :

You might also like