Koustubh Java Practicle Assignment 2
Koustubh Java Practicle Assignment 2
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.
this.percent = percent;
// Default constructor
public Student() {
this.rollno = 0;
this.percent = 0.0;
}
//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.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;
return qty;
this.qty = qty;
class Issue {
class Returns {
class Returns {
Output:
Q3.Program to count occurance of given number in an integer array of size 10
public class CountOccurrencesInArray { public static
targetNumber);
System.out.println("The number " + targetNumber + " appears in the array " + count + "
times.");
target) { count++;
return count;
Output:
SortArrayListDescending { public
arrayList.add(5);
Collections.sort(arrayList, Collections.reverseOrder());
Output:
sumEven += num;
} else {
// Odd number
sumOdd += num;
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 {
{5, 6, 7, 8},
{9, 10, 11, 12},
};
int diagonalSum = 0;
diagonalSum += matrix[i][i];
Output:
reversed.append(input.charAt(i));
return reversed.toString();
Output:
input.charAt(i); if
(result.indexOf(String.valueOf(currentChar)) == -1) {
result.append(currentChar);
return result.toString();
Output:
(Character.isUpperCase(currentChar)) {
result.append(Character.toLowerCase(currentChar));
} else {
result.append(Character.toUpperCase(currentChar));
Output: