COMPUTER PROJECT Name : ANKAN GHOSH Class : Xi section : A Roll no. : 10 School : TRIBENI TISSUES VIDYAPITH ACKNOWLEDGEMENT I would like to express my special thanks of gratitude to my teacher Avishek Paul as well as our principal Ms. Sanghamitra Chatterjee who gave me the golden opportunity to do this wonderful project on the topic Java programming which also helped me in doing a lot of Research and I came to know about so many new things, I am really thankful to them. Secondly I would also like to thank my parents and friends who helped me a lot in finalizing this project within the limited time frame. CONTENT Sl.No Topic Page.No 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. Program No.1 Program No.2 Program No.3 Program No.4 Program No.5 Program No.6 Program No.7 Program No.8 Program No.9 Program No.10 Program No.11 Program No.12 Program No.13 Program No.14 Program No.15 1-4 5-7 8-10 11-13 14-16 17-19 20-22 23-25 26-28 29-31 32-34 35-37 38-40 41-42 43-45 QUESTION 1: Write a program to find the maximum and minimum element and print the sorted 2D array. CODE: import java.util.*; public class Q1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("ENTER THE NUMBER OF ROWS IN THE MATRIX:"); int r = sc.nextInt(); System.out.print("ENTER THE NUMBER OF COLUMNS IN THE MATRIX:"); int c = sc.nextInt();// Prepare matrix System.out.println("ENTER THE ELEMENTS OF THE MATRIX : "); int matrix[][] = new int[r][c]; for(int i = 0; i < r; i++) { for(int j = 0; j < c; j++) { matrix[i][j] = sc.nextInt(); } } System.out.println("ENTERED MATRIX:"); for(int i = 0; i < r; i++) { for(int j = 0; j < c; j++) { System.out.print(" " +matrix[i][j]+"\t"); } System.out.println(); }// call method to find min and max in matrix findMinAndMax(matrix); }// Method to find maximum and minimum in matrix private static void findMinAndMax(int[][] matrix) { int max = matrix[0][0]; int min = matrix[0][0]; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { if(max < matrix[i][j]) { max = matrix[i][j]; } else if(min > matrix[i][j]) { min = matrix[i][j]; } } } System.out.println("THE MAX NUMBER IN THE MATRIX:" + max); System.out.println("THE MIN NUMBER IN THE MATRIX:" + min); } } VARIABLE DESCRIPTION: VARIABLE TYPE DESCRIPTION r int c int i int to store the value of the no. of rows. to store the value of the no. of columns. for loop variable j int for loop variable min int to store the value of the min variable max int matrix int[] to store the value of max variable 2D array to create a matrix OUTPUT SCREEN: QUESTION 2: Write a program to sort and print boundary element. CODE: import java.util.*; class Q2 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("ENTER THE ROWS OF THE MATRIX:"); int r = sc.nextInt(); System.out.print("ENTER THE COLUMNS OF THE MATRIX:"); int c = sc.nextInt(); int a[][] = new int[r][c]; int i,j; System.out.println("ENTER THE ELEMENTS OF THE MATRIX:"); for(i = 0; i < r; i++) { for(j = 0; j < c; j++) { a[i][j]=sc.nextInt(); } } System.out.println("THE MATRIX IS:"); for(i = 0; i < r; i++) { for(j = 0; j < c; j++) { System.out.print(a[i][j]+"\t"); } System.out.println(); } int B[] = new int[r*c]; //creating a 1D Array of size 'r*c' int x = 0; for(i = 0; i < r; i++) { for(j = 0; j < c; j++) { B[x] = a[i][j]; x++; } }//Sorting the 1D Array in Ascending Order int t = 0; for(i = 0; i < (r * c) - 1; i++) { for(j = i + 1; j < (r * c); j++) { if(B[i] > B[j]) { t = B[i]; B[i] = B[j]; B[j] = t; } } }//Saving the sorted 1D Array back into the 2D Array x = 0; for(i = 0; i < r; i++) { for(j = 0; j < c; j++) { a[i][j] = B[x]; x++; } }//Printing the sorted 2D Array System.out.println("THE SORTED ARRAY:"); for(i = 0; i < r; i++) { for(j = 0; j < c; j++) { System.out.print(a[i][j]+"\t"); } System.out.println(); } System.out.println("THE BOUNDARY ELEMENTS OF THE MATRIX ARE:"); for(i = 0; i < r; i++) { for(j = 0; j < c; j++) { if(i==0 || j==0 || i == r-1 || j == c-1) //condition for accessing boundary elements System.out.print(a[i][j]+"\t"); else System.out.print(" \t"); } System.out.println(); } } } VARIABLE DESCRIPTION: VARIABLE m,n TYPE int i,j a[] B[] t int int[] int[] int DESCRIPTION to store the value of rows and columns for loop variables to make 2D array 1D array temp variable for replace variable OUTPUT SCREEN: QUESTION 3: Write a program to calculate the saddle point in 2D array. CODE: import java.util.*; class Q3 { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.print("ENTER THE SIZE OF THE MATRIX:"); int m=sc.nextInt(); int a[][]=new int[m][m];//Inputting the matrix for(int i=0;i<m;i++) { for(int j=0;j<m;j++) { System.out.print("ENTER AN ELEMENT("+i+","+j+"):"); a[i][j]=sc.nextInt(); } }//Printing the matrix System.out.println("THE MATRIX IS:"); for(int i=0;i<m;i++) { for(int j=0;j<m;j++) { System.out.print(a[i][j]+"\t"); } System.out.println(); } int min,max,c,flag=0; for(int i=0;i<m;i++) { min=a[i][0]; c=0; for(int j=0;j<m;j++) { if(a[i][j]<min) { min=a[i][j]; c=j; } } max=a[0][c]; for(int k=0;k<m;k++) { if(a[k][c]>max) { max=a[k][c]; } } if(max==min) { System.out.println("THE SADDLE POINT IS:"+min); flag=1; } } if(flag==0) { System.out.println("...THERE IS NO SADDLE POINT..."); } } } VARIABLE DESCRIPTION: VARIABLE x,y i TYPE int int DESCRIPTION method variables for loop variable hcf int to store the HCF a,b int main() variables taken input to find HCF OUTPUT SCREEN: QUESTION 4: Write a Program to check for Symmetric matrix. CODE: import java.util.*; class Q4 { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.print("ENTER THE NUMBER OF ELEMENTS:"); int m=sc.nextInt(); int a[][]=new int[m][m]; if(m>2 && m<10)// Checking for valid input of rows and columns size { System.out.println("ENTER THE ELEMENTS:"); for(int i=0;i<m;i++) { for(int j=0;j<m;j++) { a[i][j]=sc.nextInt(); } } System.out.println("THE MATRIX IS:"); for(int i=0;i<m;i++) { for(int j=0;j<m;j++) { System.out.print(a[i][j]+"\t"); } System.out.println(); } int flag = 0; for(int i=0;i<m;i++) { for(int j=0;j<m;j++) { if(a[i][j] != a[j][i]) { flag = 1; break; } } } if(flag == 1) System.out.println("THE GIVEN MATRIX IS NOT SYMMETRIC"); else System.out.println("THE GIVEN MATRIX IS SYMMETRIC"); } } } VARIABLE DESCRIPTION: VARIABLE first second TYPE int int DESCRIPTION 1 value of the series 2nd value of the series third int 3rd value of the series n int while loop variable OUTPUT SCREEN: st QUESTION 5: Write A Program To generate the mirror image of 2D array. CODE: import java.util.*; class Q5 { int a[][],b[][],m; void input() { Scanner sc=new Scanner(System.in); System.out.print("ENTER THE SIZE OF THE MATRIX:"); m=sc.nextInt(); a=new int [m][m];b=new int [m][m]; System.out.println("ENTER THE ELEMENTS:"); for(int i=0;i<m;i++) { for(int j=0;j<m;j++) { a[i][j]=sc.nextInt(); } } } void Calculate() { int i,j,k,l; for(i=0;i<m;i++) { for(j=0;j<m;j++) { for(k=0;k<m;k++) { if((j+k)==(m-1)) b[i][k]=a[i][j]; } } } } void Output() { int i,j; for(i=0;i<m;i++) { for(j=0;j<m;j++) { System.out.print(b[i][j]+" "); } System.out.println(); } } void output2() { int i,j; for(i=0;i<m;i++) { for(j=0;j<m;j++) { System.out.print(a[i][j]+" "); } System.out.println(); } } public static void main(String args[]) { Q5 ob=new Q5(); ob.input(); System.out.println("THE MATRIX IS:"); ob.output2(); System.out.println("THE MIRROR MATRIX IS:"); ob.Calculate(); ob.Output(); } } VARIABLE DESCRIPTION: VARIABLE num f i TYPE int int int DESCRIPTION constructor initialisation constructor initialisation for loop variable OUTPUT SCREEN: QUESTION 6: Define a class result described as below: Data members I instance variables: name,s1 , s2, s3 (marks in 3 subjects), maximum, Member methods (i)result ()- A parameterized constructor to initialize the data members (ii)void accept()- To accept the details of a student (iii)void Compute()- To compute the maximum out of three marks (iv) void display()-To display the name, marks in three subjects, maximum and Write a main method to create an object of a class and call the above member methods. CODE: import java.util.*; public class result { String n; int s1,s2,s3,max; void accept()//accepting the values { Scanner sc=new Scanner(System.in); System.out.print("ENTER YOUR NAME:"); n = sc.nextLine(); System.out.print("ENTER YOUR MARKS IN 1ST SUBJECT:"); s1=sc.nextInt(); System.out.print("ENTER YOUR MARKS IN 2ND SUBJECT:"); s2=sc.nextInt(); System.out.print("ENTER YOUR MARKS IN 3RD SUBJECT:"); s3=sc.nextInt(); } void Compute()//calculating the maximum { max=Math.max(s1,Math.max(s2,s3)); } void display()//displaying the details { System.out.println("YOUR NAME:"+n); System.out.println("YOUR MARKS IN 1ST SUBJECT:"+s1); System.out.println("YOUR MARKS IN 2ND SUBJECT:"+s2); System.out.println("YOUR MARKS IN 3RD SUBJECT:"+s3); System.out.println("THE MAXIMUM MARKS IS:" +max); } public static void main(String[]args)//invoking main() { result op=new result(); op.accept(); op.Compute();// calling the methods op.display(); }} VARIABLE DESCRIPTION: VARIABLE name s1 s2 s3 max TYPE String int int int int DESCRIPTION To take input the name 1st subject marks 2nd subject marks 3rd subject marks to store the maximum marks OUTPUT SCREEN: QUESTION 7: Write a program in java to initialize the following character arrays and print a suitable message after checking the array whether the two arrays are identical or not . X[]={‘x’,’y’,’z’,’w’} Y[]={‘x’,’y’,’z’,’w’} CODE: public class EQUALARRAYs { public static void main(String[]args) { char X[]={'x','y','z','w'}; char Y[]={'x','y','z','w'}; boolean res=true; if(X.length==Y.length) { for(int i=0;i<X.length;i++) { if(X[i]!=Y[i]) { res=false; } } } else { res=false; } if(res==true) { System.out.println("ARRAYs ARE EQUAL"); } else { System.out.println("ARRAYs ARE NOT EQUAL"); } } } VARIABLE DESCRIPTION: VARIABLE TYPE res int i X[] Y[] int char char DESCRIPTION to store the Boolean variable for loop variable array no. 1 array no. 2 OUTPUT SCREEN: QUESTION 8: Design a function void findMarks(float M[]).The function takes single subscripted variable M[]as function arguments with marks of 10 students. Print average of 10 students along with highest and lowest marks scored by the students with suitable messages. code: import java.util.*; public class find { void findMarks(float M[]) { float total=0; for(int i=0;i<M.length;i++)//to find the total { total=total+M[i]; } float avg=total/10;//to find the average System.out.println("The average is " + avg); float max=0,min=0; max=min=M[0]; for(int j=0;j<M.length;j++)//to find the max and min { if(M[j]>max) { max=M[j]; } if(M[j]<min) { min=M[j]; } } System.out.println("The smallest element is" + min); System.out.println("The largest element is " + max); } public static void main(String[]args)//invoking main() { Scanner sc=new Scanner(System.in); find op=new find(); System.out.println("Enter 10 elements"); int n=10; float m[]=new float[n]; for(int i=0;i<m.length;i++) { m[i]=sc.nextFloat(); } op.findMarks(m);//invoking the function } } VARIABLE DESCRIPTION: VARIABLE TYPE M[],m[] float total avg float float max,min float n i int int DESCRIPTION to declare the arrays in method and in main() to store the total marks to store the average of the marks to store the max and min values to store the array size for loop variable OUTPUT SCREEN: PROGRAM 9: CODE: import java.util.*; class Special_Number { int fact(int q)//to calculate the factorial { int f=1; for(int i=1; i<=q; i++){ f=f*i; } return f; } void printNumbers()//to calculate special numbers { int sum, i, j, fact,r; System.out.println("Numbers between 10 and 200"); for( i=10; i<=200; i++) { sum=0; j=i; while(j!=0)//calculating…. { r=j%10; fact=fact(r); sum=sum+fact; j=j/10; } if(sum==i) System.out.println(i); } } public static void main(String[] args)//invoking main() { Special_Number ob=new Special_Number(); ob.printNumbers(); }} VARIABLE DESCRIPTION: VARIABLE TYPE q f i j r int int int int int fact int DESCRIPTION method variable method factorial variable for loop variable for loop variable to calculate the remainder to calculate the factorial OUTPUT SCREEN: QUESTION 10: CODE: import java.util.*; class swapping{ int a,b; void swap(swapping obj) { obj.a=obj.a+obj.b; obj.b=obj.a-obj.b; obj.a=obj.a-obj.b; } public static void main(String[] args) { swapping obj=new swapping(); Scanner sc=new Scanner(System.in); System.out.print("ENTER A:"); obj.a=sc.nextInt(); System.out.print("ENTER B:"); obj.b=sc.nextInt(); System.out.println("BEFORE INTERCHANGING:"); System.out.println("A = "+obj.a); System.out.println("B = "+obj.b); obj.swap(obj); System.out.println("AFTER INTERCHANGING:"); System.out.println("A = "+obj.a); System.out.println("B = "+obj.b); } } VARIABLE DESCRIPTION: VARIABLE a,b TYPE int DESCRIPTION to store the swapped numbers OUTPUT SCREEN: QUESTION 11: Write a program in Java to input n integer elements in an array, sort them in ascending order using the Bubble sort technique and display. CODE: import java.util.*; class BubbleSort { public static void main (String args[]) { Scanner sc=new Scanner(System.in); System.out.println("ENTER THE TOTAL SIZE OF THE ARRAY:"); int n=sc.nextInt(); //accepting size of array int arr[]=new int[n];//declaring array int i,j,temp; System.out.println("ENTER ARRAY ELEMENTS"); for (i = 0; i < n; i++) { arr[i]=sc.nextInt();//accepting array elements } System.out.println("INPUT:"); for (i = 0; i < n; i++) { System.out.print (arr[i] + "\t"); } System.out.println(); for (i = 0; i < n-1; i++) { for (j = 0; j < n-i-1; j++) { if (arr[j] > arr[j+1])//sorting array elements { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } System.out.println("OUTPUT:");//displaying array elements for (i = 0; i < arr.length; i++) { System.out.print (arr[i] + "\t"); } } } VARIABLE DESCRIPTION: VARIABLE TYPE n i,j int int temp arr int int[] DESCRIPTION Store size of array Loop variables to count array index Store temporary value Store integers OUTPUT SCREEN: QUESTION 12: Write a program to search a number from a set of integers using linear search. CODE: import java.io.*; class LinearSearch { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("ENTER THE TOTAL SIZE OF THE ARRAY:"); int n=Integer.parseInt(br.readLine()); int arr[]=new int[n]; int i,key; System.out.println("ENTER ARRAY ELEMENTS"); for (i = 0; i < n; i++) { arr[i]=Integer.parseInt(br.readLine());//input array } System.out.print("ENTER THE INTEGER TO SEARCH:"); key=Integer.parseInt(br.readLine());//value to be searched System.out.println("INPUT ARRAY:"); for (i = 0; i < n; i++) { System.out.print (arr[i] + "\t"); } System.out.println(); for(i=0;i<arr.length;i++)//searching process { if(key==arr[i]) break; } if(i==n) System.out.println(key+" not found"); else System.out.println(key+" found at index "+i); } } VARIABLE DESCRIPTION: VARIABLE TYPE n int i key arr int int int[] DESCRIPTION To store the size of the array Loop counter Value to be searched Store integers OUTPUT SCREEN: QUESTION 13: Write a program to input a sentence and display the number of spaces, vowels and words present in it. CODE: import java.util.*; class Sentence { public static void main (String args[]) { int len,i,blank=0,vowel=0; char ch; Scanner sc=new Scanner(System.in); System.out.println("ENTER A SENTENCE:"); String str=sc.nextLine();//accepting the sentence len=str.length();//calculate the length str=str.toUpperCase();//convert the input to uppercase for (i = 0; i < len; i++) { ch=str.charAt(i); if(ch==' ') blank++;//count spaces else if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') vowel++;//count vowel } System.out.println("No. OF BLANK SPACES:"+blank); System.out.println("No. OF WORDS:"+(blank+1)); System.out.println("No. OF VOWELS:"+vowel); } } VARIABLE DESCRIPTION: VARIABLE TYPE str len String int i blank vowel ch int int int char DESCRIPTION To store the sentence To store the length of the sentence Loop counter To calculate the spaces To calculate the vowels Temporary variable to store character OUTPUT SCREEN: QUESTION 14: Write a program to check whether a string is a palindrome or not. A palindrome string that reads the same from left to right and vice versa eg. MADAM, ARORA, DAD etc. CODE: import java.util.*; class Palindrome { public static void main (String args[]) { int len,i; char ch; String str,rev=""; Scanner sc=new Scanner(System.in); System.out.println("ENTER THE STRING:"); str=sc.nextLine();//accepting the string len=str.length(); for (i=len-1; i>=0; i--)//loop starting from end of the string { ch=str.charAt(i);//to store every character rev=rev+ch;//to store in a reverse way } if(rev.equalsIgnoreCase(str)) System.out.println("IT IS A PALINDROME"); else System.out.println("IT IS NOT A PALINDROME"); } } VARIABLE DESCRIPTION: VARIABLE TYPE str len String int ch char i rev int int OUTPUT SCREEN: DESCRIPTION To store the sentence To store the length of the sentence Temporary variable to store character Loop counter To store the reverse of the string QUESTION 15: Write a program to accept a name and print only the first letter of each word of the sentence in capital letters separated by a full stop. Sample Input : “Mohandas Karamchand Gandhi” Output : M.K.G. CODE: import java.util.*; class Letters { public static void main (String args[]) { String str; int len,i; char ch; Scanner sc=new Scanner(System.in); System.out.print("ENTER A NAME:");//accepting the sentence str=sc.nextLine(); len=str.length(); System.out.println("THE ENTERED STRING IS:"+str); System.out.println("OUTPUT:"); System.out.print(Character.toUpperCase(str.charAt(0))+".");//prints the first character along with '.' for (i = 0; i < len; i++) { ch=str.charAt(i); if(ch==' ')//if space found System.out.print(Character.toUpperCase(str.charAt(i+1))+".");//prints the next character } System.out.println(); } } VARIABLE DESCRIPTION: VARIABLE TYPE str len String int ch char i rev int int DESCRIPTION To store the sentence To store the length of the sentence Temporary variable to store character Loop counter To store the reverse of the string OUTPUT SCREEN: CONCLUSION This Project has been made for eduction purposes on Java Programming .Java has significant advantages not only as a commercial language but also as a teaching language. It allows students to learn object-oriented programming without exposing them to the complexity of C++. It provides the kind of rigorous compile-time error checking typically associated with Pascal. It allows instructors to introduce students to GUI programming, networking, threads, and other important concepts used in modern-day software. While making this project I have came over many new things , techniques which have helped me decrease my mistakes.