Sandeshcom
Sandeshcom
Sandeshcom
Student ID : 4539462.
Class : SYIT – A.
Practical
Question-01
Write a Program to print the area of triangle
Solution :
double base = 5;
0utput :
Question-02
Write a java Program to check the number is Prime or not.
Solution
max = array[i];
Output:
Question-03
Write a java Program to Find Largest Element of an array.
Solution
isPrime = false;
break;
if (isPrime) {
} else {
}
Question-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];
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:
Question-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:
Question-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 :
Question-07
Write a program to create a class Student with data ‘name, city and age’ along
with method print Data to display the data. Create the two objects s1, s2 to
declare 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 :
Question-08
if (s == null) {
nullCount++;
} else if (s.isEmpty()) {
emptyCount++;
}Output :
Question-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:
Question-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
}
}