[go: up one dir, main page]

0% found this document useful (0 votes)
32 views86 pages

Computer Project 12th

Uploaded by

likithpgujjar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views86 pages

Computer Project 12th

Uploaded by

likithpgujjar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 86

Linear Search

Source Code:
import java.util.*;
class LinearSearch
{
public static void main(String args[])
{
Scanner in = new Scanner (System.in);
int n,key,pos=0,i;
System.out.println("Enter the size of array");
n=in.nextInt();
int A[]= new int[n];
System.out.println("Enter the elements of array");
for(i=0;i<n;i++)
{
A[i]=in.nextInt();
}
System.out.println("Enter the key");
key=in.nextInt();
for(i=0;i<n;i++)
{
if(key==A[i])
{
pos=i+1;
break;
}
}
if(pos==0)
{
System.out.println("Key not found");
}
else
{
System.out.println("Key found at "+pos+". position");
}
}
}

1
Output 1:
Enter the elements of array
2
3
1
5
7
Enter the key
5
Key found at 4. Position

Output 2:
Enter the size of array
5
Enter the elements of array
1
4
6
3
7
Enter the key
9
Key not found

BinarySearch
Source Code:
import java.util.*;
class BinarySearch
{
public static void main(String args[])
{
Scanner in = new Scanner (System.in);
int n,key,pos=0,i,mid;
System.out.println("Enter the size of array");
n=in.nextInt();

2
int A[]= new int[n];
System.out.println("Enter the elements of array");
for(i=0;i<n;i++)
{
A[i]=in.nextInt();
}
int high=n-1;
int low=0;
System.out.println("Enter the key");
key=in.nextInt();
while(low<=high)
{
mid=(low+high)/2;
if(key==A[mid])
{
pos=mid+1;
break;
}
else if(A[mid]<key)
{
low=mid+1;
}
else if(key>A[mid])
{
high=mid-1;
}
}
if(pos==0)
{
System.out.println("Key not found");
}
else
{
System.out.println("Key found at "+pos+"position");
}
}
}

3
Output 1:
Enter the size of array
5
Enter the elements of array
12345
Enter the key
4
Key found at 4position

Output 2:
Enter the size of array
5
Enter the elements of array
12345
Enter the key
6
Key not found

4
Bubble Sort
Source Code:
import java.util.*;
class BubbleSort
{
public static void main(String args[])
{
Scanner in = new Scanner (System.in);
int n,i,j,temp=0;
System.out.println("Enter the size of array");
n=in.nextInt();
int A[]= new int[n];
System.out.println("Enter the elements of array");
for(i=0;i<n;i++)
{
A[i]=in.nextInt();
}
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(A[j]>A[j+1])
{
temp=A[j];
A[j]=A[j+1];
A[j+1]=temp;
}
}
}
System.out.println("The sorted array in Ascending order is");
for(i=0;i<n;i++)
{
System.out.println(A[i]);
}
}
}

5
Output:
Enter the size of array
5
Enter the elements of array
2
4
7
1
3
The sorted array in Ascending order is
1
2
3
4
7

6
Selection Sort
Source Code:
import java.util.*;
class SelectionSort
{
public static void main(String args[])
{
Scanner in = new Scanner (System.in);
int n,i,j,small,temp=0;
System.out.println("Enter the size of array");
n=in.nextInt();
int A[]= new int[n];
System.out.println("Enter the elements of array");
for(i=0;i<n;i++)
{
A[i]=in.nextInt();
}
for(i=0;i<n;i++)
{
small=i;
for(j=i+1;j<n;j++)
{
if(A[j]>A[small])
{
small=j;
}
}
temp=A[i];
A[i]=A[small];
A[small]=temp;
}
System.out.println("Sorted array");
for(i=0;i<n;i++)
{
System.out.println(A[i]);
}
}
}

7
Output:
Enter the size of array
5
Enter the elements of array
2
4
1
6
9
Sorted array
9
6
4
2
1

8
Insertion Sort
Source Code:
import java.util.*;
class InsertionSort
{
public static void main(String args[])
{
Scanner in = new Scanner (System.in);
int n,i,j,key;
System.out.println("Enter the size of array");
n=in.nextInt();
int A[]= new int[n];
System.out.println("Enter the elements of array");
for(i=0;i<n;i++)
{
A[i]=in.nextInt();
}
for(i=1;i<n;i++)
{
key=A[i];
j=i-1;
while(j>=0 && key<A[j])
{
A[j+1]=A[j];
j=j-1;
}
A[j+1]=key;
}
System.out.println("Sorted array");
for(i=0;i<n;i++)
{
System.out.println(A[i]);
}
}
}

9
Output:
Enter the size of array
5
Enter the elements of array
2
3
1
6
4
Sorted array
1
2
3
4
6

10
Diagonal Matrix
Source Code:
import java.util.*;
class MatrixDiagonals
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int a[][],s1=0,s2=0,m,i,j,f=1;
System.out.println("Enter the value of m:");
m=in.nextInt();
if(m>=10)
System.out.println("Matrix size is out of range");
else
{
a=new int[m][m];
System.out.println("Enter the elements of array");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
a[i][j]=in.nextInt();
}
}
System.out.println("The orginal matrix");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
System.out.print(" "+a[i][j]);
}
System.out.println();
}
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if(i==j)
s1+=a[i][j];
if(i+j==m-1)
s2+=a[i][j];
if(a[i][j]!=a[i][j])

11
{
f=0;
}
}
}
if(f==1)
System.out.println("The given matrix is symmetric");
else
System.out.println("The given matrix is not symmetric");
System.out.println("Sum of the left diagonals="+s1);
System.out.println("Sum of the right diagonals="+s2);
}
}
}

12
Output:
Enter the value of m:
3
Enter the elements of array
123456789
The orginal matrix
1 2 3
4 5 6
7 8 9
The given matrix is symmetric
Sum of the left diagonals=15
Sum of the right diagonals=15

13
Transpose Matrix
Source Code:
//transpose matrix

import java.util.*;

class transpose

public static void main(String args[])

Scanner in=new Scanner(System.in);

int m,n,i,j;

System.out.println("Enter the order of the matrix");

m=in.nextInt();

int a[][]=new int[m][m];

if(m<2&&m>10)

System.out.println("Wrong order");

System.exit(0);

System.out.println("Enter the elements of the matrix");

for(i=0;i<m;i++)

for(j=0;j<m;j++)

a[i][j]=in.nextInt();

System.out.println("Original matrix");

for(i=0;i<m;i++)

14
{

for(j=0;j<m;j++)

System.out.print(a[i][j]+" ");

System.out.println();

System.out.println("Resultant matrix");

for(j=0;j<m;j++)

for(i=m-1;i>=0;i--)

System.out.print(a[i][j]+" ");

System.out.println();

int sum=0;

for(i=0;i<m;i++)

for(j=0;j<m;j++)

if(i==0&&j==0||i==0&&j==m-1||i==m-1&&j==0||i==m-1&&j==m-1)

sum=sum+a[i][j];

System.out.println("sum of corner elements= "+sum);

15
Output:
Enter the order of the matrix
3
Enter the elements of the matrix
1
2
3
4
5
6

7
8
9
Original matrix
1 2 3
4 5 6
7 8 9
Resultant matrix
7 4 1
8 5 2
9 6 3
sum of corner elements= 20

16
Wordremove
Source Code:
import java.util.*;
class Wordremove
{
public static void main (String args[])
{
Scanner in= new Scanner(System.in);
String word,st1,st2="",st3="";
int i,t,n;
char ch;
System.out.println("Enter a sentence ending with (.)or(?)or(!)");
st1=in.nextLine().toUpperCase();
System.out.println("Enter the word to be removed:");
word=in.next();
System.out.println("Enter position number of the word to be removed");
n=in.nextInt();
ch=st1.charAt(st1.length()-1);
st1=st1.substring(0,st1.length()-1);
if (ch=='.'||ch=='?'||ch=='!')
{
StringTokenizer st=new StringTokenizer(st1);
t=st.countTokens();
for(i=1;i<=t;i++)
{
st2=st.nextToken().trim();
if(st2.equals(word)==false ||i!=n)
st3=st3+' '+st2;
}
System.out.println("Sentence after removing word:");
System.out.println(st3);
}
else
System.out.println("Invalid Sentence!!");
}
}

17
Output 1:
Enter a sentence ending with (.)or(?)or(!)
AS YOU SOW,SO SO YOU REAP.
Enter the word to be removed:
SO
Enter position number of the word to be removed
4
Sentence after removing word:
AS YOU SOW,SO YOU REAP

Output 2:
Enter a sentence ending with (.)or(?)or(!)
AS YOU SOW,SO SO YOU REAP
Enter the word to be removed:
SO
Enter position number of the word to be removed
4
Invalid Sentence!!

18
TeamNames
Source Code:
import java.io.*;
class TeamNames
{
public static void main()throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int n,i,j,max;
System.out.println("Enter number of teams");
n=Integer.parseInt(in.readLine());
if(n<=2||n>=9)
System.out.println("Invalid Input");
else
{
String name[]=new String[n];
int len[]=new int[n];
for(i=0;i<n;i++)
{
name[i]=in.readLine();
len[i]=name[i].length();
}
max=0;
for(i=0;i<n;i++)

if(len[i]>max)
max=len[i];
for(i=0;i<max;i++)
{
for(j=0;j<n;j++)
{
if(i<len[j])
System.out.print(name[j].charAt(i)+"\t");
else
System.out.print("\t");
}
System.out.println();
}
}
}
}

19
Output :
Enter number of teams
4
INDIA
ENGLAND
AUSTRALIA
NEPAL
I E A N
N N U E
D G S P
I L T A
A A R L
N A
D L
I
A

20
DisplayVowels
Source Code:
//a program to count and display vowels and consonants
import java.util.*;
class DisplayVowels
{
public static void main(String args[])
{
String s,st,str="",p;
char c,ch;
int i,j,m,n,t=0,vol,con;
Scanner in=new Scanner(System.in);
System.out.println("Enter a sentence terminated by '.'or '?'");
s=in.nextLine();
m=s.length();
if(s.charAt(m-1)=='.'||s.charAt(m-1)=='?')
{
for(i=0;i<m;i++)
{
c=s.charAt(i);
if(c==' '||c=='.'||c=='?')
{
p=s.substring(t,i);
st=Character.toUpperCase(p.charAt(0))+p.substring(1);
str=str+' '+st;
t=i+1;
}
}
System.out.println(str);
p="";
System.out.println("Words\t"+"Vowels\t"+"Consonants");
str=str.trim();
str=str+' ';
m=str.length();
for(i=0;i<m;i++)
{
c=str.charAt(i);
if(c!=' ')
p=p+c;
else
{
n=p.length();vol=0;

21
for(j=0;j<n;j++)
{
ch=p.charAt(j);
if("AEIOUaeiou".indexOf(ch)>=0)
vol++;
}
con=n-vol;
System.out.println(p+"\t"+vol+"\t"+con);
p="";
}
}
}
else
System.out.println("Invalid Input");
}
}

22
Output1 :
Enter a sentence terminated by '.'or '?'
Understandig ISC Computer Science.
Understandig ISC Computer Science
Words Vowels Consonants
Understandig 4 8
ISC 1 2
Computer 3 5
Science 3 4

Output2 :
Enter a sentence terminated by '.'or '?'
Understandig ISC Computer Science
Invalid Input

23
Factorial Using Recursive
Source Code:
// A program on factorial
import java.util.*;
class Factorial
{
double fact(double n)
{
if(n==0)
return(1);
else
return(n*fact(n-1));
}

public static void main(String args[])


{
Factorial ob=new Factorial();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number to find factorial");
double n=sc.nextDouble();
System.out.println("Factorial of the nth number is:"+ob.fact(n));
}
}

24
Output:
Enter the number to find factorial
5
Factorial of the nth number is:120.0

25
Fibonacci Using Recursive
Source Code:
import java.util.Scanner;
public class Fibonacci
{
// Recursive method to find nth Fibonacci number
public static int fibonacci(int n)
{
if (n == 0)
{
return 0;
}
else if (n == 1)
{
return 1;
}
else
{
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
// Input the number of terms in the Fibonacci sequence
System.out.print("Enter the number of terms in Fibonacci sequence: ");
int t = scanner.nextInt();
System.out.println("Fibonacci sequence up to " + t + " terms:");
// Loop to print the Fibonacci sequence
for (int i = 0; i < t; i++)
{
System.out.print(fibonacci(i) + " ");
}
}
}

26
Output:
Enter the number of terms in Fibonacci sequence: 5
Fibonacci sequence up to 5 terms:
01123

27
GCD Using Recursive
Source Code:
import java.util.Scanner;
public class GCD
{
// Recursive method to find GCD
public int gcd(int a, int b)
{
if (b == 0)
{
return a;
}
return gcd(b, a % b);
}
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
GCD ob=new GCD();
// Input two numbers from the user
System.out.print("Enter the first number: ");
int n1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int n2 = scanner.nextInt();
// Calculate GCD
int r= ob.gcd(n1, n2);
// Display the result
System.out.println("The GCD of " + n1 + " and " + n2 + " is: " + r);
}
}

28
Output:
Enter the first number: 45
Enter the second number: 65
The GCD of 45 and 65 is: 5

29
LCM Using Recursive
Source Code:
import java.util.*;
public class LCMUsingRecursion
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int n1 = sc.nextInt();
System.out.print("Enter second number: ");
int n2 = sc.nextInt();
int lcm = findLCM(n1, n2, Math.max(n1, n2));
System.out.println("The LCM of " + n1 + " and " + n2 + " is: " + lcm);
}

public static int findLCM(int n1, int n2, int m)


{
if (m % n1 == 0 && m % n2 == 0)

{
return m;
}
return findLCM(n1, n2, m + 1);
}
}

30
Output:
Enter first number: 14
Enter second number: 15
The LCM of 14 and 15 is: 210

31
Tower Of Hanoi Using Recursive
Source Code:
// TowerOfHanoi
import java.util.*;
public class Tower
{
public void shift(int n,String first,String middle,String last)
{
if(n==1)
{
System.out.println("Disc moved from\t"+first+"->"+last);
}
else
{
shift(n-1,first,last,middle);
System.out.println("Disc moved from\t"+first+"->"+last);
shift(n-1,middle,first,last);
}
}
public static void main()
{
Tower ob = new Tower();
System.out.print("Enter number of discs");
Scanner sc = new Scanner(System.in);
int nod = sc.nextInt();
ob.shift(nod,"A","B","C");
}
}

32
Output:
Enter number of discs 2
Disc moved from A->B
Disc moved from A->C
Disc moved from B->C

33
Decimal To Binary Using Recursive
Source Code:
//A program to convet decimal to binary

import java.util.Scanner;

public class DecimalToBinary

public static void main(String[] args)

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a decimal number: ");

int n = scanner.nextInt();

System.out.println("Binary representation: " + decimalToBinary(n));

public static String decimalToBinary(int n)

if (n == 0)

return "";

return decimalToBinary(n / 2) + (n % 2);

34
Output:
Enter a decimal number: 25
Binary representation: 11001

35
Binary To Decimal Using Recursive
Source Code:
import java.util.Scanner;

public class BinaryToDecimal // Recursive method to convert binary to decimal

public static int binaryToDecimal(int bin)

if (bin == 0)

return 0;

else

return (bin % 10) + 2 * binaryToDecimal(bin / 10);

public static void main(String[] args)

Scanner scanner = new Scanner(System.in);

// Input a binary number

System.out.print("Enter a binary number: ");

int bin = scanner.nextInt();

// Convert binary to decimal

int de= binaryToDecimal(bin);

// Display the result

System.out.println("The decimal equivalent of binary " + bin + " is: " + de);

36
Output:
Enter a binary number: 110001
The decimal equivalent of binary 110001 is: 49

37
Decimal To Octal Using Recursive
Source Code:
import java.util.Scanner;
public class decimalToOctal
{
// Recursive method to convert dimal to octal
public static String decimalToOctal(int d)
{
if (d == 0)
{
return "";
}
else
{
return decimalToOctal(d / 8) + (d % 8);
}
}
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
// Input a dimal number
System.out.print("Enter a decimal number: ");
int d = scanner.nextInt();
// Convert dimal to octal
String oct = decimalToOctal(d);
// Edge case: if the dimal number is 0
if (oct.isEmpty())
{
oct = "0";
}
// Display the result
System.out.println("The octal equivalent of decimal " + d + " is: " + oct);
}
}

38
Output:
Enter a decimal number: 55461
The octal equivalent of decimal 55461 is: 154245

39
Decimal To Hexadecimal Using Recursive
Source Code:
//A program to convert decimal to Hexadecimal
import java.util.Scanner;
public class DecimalToHexadecimal
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a decimal number: ");
int d = scanner.nextInt();
System.out.println("Hexadecimal representation: " + decimalToHexadecimal(d));
}
public static String decimalToHexadecimal(int d)

if (d == 0)

return "";

int r = d % 16;

char h;

if (r < 10)

h = (char) ('0' + r);

} else

h = (char) ('A' + (r - 10));

return decimalToHexadecimal(d / 16) + h;

40
Output:
Enter a decimal number: 148975261
Hexadecimal representation: 8E12E9D

41
Binary Search Using Recursive
Source Code:
//A program on binary search using recursion
import java.util.*;
public class BinarySearchRecursive
{
public static int binarySearch(int[] A, int key, int low, int high)
{
if (low <= high)
{
int mid = low + (high - low) / 2;

if (A[mid] == key)
{
return mid;
}
else if (A[mid] < key)
{
return binarySearch(A, key, mid + 1, high);
}
else
{
return binarySearch(A, key, low, mid - 1);
}
}
return -1;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = sc.nextInt();
int[] A = new int[n];
System.out.println("Enter the elements in sorted order:");
for (int i = 0; i < n; i++)
{
A[i] = sc.nextInt();
}
System.out.print("Enter the element to search for: ");
int key = sc.nextInt();
int r = binarySearch(A, key, 0, A.length - 1);

42
if (r == -1)
{
System.out.println("Element not found.");
}
else
{
System.out.println("Element found at index: " + r);
}
}
}

43
Output1:
Enter the number of elements: 5
Enter the elements in sorted order:
1
2
3
4
5
Enter the element to search for: 3
Element found at index: 2

Output2:
Enter the number of elements: 5
Enter the elements in sorted order:
1
2
3
4
5
Enter the element to search for: 6
Element not found.

44
Area Overloading
Source Code:
import java.util.Scanner;

class Shape

// Method to calculate the area of a square

public double area(double side)

return side * side;

// Overloaded method to calculate the area of a rectangle

public double area(double l, double b)

return l * b;

// Overloaded method to calculate the area of a circle

public double area(double r, boolean C)

return Math.PI * r * r; // Area of circle: π * r²

public class AreaOverloading

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

Shape ob = new Shape();

// Input and calculate area of square

45
System.out.print("Enter the side length of the square: ");

double side = sc.nextDouble();

System.out.println("Area of the square: " + ob.area(side));

// Input and calculate area of rectangle

System.out.print("Enter the length of the rectangle: ");

double l = sc.nextDouble();

System.out.print("Enter the breadth of the rectangle: ");

double b = sc.nextDouble();

System.out.println("Area of the rectangle: " + ob.area(l, b));

// Input and calculate area of circle

System.out.print("Enter the radius of the circle: ");

double r = sc.nextDouble();

System.out.println("Area of the circle: " + ob.area(r, true));

46
Output:
Enter the side length of the square: 14
Area of the square: 196.0
Enter the length of the rectangle: 25
Enter the breadth of the rectangle: 26
Area of the rectangle: 650.0
Enter the radius of the circle: 13
Area of the circle: 530.929158456675

47
Constructor Compare
Source Code:
import java.util.Scanner;

class Person

String name;

int age;

// Default constructor

public Person()

this.name = "Unknown";

this.age = 0;

// Constructor with name only

public Person(String name)

this.name = name;

this.age = 0; // Default age

// Constructor with name and age

public Person(String name, int age)

this.name = name;

this.age = age;

// Method to display person's information

public void display() {

48
System.out.println("Name: " + name + ", Age: " + age);

public void compareStrings(String str1, String str2)

// Using equals() method (compares value)

if (str1.equals(str2))

System.out.println("Using 'equals()': The strings are equal (value comparison).");

else

System.out.println("Using 'equals()': The strings are not equal (value comparison).");

// Using compareTo() method

int result = str1.compareTo(str2);

if (result == 0)

System.out.println("Using 'compareTo()': The strings are equal.");

else if (result < 0)

System.out.println("Using 'compareTo()': '" + str1 + "' is lexicographically less than '" + str2 + "'.");

else

System.out.println("Using 'compareTo()': '"+ str1 +"' is lexicographically greater than '" + str2 + "'.");

49
}

class constcompare

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

Person p1 = new Person(); // Using default constructor

// Input two strings for comparison

System.out.println("\nString Comparison:");

System.out.print("Enter the first string: ");

String str1 = sc.nextLine();

System.out.print("Enter the second string: ");

String str2 = sc.nextLine();

Person p2 = new Person(str1);

Person p3 = new Person(str2,20);

// Compare the two strings

p1.compareStrings(str1, str2);

p2.compareStrings(str1, str2);

p3.compareStrings(str1, str2);

p1.display();

p2.display();

p3.display();

50
Output:
String Comparison:
Enter the first string: rose
Enter the second string: jasmine
Using 'equals()': The strings are not equal (value comparison).
Using 'compareTo()': 'rose' is lexicographically greater than 'jasmine'.
Using 'equals()': The strings are not equal (value comparison).
Using 'compareTo()': 'rose' is lexicographically greater than 'jasmine'.
Using 'equals()': The strings are not equal (value comparison).
Using 'compareTo()': 'rose' is lexicographically greater than 'jasmine'.
Name: Unknown, Age: 0
Name: rose, Age: 0
Name: jasmine, Age: 20

51
Employee Taxing
Source Code:
//A program to calculate the gross salary of an employee using inheritance
import java.util.*;
class Employee
{
String name;
int empcode,basic;
public void getdata()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the name of the employee");
name=in.nextLine();
System.out.println("Enter employee code");
empcode=in.nextInt();
System.out.println("Enter the basic salary");
basic=in.nextInt();
}
public void showdata()
{
System.out.println("Name of employee:"+name);
System.out.println("Employee code:"+empcode);
System.out.println("Basic salary: Rs."+basic);
}
}
class incometax extends Employee
{
double da,hra,gross=0,ansal,scharge=0,tax;
public void calculate()
{
da=basic*50/100;
hra=basic*15/100;
gross=basic+da+hra;
}
public void compute()
{
ansal=gross*12;
if(ansal<=250000)
{
tax=0;
}
if(ansal>=250000 && ansal<=500000)

52
{
tax=(ansal-250000)*.1;
}
if(ansal>=500000 && ansal<=1000000)
{
tax=5000+(ansal-500000)*.2;
}
if(ansal>1000000)
{
scharge=(ansal-1000000)*.1;
}
}
public void display()
{
showdata();
System.out.println("Dearness Allowance: Rs."+da);
System.out.println("House Rent Allowance: Rs."+hra);
System.out.println("Gross Salary: Rs."+gross);
System.out.println("Annual Salary: Rs."+ansal);
System.out.println("Income Tax: Rs."+tax);
System.out.println("Surcharge: Rs."+scharge);
}
}
public class EmployeeTaxing
{
public static void main(String args[])
{
incometax ob=new incometax();
ob.getdata();
ob.showdata();
ob.calculate();
ob.compute();
ob.display();
}
}

53
Output:
Enter the name of the employee
alexander
Enter employee code
1234
Enter the basic salary
40000
Name of employee:alexander
Employee code:1234
Basic salary: Rs.40000
Name of employee:alexander
Employee code:1234
Basic salary: Rs.40000
Dearness Allowance: Rs.20000.0
House Rent Allowance: Rs.6000.0
Gross Salary: Rs.66000.0
Annual Salary: Rs.792000.0
Income Tax: Rs.63400.0
Surcharge: Rs.0.0

54
Sales Report
Source Code:
import java.util.*;

class product

String name;

int code;

double amount;

product(String n,int c,double p)

name=n;

code=c;

amount=p;

public void show()

System.out.println("name of the product:"+name);

System.out.println("product code:Rs."+code);

System.out.println("the amount of the product: Rs."+amount);

}//end of base class

//code for derived class

class sales extends product

int day;

double tax;

double totamt;

public sales(String n1,int c1,double p1,int d,double amount)

55
{

super(n1,c1,p1);

day=d;

tax=0.0;

totamt=amount;

public void compute()

double f=0.0D;

tax=12.4/100.0*totamt;

if(day>30)

f=2.5/100.0*totamt;

totamt = totamt+tax+f;

public void show()


{
super.show();
System.out.println("total amount :Rs."+totamt);
}
}
public class SalesReport

public static void main(String args[])

sales ob=new sales("lux",1001,40,18,100);

ob.compute();

ob.show();

56
Output:
name of the product:lux

product code:Rs.1001

the amount of the product: Rs.40.0

total amount :Rs.112.4

57
Interface
Source Code:
public interface inter1
{
int n1=10;
}
public interface inter2
{
int n2=20;
}

public interface inter3 extends inter1,inter2


{
int n3=20;
}

public class testing implements inter3


{
public void show()
{
System.out.println("n1 ="+n1);
System.out.println("n2 ="+n2);
System.out.println("n3 ="+n3);
}
public static void main(String args[])
{
testing ob=new testing();
ob.show();
}
}

58
Output:
n1 =10

n2 =20

n3 =20

59
Student Records Using Interface
Source Code:
public interface records

public void inputs();

public void outputs();

import java.util.*;

public class STDrecords implements records

String regno;

public String sname;

String comb;

String result;

public void inputs()

Scanner in=new Scanner(System.in);

System.out.println("Enter the student reg no ");

regno=in.next();

System.out.println("Enter the student S-Name ");

sname=in.next();

System.out.println("Enter the student Combinations ");

comb=in.next();

System.out.println("Enter the student Results ");

result=in.next();

60
public void outputs()

System.out.println("Enter the student reg no "+regno);

System.out.println("Enter the student S-Name "+sname);

System.out.println("Enter the student Combinations "+comb);

System.out.println("Enter the student Results "+result);

public static void main(String args[])

Scanner in=new Scanner(System.in);

System.out.println("Enter the Number of students records");

int n=in.nextInt();

STDrecords ob[]= new STDrecords[n];

int i,j;

System.out.println("Enter the Student records");

for ( i = 0; i < ob.length; i++)

ob[i] = new STDrecords(); // Create a new Student object for each index

for(i=0;i<ob.length;i++)

System.out.println("Enter details for Student record" + (i + 1) + ":");

ob[i].inputs();

for(i=0;i<n;i++)

for(j=0;j<n-1;j++)

61
if(ob[j].sname.compareTo(ob[j+1].sname)>0)

STDrecords temp=ob[j];

ob[j]=ob[j+1];

ob[j+1]=temp;

System.out.println("The sorted Student records are");

for(i=0;i<n;i++)

ob[i].outputs();

62
Output:
Enter the Number of students records

Enter the Student records

Enter details for Student record1:

Enter the student reg no

101

Enter the student S-Name

Rajat

Enter the student Combinations

PCMC

Enter the student Results

Pass

Enter details for Student record2:

Enter the student reg no

102

Enter the student S-Name

Manvith

Enter the student Combinations

PCMB

Enter the student Results

Pass

Enter details for Student record3:

Enter the student reg no

103

Enter the student S-Name

Pavan

Enter the student Combinations

PCMC

Enter the student Results

Fail

63
Enter details for Student record4:

Enter the student reg no

104

Enter the student S-Name

Sunil

Enter the student Combinations

PCMB

Enter the student Results

Pass

The sorted Student records are

Enter the student reg no 102

Enter the student S-Name Manvith

Enter the student Combinations PCMB

Enter the student Results Pass

Enter the student reg no 103

Enter the student S-Name Pavan

Enter the student Combinations PCMC

Enter the student Results Fail

Enter the student reg no 101

Enter the student S-Name Rajat

Enter the student Combinations PCMC

Enter the student Results Pass

Enter the student reg no 104

Enter the student S-Name Sunil

Enter the student Combinations PCMB

Enter the student Results Pass

64
Stack
Source Code:
import java.util.*;
class stack
{
int size=5;
int S[]=new int[size];
int top;
public stack()
{
top=-1;
}
public void push(int item)
{
if(top==size-1)
{
System.out.println("stack overflow");
}
else
{
top=top+1;
S[top]=item;
}
}
public void pop()
{
if(top==-1)
System.out.println("stack underflow");
else
{
int del=S[top];
System.out.println("deleted item from the stack is " +del);
top=top-1;
}
}
public void display()
{
if (top==-1)
System.out.println("stack underflow");
else
{
System.out.println("stack items are:");

65
for(int i=top;i>=0;i--)
{
System.out.println(S[i]);
}
}
}
public static void main (String args[])
{
Scanner in =new Scanner(System.in);
stack ob=new stack();
int item;
for(;;)
{
System.out.println("stack menu options");
System.out.println("1.push"+"\t"+"2.pop"+"\n"+"3.display"+"\t"+"4.exit");
System.out.println("pick any one menu option");
int menu=in.nextInt();
switch(menu)
{
case 1:
System.out.println("enter the item");
item=in.nextInt();
ob.push(item);
break;

case 2:
ob.pop();
break;
case 3:

ob.display();
break;

case 4:
System.exit(0);
}
}
}
}

66
Output:
stack menu options

1.push 2.pop

3.display 4.exit

pick any one menu option

enter the item

10

stack menu options

1.push 2.pop

3.display 4.exit

pick any one menu option

enter the item

20

stack menu options

1.push 2.pop

3.display 4.exit

pick any one menu option

enter the item

30

stack menu options

1.push 2.pop

3.display 4.exit

pick any one menu option

enter the item

67
40

stack menu options

1.push 2.pop

3.display 4.exit

pick any one menu option

enter the item

50

stack menu options

1.push 2.pop

3.display 4.exit

pick any one menu option

enter the item

60

stack overflow

stack menu options

1.push 2.pop

3.display 4.exit

pick any one menu option

stack items are:

50

40

30

20

10

stack menu options

68
1.push 2.pop

3.display 4.exit

pick any one menu option

deleted item from the stack is 50

stack menu options

1.push 2.pop

3.display 4.exit

pick any one menu option

deleted item from the stack is 40

stack menu options

1.push 2.pop

3.display 4.exit

pick any one menu option

deleted item from the stack is 30

stack menu options

1.push 2.pop

3.display 4.exit

pick any one menu option

deleted item from the stack is 20

stack menu options

1.push 2.pop

3.display 4.exit

pick any one menu option

69
deleted item from the stack is 10

stack menu options

1.push 2.pop

3.display 4.exit

pick any one menu option

stack underflow

stack menu options

1.push 2.pop

3.display 4.exit

pick any one menu option

stack underflow

stack menu options

1.push 2.pop

3.display 4.exit

pick any one menu option

70
Queue
Source Code:

import java.util.*;

class queue

int size=5;

int Q[]=new int[size];

int r,f;

public queue()

r=-1;

f=0;

public void enqueue(int item)

if(r==size-1)

System.out.println("queue overflow");

else

r=r+1;

Q[r]=item;

71
}

public int dequeue()

int del=0;

if(f>r)

System.out.println("queue underflow");

f=0;

r=-1;

else

del=Q[f];

f=f+1;

return del;

public void display()

if (f>r)

System.out.println("queue underflow");

else

System.out.println("queue items are:");

for(int i=f;i<=r;i++)

72
System.out.println(Q[i]);

public static void main (String args[])

Scanner in =new Scanner(System.in);

queue ob=new queue();

int item;

for(;;)

System.out.println("queue menu options");

System.out.println("1.enqueue"+"\t"+"2.dequeue"+"\n"+"3.display"+"\t"+"4.exit");

System.out.println("pick any one menu option");

int menu=in.nextInt();

switch(menu)

case 1:

System.out.println("enter the item");

item=in.nextInt();

ob.enqueue(item);

break;

case 2:

int d=ob.dequeue();

if(d>0)

System.out.println("deleted item from the queue is " +d);

73
break;

case 3:

ob.display();

break;

case 4:

System.exit(0);

74
Output:
queue menu options

1.enqueue 2.dequeue

3.display 4.exit

pick any one menu option

enter the item

10

queue menu options

1.enqueue 2.dequeue

3.display 4.exit

pick any one menu option

enter the item

20

queue menu options

1.enqueue 2.dequeue

3.display 4.exit

pick any one menu option

enter the item

30

queue menu options

1.enqueue 2.dequeue

3.display 4.exit

pick any one menu option

enter the item

75
40

queue menu options

1.enqueue 2.dequeue

3.display 4.exit

pick any one menu option

enter the item

50

queue menu options

1.enqueue 2.dequeue

3.display 4.exit

pick any one menu option

enter the item

60

queue overflow

queue menu options

1.enqueue 2.dequeue

3.display 4.exit

pick any one menu option

queue items are:

10

20

30

40

50

queue menu options

76
1.enqueue 2.dequeue

3.display 4.exit

pick any one menu option

deleted item from the queue is 10

queue menu options

1.enqueue 2.dequeue

3.display 4.exit

pick any one menu option

deleted item from the queue is 20

queue menu options

1.enqueue 2.dequeue

3.display 4.exit

pick any one menu option

queue items are:

30

40

50

queue menu options

1.enqueue 2.dequeue

3.display 4.exit

pick any one menu option

deleted item from the queue is 30

queue menu options

1.enqueue 2.dequeue

77
3.display 4.exit

pick any one menu option

deleted item from the queue is 40

queue menu options

1.enqueue 2.dequeue

3.display 4.exit

pick any one menu option

deleted item from the queue is 50

queue menu options

1.enqueue 2.dequeue

3.display 4.exit

pick any one menu option

queue underflow

queue menu options

1.enqueue 2.dequeue

3.display 4.exit

pick any one menu option

queue underflow

queue menu options

1.enqueue 2.dequeue

3.display 4.exit

pick any one menu option

