Name: Koustubh Rajesh Sadavarte
Roll No: 41
Q1.Create Package College with one class Student(rollno, Percent and method set(), get() to
get and show the details of student). Use this package in another class.
//Define the "Student" class in the "Student.java" file
package College; public class Student { private
int rollno; private double percent;
// Constructor public Student(int rollno,
double percent) { this.rollno = rollno;
this.percent = percent;
// Default constructor
public Student() {
this.rollno = 0;
this.percent = 0.0;
// Set student details public void set(int
rollno, double percent) { this.rollno =
rollno; this.percent = percent;
// Get and display student details
public void get() {
System.out.println("Roll No: " + rollno);
System.out.println("Percentage: " + percent);
}
//To use the "College" package in another class, you can create a separate Java class and import
the "Student" class: import College.Student; public class Main { public static void main(String[]
args) { //Create a student object
Student student = new Student();
// Set student details
student.set(101, 90.5);
// Display student details
System.out.println("Student Details:");
student.get();
Output:
Q2.Create package Library with 3 classes books (bid, qty), Issue() and Returns() package
Library; public class Books { private int bid; private int qty; public Books(int bid, int qty) {
this.bid = bid; this.qty = qty;
// Getters and setters for bid and qty
public int getBid() { return bid;
public void setBid(int bid) {
this.bid = bid;
public int getQty() {
return qty;
public void setQty(int qty) {
this.qty = qty;
package Library; public
class Issue {
// Add methods and attributes related to issuing books here
package Library; public
class Returns {
// Add methods and attributes related to returning books here
package Library; public
class Returns {
// Add methods and attributes related to returning books here
Output:
Q3.Program to count occurance of given number in an integer array of size 10
public class CountOccurrencesInArray { public static
void main(String[] args) { int[] array = {5, 2, 8, 5, 9, 5,
4, 2, 7, 5}; int targetNumber = 5; // The number you
want to count int count = countOccurrences(array,
targetNumber);
System.out.println("The number " + targetNumber + " appears in the array " + count + "
times.");
public static int countOccurrences(int[] arr, int target) {
int count = 0; for (int num : arr) { if (num ==
target) { count++;
return count;
Output:
Q4.Program to sort an array in descending order.
import java.util.ArrayList; import
java.util.Collections; public class
SortArrayListDescending { public
static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(5); arrayList.add(2); arrayList.add(8);
arrayList.add(5); arrayList.add(9); arrayList.add(5);
arrayList.add(4); arrayList.add(2); arrayList.add(7);
arrayList.add(5);
// Sort the ArrayList in descending order
Collections.sort(arrayList, Collections.reverseOrder());
// Print the sorted ArrayList
System.out.println("Sorted ArrayList in descending order: " + arrayList);
Output:
Q5. Program to sum odd and even elements of an array
public class SumOddEvenElements {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sumEven = 0; int sumOdd = 0;
for (int num : array) { if (num % 2 ==
0) { // Even number
sumEven += num;
} else {
// Odd number
sumOdd += num;
System.out.println("Sum of even elements: " + sumEven);
System.out.println("Sum of odd elements: " + sumOdd);
Output:
Q6. For a given 2D square matrix of size N*N, the task is to find the sum of elements in the
Principal diagonals. For example, analyze the following 4 × 4
public class PrincipalDiagonalSum {
public static void main(String[] args) {
int N = 4; // Size of the square matrix
int[][] matrix = { {1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}
};
int diagonalSum = 0;
// Iterate through the principal diagonal
for (int i = 0; i < N; i++) {
diagonalSum += matrix[i][i];
System.out.println("Sum of elements in the principal diagonal: " + diagonalSum);
Output:
Q7. Program to reverse string
public class ReverseString { public
static void main(String[] args) {
String originalString = "Hello, World!";
String reversedString = reverseString(originalString);
System.out.println("Original string: " + originalString);
System.out.println("Reversed string: " + reversedString);
public static String reverseString(String input) {
StringBuilder reversed = new StringBuilder(); for
(int i = input.length() - 1; i >= 0; i--) {
reversed.append(input.charAt(i));
return reversed.toString();
Output:
Q8. Program to remove duplicate characters from String
public class RemoveDuplicateCharacters {
public static void main(String[] args) {
String inputString = "programming";
String resultString = removeDuplicates(inputString);
System.out.println("Original string: " + inputString);
System.out.println("String without duplicates: " + resultString);
public static String removeDuplicates(String input) {
StringBuilder result = new StringBuilder(); for (int i = 0; i
< input.length(); i++) { char currentChar =
input.charAt(i); if
(result.indexOf(String.valueOf(currentChar)) == -1) {
result.append(currentChar);
return result.toString();
Output:
Q9. Program to toggle case each charatcer of every word of string
public class ToggleCaseInWords {
public static void main(String[] args) {
String inputString = "Hello World!";
String resultString = toggleCaseInWords(inputString);
System.out.println("Original string: " + inputString);
System.out.println("String with toggled case in words: " + resultString);
public static String toggleCaseInWords(String input) {
String[] words = input.split(" ");
StringBuilder result = new StringBuilder(); for (String
word : words) { for (int i = 0; i < word.length(); i++) {
char currentChar = word.charAt(i); if
(Character.isUpperCase(currentChar)) {
result.append(Character.toLowerCase(currentChar));
} else {
result.append(Character.toUpperCase(currentChar));
result.append(" "); // Add a space between words
return result.toString().trim(); // Trim to remove the trailing space
Output: