Practical 1
Basic Programs
1.1 Study of class path and java runtime environment
There are two ways of set a path variable
1) By ordinary method
Open a Environment Variables Dialog box by search box
Click on New Button -> Enter PATH as a variable name and copy the path of bin file of JDK and paste to the
variable value -> click on OK.
2) By Command Prompt
Syntax: path [[<drive>:]<path>[;...][;%PATH%]]
1.2 Write a program to Implement command line calculator.
package [Link];
import [Link];
1 12302040701175 Siya Soni
public class Calculator { public static void
main(String[] args) { double
n1,n2,ans; int c;
Scanner sc = new Scanner([Link]);
[Link]("Enter two
numbers:"); n1=[Link]();
n2=[Link]();
[Link]("Enter your choice:");
[Link]("1. Addition: ");
[Link]("2. Subtraction: ");
[Link]("3. Multiplication: ");
[Link]("4. Division: ");
c=[Link]();
switch (c) { case 1 : ans = n1 + n2;
[Link]("Addition is:"+ans); break; case
2 : ans = n1 - n2;
[Link]("Subtraction is:"+ans); break;
case 3 : ans = n1 * n2;
[Link]("Multiplication is:"+ans); break; case
4 : ans = n1 / n2;
[Link]("Division is:"+ans); break; default :
[Link]("Invalid case");
return ; } }
}
Output:
2 12302040701175 Siya Soni
1.2 Write a program to Print Fibonacci Series.
package [Link];
public class Fibonacci {
public static void main(String[] args) {
int x=0, y=1, z, count=11;
[Link]("Fibonacci Series: ");
[Link](x+" "+y); for(int i=2; i<count; i++) { z=
x+y; [Link](" "+z); x = y;
y = z; } }
}
Output:
Practical 2
Array 2.1 Define a class Array with following member
Field: int
3 12302040701175 Siya Soni
data[]
Function: Array( )
Array(int size)
Array(int data[]) void
Reverse _an _array () int
Maximum _of _array ()
int Average_of _array()
void Sorting () void
display() int search(int
no) int size()
Use all the function in main method. Create different objects with different
constructors.
import [Link];
class Array{
public int [] data;
public Array()
{ int[] data;
}
public Array(int n){
int[] data = new int[n];
} public Array(int... arr){ int[]
data = new int[[Link]];
[Link] = arr;
}
void printArray(){
for (int i = 0; i < [Link]; i++) [Link]("a[" + i + "]: " + data[i]);
}
void reverseArray(){
int l = [Link]; int n =
[Link](l,2); int
temp;
for (int i = 0;i<n;i++)
{ temp = data[i];
data[i] = data[l-1-i];
data[l-1-i] = temp; }
}
4 12302040701175 Siya Soni
void maximumOfArray(){ int max
= Integer.MIN_VALUE; for (int
e: data){ if (e>max)
max = e; }
[Link](max); } void
averageOfArray(){
int sum = 0;
for (int i =0;i<=[Link]-1;i++)
{ sum += data[i]; }
[Link](sum/[Link]);
}
void sorting()
{ [Link](data);
for (int e:data) {
[Link](e); }
}
void display(int n) {
[Link]("The value of your variable is: "+data[n]); }
void search(int n){ for (int i = 0;i < [Link];i++) { if (n ==
data[i])
[Link](i); }
} void size(){
[Link]("Size of Array: "+[Link]); } }
public class Pr2_1 {
public static void main(String[] args) {
Array a = new Array(4,55,447,54,30,5455);
[Link]("Printing Array: ");
[Link]();
[Link]("Reverse Array:");
[Link]();
[Link]();
[Link]("Maximum of Array: ");
[Link]();
[Link]("Average of Array: ");
[Link]();
[Link]("Sorted Array:");
[Link]();
[Link](3);
5 12302040701175 Siya Soni
[Link](55);
[Link]();
}
}
Output:
2.1 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( )
6 12302040701175 Siya Soni
Create three object of Matrix class with different constructors in main and
test all the functions in main.
class Matrix{
int row,column; float mat[][] =
{{1,2},{3,4}}; public Matrix(int a[]
[]){} public Matrix(){} public
Matrix(int row,int column){}
public void readMatrix()
{ [Link]("Matrix: ");
for (int i=0;i<2;i++){
for (int j=0;j<2;j++)
[Link]((int)mat[i][j]+" ");
[Link](" ");
}
}
public void transpose()
{
float [][]transpose = new float[2][2];
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
transpose[i][j]= mat[j][i];
}
}
[Link]("Transpose:");
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
[Link]((int)transpose[i][j]+" ");
}
[Link]();//new line
}
}
public void multiplication(float[][] mat2){ float[]
[] c = new float[2][2];
[Link]("Multiplication:");
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
7 12302040701175 Siya Soni
c[i][j]=0; for(int
k=0;k<2;k++)
{ c[i][j]+=mat[i][k]*mat2[k][j];
}
[Link]((int)c[i][j]+" ");
}
[Link]();
}
}
public void displayMatrix(float[][] a){
[Link]();
[Link]("Matrix given: ");
for (float[] fs : a) {
for (float ele : fs) {
[Link](ele + " ");
}
[Link]();
}
}
public void displayMatrix(){
[Link]();
[Link]("Matrix : ");
for (float[] fs : mat) {
for (float ele : fs) {
[Link](ele + " ");
}
[Link]();
}
}
public float maximum_of_array(){
float max=mat[0][0];
for (float[] fs : mat)
{ for (float ele : fs)
{ if(max<ele){
max=ele;
}
}
8 12302040701175 Siya Soni
}
return max;
}
public float average_of_array( ){
float sum=0,avg,i=0;
for (float[] fs : mat) {
for (float ele : fs) {
i++;
sum+=ele;
}
}
avg=sum/i;
return avg;
}
}
public class Pr2_2 {
public static void main(String[] args) {
Matrix a = new Matrix(new int [10][10]);
Matrix b = new Matrix();
Matrix c = new Matrix(2,5); float[]
[] mat2 = {{4,5},{6,9}};
[Link]();
[Link]();
[Link](mat2);
[Link](mat2);
[Link]();
[Link]("Average of an array is :"+b.average_of_array());
[Link]("Maximum of an array is :"+b.maximum_of_array());
}
}
Output:
9 12302040701175 Siya Soni
2.2 Write a program to demonstrate usage of different methods of Wrapper
class .
class wrapper { public static void main(String[] args) {
Integer I = [Link]("12");
[Link]("Integer value: " +I);
Double D = [Link]("12.0");
[Link]("Double value: " +D);
Boolean B = [Link]("true");
[Link]("Boolean value: " +B); } }
Output:
10 12302040701175 Siya Soni
2.3 Write a program to demonstrate usage of String and StringBuffer class.
Code:
public class Example{ public static void
main(String[] args) { String s1="Java";
char ch[]={'S','t','r','i','n','g','s'};
String s2=new String(ch);
String s3=new String("Example");
[Link](s1);
[Link](s2);
[Link](s3);
StringBuffer sb = new StringBuffer("Hello!! My name is Siya ");
[Link](sb); [Link]("Siya");
[Link](sb); [Link](0,7); [Link](sb);
[Link](0,"Welcome!!!"); [Link](sb); [Link]();
[Link](sb);
[Link]([Link]());
}
}
Output:
11 12302040701175 Siya Soni
2.4 Define a class Cipher with following data
Field: String
plainText; int
Functions: Cipher(String plaintext,int key)
String Encryption( )
String Decryption( )
Read string and key from command prompt and replace every character of string
with character which is key place down from current character. Example plainText =
“GCET” Key = 3 Encryption function written following String “ JFHW” Decryption
function will convert encrypted string to original form “GCET”
Code:
import [Link];
class Cipher1{ private
String plaintxt; private
String ciphertxt;
public Cipher1(String txt)
{ [Link] = txt;
}
public String encrypt(){ char[] ptxt =
[Link]();
for(int i =0; i<[Link]; i++ )
{ ptxt[i] = (char)(ptxt[i]+3);
}
[Link] = new String(ptxt);
return new String(ptxt);
}
public String decrypt(){ char[] ptxt =
[Link]();
for(int i =0; i<[Link]; i++ )
{ ptxt[i] = (char)(ptxt[i]-3);
12 12302040701175 Siya Soni
}
return new String(ptxt);
}
}
class Pr2_5{
public static void main(String args[]){
Scanner scanner = new Scanner([Link]);
[Link]("Enter Text: ");
String data = [Link]();
Cipher1 cipher = new Cipher1(data);
[Link]("Cipher Text : " + [Link]());
[Link]("Plain Text : " + [Link]());
}
}
Output:
13 12302040701175 Siya Soni
Practical 3
Basic Program using Class
3.1Create a class BankAccount that has Depositor name , Acc_no, Acc_type, Balance
as Data Members and void createAcc() . void Deposit(), void withdraw() and void
BalanceInquiry as Member Function. When a new Account is created assign next
serial no as account number. Account number starts from 1
Code:
package [Link].pr3_1;
class BankAccount<acc_no> {
String depositor_name;
static int acc_no = 0; String
acc_type; private int
balance;
public void createAccount(String name,String type) {
depositor_name = name;
acc_type = type; acc_no+
+;
}
void deposit(int n){ balance += n;}
void withdraw(int n){balance -= n;}
void balanceInquiry(){
[Link](balance);
}
}
public class Pr3_1 { public static void
main(String[] args) { BankAccount one =
new BankAccount();
BankAccount two = new BankAccount();
14 12302040701175 Siya Soni
[Link]("Krisha","Savings");
[Link](25000);
[Link](2000);
[Link]();
[Link]("Account number of "+ one.depositor_name+" is "+ BankAccount.acc_no);
[Link]("Shushit","Business");
[Link](450000);
[Link](60000);
[Link]();
[Link]("Account number of "+two.depositor_name+" is "+ BankAccount.acc_no);
}
}
Output:
3.2Create 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. Code:
import [Link];
public class CalendarExample {
public static void main(String[] args) {
Calendar calendar = [Link]();
[Link]("Current Date and Time: " + [Link]());
[Link]([Link], 5);
[Link]("5 minutes added to time: " + [Link]()); }
}
15 12302040701175 Siya Soni
Output:
3.3Define a class with the Name, Basic salary and dearness allowance as data
[Link] 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
Code:
package [Link].pr3_3;
class employee{ String name; int
basicSalary; double DA;
employee(String name,int basicSalary)
{
[Link] = name;
[Link] = basicSalary;
[Link] = 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;
16 12302040701175 Siya Soni
return tds;
}
double netSalary(){
return grossSalary() - tds();
}
void getInformation(){
[Link]("Name of the employee: "+name);
[Link]("Basic Salary of the employee: "+basicSalary);
[Link]("Dearness Allowance of the salary: "+DA);
[Link]("TDS of the salary: "+tds());
[Link]("Net Salary of the employee: "+netSalary());
}
}
public class Pr3_3 {
public static void main(String[] args)
{ employee a = new
employee("Prachi",40000); [Link]();
}
}
Output:
17 12302040701175 Siya Soni