CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
1. Write a Java program to sort a numeric array and a string array?
import [Link];
public class ArraySorting {
public static void main(String[] args) {
// Sorting a numeric array
int[] numericArray = {5, 2, 9, 1, 5};
[Link](numericArray);
[Link]("Sorted numeric array: " + [Link](numericArray));
// Sorting a string array
String[] stringArray = {"apple", "banana", "cherry", "date", "berry"};
[Link](stringArray);
[Link]("Sorted string array: " + [Link](stringArray));
2. Write a Java program to calculate the average value of array elements.
public class ArrayAverage {
public static void main(String[] args) {
double[] values = {12.5, 7.2, 9.8, 3.6, 6.4};
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
double sum = 0.0;
for (double value : values) {
sum += value;
double average = sum / [Link];
[Link]("Average: " + average);
3. Write a Java program to convert an array to ArrayList.
import [Link];
import [Link];
import [Link];
public class ArrayToArrayList {
public static void main(String[] args) {
// Create an array of integers
Integer[] array = {1, 2, 3, 4, 5};
// Convert the array to an ArrayList
List<Integer> list = [Link](array);
ArrayList<Integer> arrayList = new ArrayList<>(list);
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
// Print the ArrayList
[Link]("ArrayList: " + arrayList);
4. Write a Java program to convert an ArrayList to an array.
import [Link];
public class ArrayListToArray {
public static void main(String[] args) {
// Create an ArrayList of integers
ArrayList<Integer> arrayList = new ArrayList<>();
[Link](1);
[Link](2);
[Link](3);
[Link](4);
[Link](5);
// Convert the ArrayList to an array
Integer[] array = [Link](new Integer[0]);
// Print the array
for (Integer element : array) {
[Link](element + " ");
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
5. Write a program to Accessing elements from an ArrayList
import [Link];
public class AccessArrayList {
public static void main(String[] args) {
// Create an ArrayList of strings
ArrayList<String> arrayList = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Cherry");
[Link]("Date");
// Access elements from the ArrayList
[Link]("Accessing elements from the ArrayList:");
for (int i = 0; i < [Link](); i++) {
String element = [Link](i);
[Link]("Element at index " + i + ": " + element);
}
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
6. Create a class called 'Matrix' containing a constructor that initializes the
number of rows and number of columns of a new Matrix object. The Matrix
class has the following information:
[Link] of rows of matrix
2 number of columns of matrix
3 - elements of matrix in the form of 2D array.
The Matrix class has methods for each of the following:
1 - get the number of rows
2 - get the number of columns
3 - set the elements of the matrix at given position (i,j)
4 - adding two matrices. If the matrices are not addable, "Matrices cannot be
added" will be displayed.
5 - multiplying the two matrices
public class Matrix {
private int numRows;
private int numColumns;
private int[][] elements;
public Matrix(int numRows, int numColumns, int[][] elements) {
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
[Link] = numRows;
[Link] = numColumns;
[Link] = elements;
public int getNumRows() {
return numRows;
public int getNumColumns() {
return numColumns;
public void setElement(int i, int j, int value) {
if (i >= 0 && i < numRows && j >= 0 && j < numColumns) {
elements[i][j] = value;
} else {
[Link]("Invalid position (" + i + ", " + j + ")");
public static Matrix addMatrices(Matrix matrix1, Matrix matrix2) {
if ([Link]() != [Link]() || [Link]() !=
[Link]()) {
[Link]("Matrices cannot be added.");
return null;
int numRows = [Link]();
int numColumns = [Link]();
int[][] resultElements = new int[numRows][numColumns];
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numColumns; j++) {
resultElements[i][j] = [Link][i][j] + [Link][i][j];
return new Matrix(numRows, numColumns, resultElements);
public static Matrix multiplyMatrices(Matrix matrix1, Matrix matrix2) {
if ([Link]() != [Link]()) {
[Link]("Matrices cannot be multiplied.");
return null;
int numRows = [Link]();
int numColumns = [Link]();
int[][] resultElements = new int[numRows][numColumns];
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numColumns; j++) {
int sum = 0;
for (int k = 0; k < [Link](); k++) {
sum += [Link][i][k] * [Link][k][j];
resultElements[i][j] = sum;
return new Matrix(numRows, numColumns, resultElements);
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
public void displayMatrix() {
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numColumns; j++) {
[Link](elements[i][j] + " ");
[Link]();
public static void main(String[] args) {
int numRows = 2;
int numColumns = 2;
int[][] matrix1Elements = {
{1, 2},
{3, 4}
};
int[][] matrix2Elements = {
{5, 6},
{7, 8}
};
Matrix matrix1 = new Matrix(numRows, numColumns, matrix1Elements);
Matrix matrix2 = new Matrix(numRows, numColumns, matrix2Elements);
[Link]("Matrix 1:");
[Link]();
[Link]("\nMatrix 2:");
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
[Link]();
[Link]("\nAdding Matrices:");
Matrix sumMatrix = addMatrices(matrix1, matrix2);
if (sumMatrix != null) {
[Link]();
[Link]("\nMultiplying Matrices:");
int[][] matrix3Elements = {
{1, 2, 3},
{4, 5, 6}
};
int[][] matrix4Elements = {
{7, 8},
{9, 10},
{11, 12}
};
Matrix matrix3 = new Matrix(2, 3, matrix3Elements);
Matrix matrix4 = new Matrix(3, 2, matrix4Elements);
Matrix productMatrix = multiplyMatrices(matrix3, matrix4);
if (productMatrix != null) {
[Link]();
}
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
7. Write a program that has variables to store Car data like; CarModel,
CarName, CarPrice and CarOwner. The program should include functions to
assign user defined values to the above mentioned variable and a display
function to show the values. Write a main that calls these functions.
public class Car {
private String carModel;
private String carName;
private double carPrice;
private String carOwner;
public void setCarModel(String model) {
carModel = model;
public void setCarName(String name) {
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
carName = name;
public void setCarPrice(double price) {
carPrice = price;
public void setCarOwner(String owner) {
carOwner = owner;
public void displayCarInfo() {
[Link]("Car Model: " + carModel);
[Link]("Car Name: " + carName);
[Link]("Car Price: $" + carPrice);
[Link]("Car Owner: " + carOwner);
public static void main(String[] args) {
Car myCar = new Car();
// Assign values to car data
[Link]("Mahindra Thar");
[Link]("4x4 M210021");
[Link](2050000);
[Link]("Rohan sahu");
// Display car information
[Link]("Car Information:");
[Link]();
}
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
8. Write a program to give the example for method overriding concepts
class Animal {
void makeSound() {
[Link]("Animal makes a generic sound.");
class Dog extends Animal {
@Override
void makeSound() {
[Link]("Dog barks.");
class Cat extends Animal {
@Override
void makeSound() {
[Link]("Cat meows.");
}
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
public class MethodOverridingExample {
public static void main(String[] args) {
Animal animal = new Animal();
Animal dog = new Dog();
Animal cat = new Cat();
[Link](); // Output: Animal makes a generic sound.
[Link](); // Output: Dog barks.
[Link](); // Output: Cat meows.
9. Write a program to create a class named shape. In this class we have three
subclasses: circle, triangle and square. Each class has two member functions
named draw () and erase (). Create these using polymorphism concepts.
class Shape {
void draw() {
[Link]("Drawing a generic shape");
}
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
void erase() {
[Link]("Erasing a generic shape");
class Circle extends Shape {
@Override
void draw() {
[Link]("Drawing a circle");
@Override
void erase() {
[Link]("Erasing a circle");
class Triangle extends Shape {
@Override
void draw() {
[Link]("Drawing a triangle");
@Override
void erase() {
[Link]("Erasing a triangle");
class Square extends Shape {
@Override
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
void draw() {
[Link]("Drawing a square");
@Override
void erase() {
[Link]("Erasing a square");
public class ShapeDemo {
public static void main(String[] args) {
Shape circle = new Circle();
Shape triangle = new Triangle();
Shape square = new Square();
// Polymorphism: Calling draw and erase on different shapes
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
10. Write a program to create interface A. In this interface we have two
methods meth1 and meth2. Implements this interface in another class named
MyClass
interface A {
void meth1();
void meth2();
class MyClass implements A {
@Override
public void meth1() {
[Link]("Inside meth1 method in MyClass");
@Override
public void meth2() {
[Link]("Inside meth2 method in MyClass");
}
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
public class Main {
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.meth1();
myClass.meth2();
11. Create an outer class with a function display, again create another class
inside the outer class named inner with a function called display and call the
two functions in the main class.
class Outer {
void display() {
[Link]("This is the display method in the Outer class");
class Inner {
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
void display() {
[Link]("This is the display method in the Inner class");
public class Main {
public static void main(String[] args) {
Outer outerObj = new Outer();
[Link]();
[Link] innerObj = [Link] Inner();
[Link]();
12. Write a Java program to perform employee payroll processing using
packages. In the java file, [Link] creates a package employee and creates a
class Emp. Declare the variables name,empid, category, bpay, hra, da, npay, pf,
grosspay, incometax, and allowance. Calculate the values in methods. Create
another java file [Link]. Create an object e to call the methods to
perform and print values.
public class Emp {
String name;
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
int empid;
String category;
double bpay;
double hra;
double da;
double npay;
double pf;
double grosspay;
double incometax;
double allowance;
public void calculatePayroll() {
npay = bpay + hra + da;
pf = 0.12 * npay; // Assuming 12% of basic pay for PF
grosspay = npay - pf;
incometax = 0.1 * grosspay; // Assuming 10% income tax
allowance = 0.05 * npay; // Assuming 5% of basic pay as allowance
//ANOTHER FILE FOR EMPFILE
public class Emppay {
public static void main(String[] args) {
Emp e = new Emp();
[Link] = "John";
[Link] = 101;
[Link] = "Manager";
[Link] = 50000;
[Link] = 0.2 * [Link];
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
[Link] = 0.1 * [Link];
[Link]();
[Link]("Employee Name: " + [Link]);
[Link]("Employee ID: " + [Link]);
[Link]("Employee Category: " + [Link]);
[Link]("Basic Pay: " + [Link]);
[Link]("HRA: " + [Link]);
[Link]("DA: " + [Link]);
[Link]("Net Pay: " + [Link]);
[Link]("PF Deduction: " + [Link]);
[Link]("Gross Pay: " + [Link]);
[Link]("Income Tax Deduction: " + [Link]);
[Link]("Allowance: " + [Link]);
}
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
13. Write a program in Java such that it accepts a number and displays the
square of the number 12 using applet. Create an input text field to accept a
number from user. Create a button to confirm the number and calculate its
square. When the button is clicked, display its square in a text field.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class SquareCalculator extends JFrame implements ActionListener {
private JTextField numberField, resultField;
private JButton calculateButton;
public SquareCalculator() {
setTitle("Calculate Square");
setLayout(null);
JLabel inputLabel = new JLabel("Enter a number:");
[Link](20, 20, 120, 20);
add(inputLabel);
numberField = new JTextField();
[Link](150, 20, 100, 20);
add(numberField);
calculateButton = new JButton("Calculate Square");
[Link](20, 60, 230, 30);
[Link](this);
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
add(calculateButton);
JLabel resultLabel = new JLabel("Square result:");
[Link](20, 110, 120, 20);
add(resultLabel);
resultField = new JTextField();
[Link](150, 110, 100, 20);
[Link](false);
add(resultField);
public void actionPerformed(ActionEvent e) {
if ([Link]() == calculateButton) {
try {
String inputText = [Link]();
double number = [Link](inputText);
double square = number * number;
[Link]([Link](square));
} catch (NumberFormatException ex) {
[Link]("Invalid Input");
public static void main(String[] args) {
SquareCalculator calculator = new SquareCalculator();
[Link](JFrame.EXIT_ON_CLOSE);
[Link](280, 180);
[Link](true);
}
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
14. Build and run "simple calculator " using swing.
import [Link].*;
import [Link].*;
import [Link];
import [Link];
public class SimpleCalculator extends JFrame implements ActionListener {
private JTextField display;
private double num1, num2;
private char operator;
public SimpleCalculator() {
setTitle("Simple Calculator");
setSize(300, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
display = new JTextField();
[Link]([Link]);
[Link](false);
JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
String[] buttonLabels = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", "C", "=", "+"
};
for (String label : buttonLabels) {
JButton button = new JButton(label);
[Link](this);
[Link](button);
add(display, [Link]);
add(buttonPanel);
setVisible(true);
@Override
public void actionPerformed(ActionEvent e) {
String command = [Link]();
if ([Link]("[0-9]")) {
[Link]([Link]() + command);
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
} else if ([Link]("C")) {
[Link]("");
num1 = 0;
num2 = 0;
operator = ' ';
} else if ([Link]("=")) {
num2 = [Link]([Link]());
double result = calculate(num1, num2, operator);
[Link]([Link](result));
num1 = result;
num2 = 0;
operator = ' ';
} else if ([Link]("[+\\-*/]")) {
num1 = [Link]([Link]());
operator = [Link](0);
[Link]("");
private double calculate(double num1, double num2, char operator) {
switch (operator) {
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
if (num2 != 0) {
return num1 / num2;
} else {
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
[Link](this, "Cannot divide by zero");
return 0;
default:
return num2;
public static void main(String[] args) {
[Link](() -> new SimpleCalculator());
}
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
15. Build and run “Simple registration page” using Swing
import [Link].*;
import [Link].*;
import [Link];
import [Link];
public class SimpleRegistrationPage extends JFrame implements ActionListener {
private JTextField usernameField;
private JPasswordField passwordField;
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
private JTextField emailField;
private JButton registerButton;
public SimpleRegistrationPage() {
setTitle("Simple Registration Page");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(4, 2));
JLabel usernameLabel = new JLabel("Username:");
usernameField = new JTextField();
JLabel passwordLabel = new JLabel("Password:");
passwordField = new JPasswordField();
JLabel emailLabel = new JLabel("Email:");
emailField = new JTextField();
registerButton = new JButton("Register");
[Link](this);
[Link](usernameLabel);
[Link](usernameField);
[Link](passwordLabel);
[Link](passwordField);
[Link](emailLabel);
[Link](emailField);
[Link](registerButton);
add(panel);
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109
setVisible(true);
@Override
public void actionPerformed(ActionEvent e) {
if ([Link]() == registerButton) {
String username = [Link]();
String password = new String([Link]());
String email = [Link]();
// You can add your registration logic here
// For this example, we will simply display the entered values
[Link](this, "Registered with:\nUsername: " + username + "\
nEmail: " + email);
public static void main(String[] args) {
[Link](() -> new SimpleRegistrationPage());
}
CLASS : -SY BSC(IT) SUBJECT : - JAVA
DATE : - 7/11/2023 ENROLMENT : 2202040601109