0% found this document useful (0 votes)
9 views10 pages

Samdeshftu RD

Uploaded by

gdlsource24
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)
9 views10 pages

Samdeshftu RD

Uploaded by

gdlsource24
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/ 10

* OOPs with Java *

Name : Sandesh Gupta


Student ID : 4551820
Class : SYIT A.
Experiment

Experiment-01
Write a Program to print the area of triangle
Solution :

public class Area {

public static void main(String[] args) {

double base = 5;
double height = 10;

double area = (base * height) / 2;

System.out.println("Area of the triangle: " + area);

}
}

0utput :

SandeshGupta
Experiment-02
Write a java Program to check the number is Prime or not.
Solution

public class Pract2 {

public static void main(String[] args) {

int[] array = {10, 20, 30, 40, 50};


int max = array[0];

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

if (array[i] > max) {


max = array[i];

System.out.println("Largest element: " + max);

Output:

SandeshGupta
Experiment-03
Write a java Program to Find Largest Element of an array.
Solution

blic class Pract3 {

public static void main(String[] args) {

int num = 29;


boolean isPrime = true;

for (int i = 2; i <= num / 2; i++) {


if (num % i == 0) {

isPrime = false;

break;
}

}
if (isPrime) {

System.out.println(num + " is a prime number.");

} else {

System.out.println(num + " is not a prime number.");

SandeshGupta
Experiment-04
Write a java Program to Multiply Two Matrix Using Multi-dimensional arrays.
Solution
public class Pract4 {
public static void main(String[] args) {
int[][] a = {{1, 2}, {3, 4}};
int[][] b = {{5, 6}, {7, 8}};
int[][] c = new int[2][2];

for (int i = 0; i < 2; i++) {


for (int j = 0; j < 2; j++) {
c[i][j] = 0;
for (int k = 0; k < 2; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}

System.out.println("Product of matrices:");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}
}
}

Output:

SandeshGupta
Experiment-05
Write a java Program to Count the Number of Vowels and Consonants in a Sentence.
Solution
public class Pract5 {
public static void main(String[] args) {
String str = "sandesh";
str = str.toLowerCase();
int vowels = 0, consonants = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowels++;
} else if ((ch >= 'a' && ch <= 'z')) {
consonants++;
}
}
System.out.println("Vowels: " + vowels);
System.out.println("Consonants: " + consonants);
}
}

Output:

SandeshGupta
Experiment-06
Write a java Program to Check if a String is Empty or Null.
Solution :
public class Pract6 {
public static void main(String[] args) {
String[] array = {"hello", null, "", "world", null, ""};
int emptyCount = 0, nullCount = 0;
for (String s : array) {
if (s == null) {
nullCount++;
} else if (s.isEmpty()) {
emptyCount++;
}
}
System.out.println("Empty elements: " + emptyCount);
System.out.println("Null elements: " + nullCount);
}
}
Output :

SandeshGupta
Experiment-07 with method print Data to display the data. Create the two objects s1, s2 todeclare
and access the values.
Solution :
class Pract7 {
int id;
String name;
int age;
void printData() {
System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age); }}
public class Student {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
s1.id = 1;
s1.name = "Sandesh";
s1.age = 20;
s2.id = 2;
s2.name = "Prashant";
s2.age = s1.age;
s1.printData();
s2.printData();
}
}Output :

SandeshGupta
Experiment-08

Write Java program to create calculator using switch case.


Solution :

public class Count {

public static void main(String[] args) {

String[] array = {"hello", null, "", "world", null, ""};

int emptyCount = 0, nullCount = 0;

for (String s : array) {

if (s == null) {

nullCount++;

} else if (s.isEmpty()) {

emptyCount++;

System.out.println("Empty elements: " + emptyCount);

System.out.println("Null elements: " + nullCount);

}Output :

SandeshGupta
Experiment-09
Write a java program to implement Exception Handling.
Solution:
public class Pract9 {
public static void main(String[] args) {
try {
int data = 50 / 0;
} catch (ArithmeticException e) {
System.out.println("Exception caught: " + e);
} finally {
System.out.println("Finally block executed.");
}
}
}
Output:
SandeshGupta
Experiment-10
Write a java program to implement Single level inheritance.
Solution
class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Human extends Animal {
void speak() {
System.out.println("Speaking...");
}
}
public class InheritanceTest {
public static void main(String[] args) {
Human h = new Human();
h.eat(); // Calls the inherited eat() method from Animal class
h.speak(); // Calls the speak() method from Human class
}
}

Sandesh

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy