0% found this document useful (0 votes)
2 views

java[1]

The document contains practical programming exercises in Java, including setting up the Java runtime environment, creating a command line calculator, and implementing classes for arrays and matrices. It also covers wrapper classes, string manipulation, and basic banking operations. Additionally, it includes employee salary calculations with TDS deductions and time management using the Calendar class.

Uploaded by

12302040701170
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

java[1]

The document contains practical programming exercises in Java, including setting up the Java runtime environment, creating a command line calculator, and implementing classes for arrays and matrices. It also covers wrapper classes, string manipulation, and basic banking operations. Additionally, it includes employee salary calculations with TDS deductions and time management using the Calendar class.

Uploaded by

12302040701170
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

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 com.mycompany.calculator;

import java.util.Scanner;

1 12302040701175 Siya Soni


public class Calculator { public static void

main(String[] args) { double

n1,n2,ans; int c;

Scanner sc = new Scanner(System.in);

System.out.println("Enter two

numbers:"); n1=sc.nextDouble();

n2=sc.nextDouble();

System.out.println("Enter your choice:");

System.out.println("1. Addition: ");

System.out.println("2. Subtraction: ");

System.out.println("3. Multiplication: ");

System.out.println("4. Division: ");

c=sc.nextInt();

switch (c) { case 1 : ans = n1 + n2;

System.out.println("Addition is:"+ans); break; case

2 : ans = n1 - n2;

System.out.println("Subtraction is:"+ans); break;

case 3 : ans = n1 * n2;

System.out.println("Multiplication is:"+ans); break; case

4 : ans = n1 / n2;

System.out.println("Division is:"+ans); break; default :

System.out.println("Invalid case");

return ; } }

}
Output:

2 12302040701175 Siya Soni


1.2 Write a program to Print Fibonacci Series.
package com.mycompany.fibonacci;
public class Fibonacci {
public static void main(String[] args) {
int x=0, y=1, z, count=11;
System.out.println("Fibonacci Series: ");
System.out.print(x+" "+y); for(int i=2; i<count; i++) { z=
x+y; System.out.print(" "+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 java.util.Arrays;
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[arr.length];
this.data = arr;
}
void printArray(){
for (int i = 0; i < data.length; i++) System.out.println("a[" + i + "]: " + data[i]);
}
void reverseArray(){
int l = data.length; int n =
Math.floorDiv(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; }
System.out.println(max); } void
averageOfArray(){
int sum = 0;
for (int i =0;i<=data.length-1;i++)
{ sum += data[i]; }
System.out.println(sum/data.length);
}
void sorting()
{ Arrays.sort(data);
for (int e:data) {
System.out.println(e); }
}
void display(int n) {
System.out.println("The value of your variable is: "+data[n]); }
void search(int n){ for (int i = 0;i < data.length;i++) { if (n ==
data[i])
System.out.println(i); }
} void size(){
System.out.println("Size of Array: "+data.length); } }
public class Pr2_1 {
public static void main(String[] args) {
Array a = new Array(4,55,447,54,30,5455);
System.out.println("Printing Array: ");
a.printArray();
System.out.println("Reverse Array:");
a.reverseArray();
a.printArray();
System.out.print("Maximum of Array: ");
a.maximumOfArray();
System.out.print("Average of Array: ");
a.averageOfArray();
System.out.println("Sorted Array:");
a.sorting();
a.display(3);

5 12302040701175 Siya Soni


a.search(55);
a.size();
}
}
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()
{ System.out.println("Matrix: ");
for (int i=0;i<2;i++){
for (int j=0;j<2;j++)
System.out.print((int)mat[i][j]+" ");
System.out.println(" ");
}
}
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];
}
}
System.out.println("Transpose:");
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
System.out.print((int)transpose[i][j]+" ");
}
System.out.println();//new line
}
}
public void multiplication(float[][] mat2){ float[]
[] c = new float[2][2];
System.out.println("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];
}
System.out.print((int)c[i][j]+" ");
}
System.out.println();
}
}
public void displayMatrix(float[][] a){
System.out.println();
System.out.println("Matrix given: ");
for (float[] fs : a) {
for (float ele : fs) {
System.out.print(ele + " ");
}
System.out.println();
}
}
public void displayMatrix(){
System.out.println();
System.out.println("Matrix : ");
for (float[] fs : mat) {
for (float ele : fs) {
System.out.print(ele + " ");
}
System.out.println();
}
}
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}};
b.readMatrix();
b.transpose();
b.multiplication(mat2);
b.displayMatrix(mat2);
b.displayMatrix();
System.out.println("Average of an array is :"+b.average_of_array());
System.out.println("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 = Integer.valueOf("12");
System.out.println("Integer value: " +I);

Double D = Double.valueOf("12.0");
System.out.println("Double value: " +D);

Boolean B = Boolean.valueOf("true");
System.out.println("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");
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
StringBuffer sb = new StringBuffer("Hello!! My name is Siya ");
System.out.println(sb); sb.append("Siya");
System.out.println(sb); sb.delete(0,7); System.out.println(sb);
sb.insert(0,"Welcome!!!"); System.out.println(sb); sb.reverse();
System.out.println(sb);
System.out.println(sb.capacity());
}
}

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 java.util.Scanner;
class Cipher1{ private
String plaintxt; private
String ciphertxt;

public Cipher1(String txt)


{ this.plaintxt = txt;
}

public String encrypt(){ char[] ptxt =


plaintxt.toCharArray();

for(int i =0; i<ptxt.length; i++ )


{ ptxt[i] = (char)(ptxt[i]+3);
}
this.ciphertxt = new String(ptxt);
return new String(ptxt);
}

public String decrypt(){ char[] ptxt =


ciphertxt.toCharArray();

for(int i =0; i<ptxt.length; 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(System.in);
System.out.print("Enter Text: ");
String data = scanner.nextLine();
Cipher1 cipher = new Cipher1(data);

System.out.println("Cipher Text : " + cipher.encrypt());


System.out.println("Plain Text : " + cipher.decrypt());
}
}

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 com.mycompany.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(){
System.out.println(balance);
}
}
public class Pr3_1 { public static void
main(String[] args) { BankAccount one =
new BankAccount();
BankAccount two = new BankAccount();

14 12302040701175 Siya Soni


one.createAccount("Krisha","Savings");
one.deposit(25000);
one.withdraw(2000);
one.balanceInquiry();

System.out.println("Account number of "+ one.depositor_name+" is "+ BankAccount.acc_no);

two.createAccount("Shushit","Business");
one.deposit(450000);
one.withdraw(60000);
one.balanceInquiry();

System.out.println("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 java.util.Calendar;
public class CalendarExample {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
System.out.println("Current Date and Time: " + calendar.getTime());
calendar.add(Calendar.MINUTE, 5);
System.out.println("5 minutes added to time: " + calendar.getTime()); }
}

15 12302040701175 Siya Soni


Output:

3.3Define 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

Code:
package com.mycompany.pr3_3;

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;

16 12302040701175 Siya Soni


return tds;
}
double netSalary(){
return grossSalary() - tds();
}
void getInformation(){
System.out.println("Name of the employee: "+name);
System.out.println("Basic Salary of the employee: "+basicSalary);
System.out.println("Dearness Allowance of the salary: "+DA);
System.out.println("TDS of the salary: "+tds());
System.out.println("Net Salary of the employee: "+netSalary());
}
}
public class Pr3_3 {
public static void main(String[] args)
{ employee a = new
employee("Prachi",40000); a.getInformation();

}
}

Output:

17 12302040701175 Siya Soni

You might also like