78
Single Linked List
Source Code:
import java.util.*;

class Node
{

int data;

Node next;

Node(int data)
{
this.data = data;
this.next = null;
}
}

class LinkedList
{

private Node head;

// Add a new node at the end


public void add(int data)
{

Node newNode = new Node(data);

if (head == null)
{

head = newNode;

return;

Node current = head;

while (current.next != null)


{

79
current = current.next;

current.next = newNode;

// Print the linked list


public void printList()
{
Node current = head;
while (current != null)
{
System.out.print(current.data + " -> ");
current = current.next;
}
System.out.println("null");
}

// Delete a node by value

public void delete(int key)


{

if (head == null) return;

// If head node itself holds the key


if (head.data == key)
{
head = head.next;
return;
}

Node current = head;

Node previous = null;

while (current != null && current.data != key)


{
previous = current;
current = current.next;

80
}

// If the key was not present

if (current == null) return;

// Unlink the node


previous.next = current.next;
}
}
class linking
{

public static void main(String[] args)


{

LinkedList ob = new LinkedList();


Scanner in =new Scanner(System.in);
int item;
for(;;)
{
System.out.println("Linked list menu options");
System.out.println("1.Add"+"\t"+"2.DELETE"+"\n"+"3.display"+"\t"+"4.exit");
System.out.println("pick any one menu option");
int menu=in.nextInt();
switch(menu)
{
case 1:

System.out.println("enter the item");


item=in.nextInt();
ob.add(item);
break;

case 2:
System.out.println("enter the item to delete from list");
int d=in.nextInt();
ob.delete(d);
break;

case 3:
ob.printList();
break;

81
case 4:
System.exit(0);
}

}
}
}

82
Output:
Linked list menu options
1.Add 2.DELETE
3.display 4.exit
pick any one menu option
1
enter the item
10
Linked list menu options
1.Add 2.DELETE
3.display 4.exit
pick any one menu option
1
enter the item
20
Linked list menu options
1.Add 2.DELETE
3.display 4.exit
pick any one menu option
1
enter the item
30
Linked list menu options
1.Add 2.DELETE
3.display 4.exit
pick any one menu option
1
enter the item
40
Linked list menu options
1.Add 2.DELETE
3.display 4.exit
pick any one menu option
3
10 -> 20 -> 30 -> 40 -> null
Linked list menu options
1.Add 2.DELETE
3.display 4.exit
pick any one menu option
2
enter the item to delete from list
20
Linked list menu options

83
1.Add 2.DELETE
3.display 4.exit
pick any one menu option
3
10 -> 30 -> 40 -> null
Linked list menu options
1.Add 2.DELETE
3.display 4.exit
pick any one menu option
4

84
CONCLUSION
Throughout this project, my exploration of Java has significantly deepened my understanding of
object-oriented programming (OOP) principles and their practical applications. By working on
various modules and implementing key features like inheritance, polymorphism, and recursion, I
gained firsthand experience with Java’s versatility and power in developing reliable, scalable
applications. Whether it was designing class hierarchies or writing recursive methods to solve
complex problems, I came to appreciate how Java’s structure promotes efficiency and reusability,
especially when managing more intricate systems.

The project not only exposed me to the technical side of programming but also allowed me to enhance
my problem-solving skills. Each challenge I encountered—be it fixing bugs, optimizing code for
performance, or finding the most effective algorithm—pushed me to think creatively and critically. As
a result, debugging became an integral part of my workflow, enabling me to refine my logical
thinking and strengthen my attention to detail. The process of identifying errors and optimizing
solutions taught me the importance of writing clean, efficient code that not only works but also
performs optimally.

Moreover, this journey reinforced the importance of well-structured code, as the clarity and
maintainability of a project greatly influence its long-term viability. The significance of
modularization became evident as I worked on breaking down the project into smaller, manageable
components, making both the development and testing processes smoother. Similarly, proper
documentation played a key role in ensuring that my code could be easily understood and modified in
the future—a critical aspect when dealing with larger codebases or collaborating with others.

Another key takeaway from this experience was the importance of testing and validation. Writing unit
tests for different parts of the project not only helped me ensure the correctness of my code but also
boosted my confidence in handling edge cases and unexpected inputs. I realized that thorough testing
is vital for creating robust applications, and incorporating test-driven development practices into my
workflow enhanced the overall quality of my project.

This project not only helped me develop technical proficiency in Java but also strengthened my ability
to tackle complex programming problems methodically. The experience has inspired me to further
explore advanced topics in Java, such as concurrency, multithreading, and design patterns, and apply
these skills to future projects. With a solid foundation in both OOP principles and problem-solving
strategies, I feel equipped to take on more sophisticated software development challenges and
continue evolving as a programmer.

85
BIBLIOGRAPHY
 UNDERSTANDING ISC COMPUTER SCIENCE, CLASS 12, AVICHAL
PUBLICATIONS.
 UNDERSTANDING ISC COMPUTER SCIENCE, CLASS 11, AVICHAL
PUBLICATIONS.

86

You might also like