Nisarg Java File
Nisarg Java File
Practical :- 1
1. Study of class path and java runtime environment.
Ans :-
class P1
{
public static void main (String a[])
{
System.out.println("Hello World");
}
}
Output :-
Output :-
Output :-
Practical :- 2
Ans :-
import java.util.*;
class Array
{
int data[];
Array()
{
data = new int[0];
}
Array(int size)
{
data = new int[size];
}
Array(int data[])
{
this.data = data;
}
void reverseArray()
{
int n = data.length;
int temp;
for (int i = 0; i < n / 2; i++)
{
temp = data[i];
data[i] = data[n - i - 1];
data[n - i - 1] = temp;
}
}
int maximumOfArray()
{
int max = data[0];
for (int i = 1; i < data.length; i++)
{
if (data[i] > max)
{
max = data[i];
}
}
return max;
}
int averageOfArray()
{
int sum = 0;
for (int i = 0; i < data.length; i++)
{
sum += data[i];
}
return sum / data.length;
}
void sorting()
{
for (int i = 0; i < data.length; i++)
{
for (int j = 0; j < data.length - i - 1; j++)
{
if (data[j] > data[j + 1])
{
int temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
}
}
void display()
{
for (int i = 0; i < data.length; i++)
{
System.out.print(data[i] + " ");
}
System.out.println();
}
int search(int no)
{
for (int i = 0; i < data.length; i++)
{
if (data[i] == no)
{
return i;
}
}
return -1;
}
int size()
{
return data.length;
}
}
}
class Arr1
{
public static void main(String[] args)
{
int data1[] = {1, 2, 3, 4, 5};
Array array1 = new Array();
Array array2 = new Array(10);
Array array = new Array(data1);
int searchKey = 3;
int index = array.search(searchKey);
if (index == -1)
{
System.out.println(searchKey + " not found in Array.");
}
else
{
Output :-
2. Define a class Matrix with following Field : int row , column ; float
mat[][] Function : Matrix(int a[][]) , Matrix() , Matrix(int rwo, int
col) , void readMatrix() , float [][] transpose( ) , float [][]
matrixMultiplication(Matrix second ) , void displayMatrix(float [][]a)
, void displayMatrix() , float maximum_of_array() , float
average_of_array( ) create three object of Matrix class with different
constructors in main and test all the functions in main.
Ans :-
import java.util.Scanner;
class Matrix
{
int row;
int column;
float mat[][];
Matrix(int a[][])
{
this.row = a.length;
this.column = a[0].length;
this.mat =new float[this.row][this.column];
for(int i=0;i<this.row;i++){
for(int j=0;j<this.column;j++)
{
this.mat[i][j]=a[i][j];
}
}
}
Matrix()
{
this.row = 3;
this.column = 3;
}
}
return transposed;
}
float[][] matrixMultiplication(Matrix second)
{
if (this.column != second.row)
{
System.out.println("Error: The number of columns in the first matrix must match
the number of rows in the second matrix");
return null;
}
float[][] result = new float[this.row][second.column];
for (int i = 0; i < this.row; i++)
{
for (int j = 0; j < second.column; j++)
{
for (int k = 0; k < this.column; k++)
{
result[i][j] += this.mat[i][k] * second.mat[k][j];
}
}
}
return result;
}
void displayMatrix(float[][] a)
System.out.println("Matrix 1:");
matrix1.readMatrix();
matrix1.displayMatrix();
}
}
Output :-
Output :-
Output :-
Output :-
Practical :- 3
class BankAccount12
{
public static void main(String args[])
{
BankAccount A1= new BankAccount();
A1.createAcc();
A1.Deposit();
A1.withdraw();
A1.balanceInquiry();
}
}
Output :-
2. Create a class time that has hour, minute and second as data
members. Create a parameterized constructor to initialize Time
Objects. Create a member Function Time Sum (Time, Time) to sum
two time objects.
{
int hour , minute , second;
time()
{
hour = 0;
minute = 0;
second = 0;
}
time(int h , int m , int s)
{
hour = h;
minute = m;
second = s;
}
time sum(time t2 , time t3)
{
t3.hour = hour + t2.hour;
t3.minute = minute + t2.minute;
t3.second = second + t2.second;
if(t3.second>=60)
{
t3.minute = t3.minute + t3.minute;
return t3;
}
void display()
{
System.out.println(hour + " : " + minute + " : " + second);
}
}
class time1
{
public static void main(String args[])
{
time t1 = new time (4 , 24 , 29);
time t2 = new time (2 , 49 , 29);
time t3 = new time ();
t3 = t1.sum(t2,t1);
System.out.print("t3 = ");
t3.display();
}
}
3. Define a class with the Name, Basic salary and dearness allowance as
data members. Calculate and print the Name, Basic salary(yearly),
dearness allowance and tax deduced at source(TDS) and net salary,
where TDS is charged on gross salary which is basic salary +
dearness allowance and TDS rate is as per following table:
Gross Salary TDS
Rs. 100000 and below NIL
Above Rs. 100000 10% on excess over 100000
DA is 74% of Basic Salary for all. Use appropriate member function.
Ans :- class employee
{
String name;
int basicSalary;
double DA;
employee(String name,int basicSalary)
{
this.name = name;
this.basicSalary = basicSalary;
this.DA = 0.74 * basicSalary;
}
public double grossSalary()
{
return basicSalary + DA;
}
public double tds()
{ double tds = 0;
if (grossSalary()<=100000 )
tds = 0;
else if (grossSalary()>100000)
tds = grossSalary() * 0.1;
Output :-
Practical :- 4
String name;
int age;
void display()
this.name = name;
this.age = age;
this.noOfOdi = noOfOdi;
this.noOfTest = noOfTest;
super(name,age);
this.noOfOdi = noOfOdi;
this.noOfTest = noOfTest;
display();
m.printInfo();
Output :-
int ConstructionCost=500;
int AddCostFlat=200000;
int AddCostBun=200;
int LandCost=400;
float computePrice();
String name;
float constructionArea;
this.name=name;
this.constructionArea=constructionArea;
this.landArea=landArea;
return constructionArea*(ConstructionCost+AddCostBun)+landArea*LandCost;
String name;
float constructionArea;
this.name=name;
this.constructionArea = constructionArea;
System.out.println(b.computePrice());
System.out.println(f.computePrice());
Output :-
this.height=height;
this.radius=radius;
this.width = width;
this.height = height;
return height*width;
return 2*(height+width);
System.out.println("Rectangle :");
System.out.println("Area : "+r.area());
r.describe();
System.out.println("Perimeter : "+r.perimeter());
System.out.println();
System.out.println("Sphere :");
System.out.println("Volume : "+s.volume());
s.describe();
System.out.println("Cone :");
System.out.println(""+c.volume());
c.describe();
Output :-
Practical :- 5
Inner Class :
Define two nested classes: Processor and RAM inside the outer class: CPU
with following data members
class CPU {
double price;
class Processor{ // nested class
double cores;
double catch()
String manufacturer;
double getCache()
void displayProcesorDetail()
}
protected class RAM{ // nested protected class
// members of protected nested class
double memory;
String manufacturer;
Double clockSpeed;
double getClockSpeed()
void displayRAMDetail()
}
}
public CPU(){}
public Processor() {}
double getCache()
return 16;
void displayProcesorDetail()
double getClockSpeed()
return clockSpeed;
void displayRAMDetail()
cpu.displayProcesorDetail();
r.displayRAMDetail();
Output :-
Packages :
this.name = name;
return 0.0;
super(name);
this.radius = radius;
circle.display();
Output :-
Output :-
Practical :- 6
1. Declare a class InvoiceDetail which accepts a type parameter which is
of type Number with following data members
class InvoiceDetail {
private String invoiceName;
private N amount;
private N Discount // write getters, setters, and constructors
}
Call the methods in Main class
invoice.setAmount(90.0);
invoice.setDiscount(9.0);
Output :-
Collections.sort(books);
Output :-
Practical :- 7
1. Write a program for creating a Bank class, which is used to manage
the bank account of customers. Class has two methods, Deposit () and
withdraw (). Deposit method display old balance and new balance
after depositing the specified amount. Withdrew method display old
balance and new balance after withdrawing. If balance is not enough
to withdraw the money, it throws ArithmeticException and if balance
is less than 500rs after withdrawing then it throw custom exception,
NotEnoughMoneyException.
Output :-
try {
System.out.print("Enter the number of elements: ");
n = scanner.nextInt();
if (n == 0)
{
throw new Exception("Number of elements cannot be zero");
}
} catch (Exception e) {
System.out.println(e.getMessage());
return;
}
try {
for (int i = 0; i < n; i++) {
System.out.print("Enter element " + (i + 1) + ": ");
int element = scanner.nextInt();
if (element <= 0) {
throw new Exception("Element value must be positive");
}
A[i] = element;
}
} catch (Exception e) {
System.out.println(e.getMessage());
return;
}
int sum = 0;
for (int i = 0; i < n; i++) {
sum += A[i];
}
double average = (double) sum / n;
System.out.println("The average is: " + average);
}
Output :-