74
Assignment 03 Question 1: Write a program to enter age of 10 people and display those peoples’s age which is greater than average ages. Answer: import java.util.Scanner; class Age { public static void main(String ar[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter the number of peopels:"); int n=sc.nextInt(); int age[]=new int[n]; System.out.println("Enter the age of all peoples::"); int i,sum=0,avg=0; for(i=0;i<n;i++) { System.out.println("Member "+(i+1)); age[i]=sc.nextInt();

Java Assignment 03

Embed Size (px)

DESCRIPTION

sgsits

Citation preview

Assignment 03Question 1: Write a program to enter age of 10 people and display those peopless age which is greater than average ages.Answer:import java.util.Scanner;class Age{ public static void main(String ar[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter the number of peopels:"); int n=sc.nextInt(); int age[]=new int[n]; System.out.println("Enter the age of all peoples::"); int i,sum=0,avg=0;for(i=0;i5Enter the age of all peoples::Member 1 ->12Member 2 ->12 Member 3 ->34Member 4 -> 15Member 5 ->34peoples age greater then averagemember 3- 34member 5- 34

Question 2: Write a menu driven program to the following operation on an array:a)add the element at the first positionb)add the element at last positionc)add element at given positiond)show all elementse)exitAnswer:import java.util.Scanner;class Arraymenu{ public static void main(String arg[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter the number of elements present in array::"); int n=sc.nextInt(); int a[]=new int[n]; int loop=0,size=0,p,i;while(loop==0) {System.out.println("*****MENU*****");System.out.println("1. Add element at first position.");System.out.println("2. Add element at last position.");System.out.println("3. Add element at any position.");System.out.println("4. Display the array.");System.out.println("5. EXIT");System.out.println("\n Enter your choice...");int choice=sc.nextInt(); switch(choice) {case 1:System.out.println("Enter the number you want to insert-");p=sc.nextInt();for(i=size;i>=0;i--) { a[i+1]=a[i]; }a[0]=p;size++;System.out.println("Element added at first position...");break;case 2:System.out.println("Enter the number you want to insert-");p=sc.nextInt();a[size+1]=p;size++;break;case 3:System.out.println("Enter the position where you want to insert-");int q=sc.nextInt();q=q-1;if(size=0&&q=q;i--) { a[i+1]=a[i]; } a[q]=p; size++;}}break;case 4:System.out.println("Array after performing operation is::");for(i=0;i1Enter the number you want to insert- ->12Element added at first position...Do you want to perform again....then press 0 If not then press anything except 0...->0*****MENU*****1. Add element at first position.2. Add element at last position.3. Add element at any position.4. Display the array.5. EXIT Enter your choice...->2Enter the number you want to insert- ->11Do you want to perform again....then press 0 If not then press anything except 0...->0*****MENU*****1. Add element at first position.2. Add element at last position.3. Add element at any position.4. Display the array.5. EXIT Enter your choice...->3Enter the position where you want to insert- ->2Enter the number you want to insert->13Do you want to perform again....then press 0 If not then press anything except 0...->0*****MENU*****1. Add element at first position.2. Add element at last position.3. Add element at any position.4. Display the array.5. EXIT Enter your choice...->4Array after performing operation is::12 13 11

Question 3:Write a menu driven program to perform the following operation:a)add -add at first position -add at last position -add at given position -backb)delete -delete first element -delete second element -delete at given position -delete given element -backc)searchd)sorte)find largest element in arrayf)smallest element of arrayg)average of all elementsh)exitAnswer:import java.util.Scanner;class array{public static void main(String args[]){Scanner sc=new Scanner(System.in);System.out.println("Enter array size");int n=sc.nextInt();int arr[]=new int[n];int c;int t;int p;output out=new output();out.l=n;do{ System.out.println("Press 1 to Add"); System.out.println("Press 2 to Delete"); System.out.println("press 3 to Search"); System.out.println("Press 4 to Show"); System.out.println("Press 5 to Sort"); System.out.println("Press 6 to Find the Largest element "); System.out.println("Press 7 to Find the smallest element"); System.out.println("Press 8 to Find the average"); System.out.println("Press 9 to EXIT"); t=sc.nextInt(); switch(t) { case 1:do{System.out.println("Press 1 to add element in first position");System.out.println("Press 2 to add element in last position");System.out.println("Press 3 to add element in given position");System.out.println("Press 4 to show all elements");System.out.println("Press 5 to EXIT");c=sc.nextInt();switch(c){case 1: out.addfirst(arr);break;case 2: out.addlast(arr);break;case 3: out.addgiven(arr);break;case 4: out.show(arr);break;case 5: System.out.println(" Sub menu exited");break;default: System.out.println("wrong choice");}}while(c!=5);break; case 2: do { System.out.println("Press 1 to delete the first"); System.out.println("Press 2 to delete the last"); System.out.println("Press 3 to delete the given position"); System.out.println("Press 4 to delete the given element"); System.out.println("Press 5 to EXIT"); p=sc.nextInt(); switch(p) { case 1:out.deletefirst(arr); break; case 2:out.deletelast(arr); break; case 3:out.deletegivenpos(arr); break; case 4:out.deletegivenele(arr); break; case 5:System.out.println("Sub menu EXITED"); break; default:System.out.println("Wrong choice"); } } while(p!=5); break; case 3:out.search(arr); break; case 4:out.show(arr); break; case 5:out.sort(arr); break; case 6:out.largeelement(arr); break; case 7:out.smallelement(arr); break; case 8:out.average(arr); break; case 9: System exit; break; default:System.out.println("Wrong choice"); }}while(t!=9);}} class output{Scanner ob=new Scanner(System.in);int l,k,p;int i=0;void deletegivenele(int x[]){ int no=0; System.out.println("Enter the element"); int y=ob.nextInt(); for(int w=0;w2Enter the number to be add at last->34Press 1 to add element in first positionPress 2 to add element in last positionPress 3 to add element in given positionPress 4 to show all elementsPress 5 to EXIT->423 2 4 34Press 1 to add element in first positionPress 2 to add element in last positionPress 3 to add element in given positionPress 4 to show all elementsPress 5 to EXIT->1Enter the element to be add first->66Press 1 to add element in first positionPress 2 to add element in last positionPress 3 to add element in given positionPress 4 to show all elementsPress 5 to EXIT->5 Sub menu exitedPress 1 to AddPress 2 to Deletepress 3 to SearchPress 4 to ShowPress 5 to SortPress 6 to Find the Largest element Press 7 to Find the smallest elementPress 8 to Find the averagePress 9 to EXIT->2Press 1 to delete the firstPress 2 to delete the lastPress 3 to delete the given positionPress 4 to delete the given elementPress 5 to EXIT->1Press 1 to delete the firstPress 2 to delete the lastPress 3 to delete the given positionPress 4 to delete the given elementPress 5 to EXIT->2Press 1 to delete the firstPress 2 to delete the lastPress 3 to delete the given positionPress 4 to delete the given elementPress 5 to EXIT->5Sub menu EXITEDPress 1 to AddPress 2 to Deletepress 3 to SearchPress 4 to ShowPress 5 to SortPress 6 to Find the Largest element Press 7 to Find the smallest elementPress 8 to Find the averagePress 9 to EXIT->3Enter the element->2Postion of element 2 is 1Press 1 to AddPress 2 to Deletepress 3 to SearchPress 4 to ShowPress 5 to SortPress 6 to Find the Largest element Press 7 to Find the smallest elementPress 8 to Find the averagePress 9 to EXIT->423 2 4Press 1 to AddPress 2 to Deletepress 3 to SearchPress 4 to ShowPress 5 to SortPress 6 to Find the Largest element Press 7 to Find the smallest elementPress 8 to Find the averagePress 9 to EXIT->5Press 1 to AddPress 2 to Deletepress 3 to SearchPress 4 to ShowPress 5 to SortPress 6 to Find the Largest element Press 7 to Find the smallest elementPress 8 to Find the averagePress 9 to EXIT->6Largest element is 23Press 1 to AddPress 2 to Deletepress 3 to SearchPress 4 to ShowPress 5 to SortPress 6 to Find the Largest element Press 7 to Find the smallest elementPress 8 to Find the averagePress 9 to EXIT->7Smallest element is 2Press 1 to AddPress 2 to Deletepress 3 to SearchPress 4 to ShowPress 5 to SortPress 6 to Find the Largest element Press 7 to Find the smallest elementPress 8 to Find the averagePress 9 to EXIT->8Average is 9Press 1 to AddPress 2 to Deletepress 3 to SearchPress 4 to ShowPress 5 to SortPress 6 to Find the Largest element Press 7 to Find the smallest elementPress 8 to Find the averagePress 9 to EXIT->42 4 23Press 1 to AddPress 2 to Deletepress 3 to SearchPress 4 to ShowPress 5 to SortPress 6 to Find the Largest element Press 7 to Find the smallest elementPress 8 to Find the averagePress 9 to EXIT->9

Question 4:Write a program to find the HCF of two number using recursion.Answer:import java.util.Scanner;class hcf{ public static void main(String args[]) { double result; Scanner sc=new Scanner(System.in); System.out.println("Enter the First number"); double a=sc.nextInt(); System.out.println("Enter the second number"); double b=sc.nextInt(); process ob=new process(); ob.c=a; ob.d=b; if(a0) { if((c%x==0)&&(d%x==0)) return x; else return recursion(x-1); } else return 1; }

}

OUTPUT:Enter the First number81Enter the second number36HCF of numbers 81.0 and 36.0 is 9.0Question 5: Write a program which take n no of elements which also consist duplicate elements now you have to copy all the elements in new array in which it will not contain any duplicate element & display all the elements.Answer:class arry { public static void main(String arg[]) { Scanner sc=new Scanner(System.in);System.out.println("Enter size of array:");int n=sc.nextInt(); int a[]=new int[n];int temp=0; int b[]=new int[n]; int i,c=0; System.out.print("Enter elements of array:");for(i=0;iakshayEnter the working days of Employee No. 1->15Enter the city name of Employee No. 1->indoreEnter the id of Employee No. 2->234Enter the name of Employee No. 2->rahulEnter the working days of Employee No. 2->20Enter the city name of Employee No. 2->bhopalEnter the id of Employee No. 3->345Enter the name of Employee No. 3->atulEnter the working days of Employee No. 3->10Enter the city name of Employee No. 3->indorePress 1 to enter the data of employees Press 2 to Display employees dataPress 3 to Exit->2Data of Employee No. 1Employee's id 123Employee's name akshayEmployee's working days 15Employee's salary 15000Employee's city indoreData of Employee No. 2Employee's id 234Employee's name rahulEmployee's working days 20Employee's salary 20000Employee's city bhopalData of Employee No. 3Employee's id 345Employee's name atulEmployee's working days 10Employee's salary 10000Employee's city indorePress 1 to enter the data of employees Press 2 to Display employees dataPress 3 to Exit->3

Question 7:Write a menu driven program which contains a class named student having data members roll number,name,branch and fees and member functions:a)add studentb)search by roll noc)search by a branchd)search by name

choices of menu:1)add new student2)search student by roll no3)search student by branch4)search student by name5)print all6)exitAnswer:import java.util.Scanner;class studentdata{public static void main(String args[]){int c;process ob=new process();Scanner sc=new Scanner(System.in);System.out.println("Enter the number of students");int i=sc.nextInt();int roll[]=new int[i];double fee[]=new double[i];String branch[]=new String[i];String name[]=new String[i];ob.l=i;do{System.out.println("Press 1 to ADD students");System.out.println("Press 2 to search a student by BRANCH ");System.out.println("Press 3 to search a student by NAME ");System.out.println("Press 4 to search a student by ROLL NUMBER ");System.out.println("Press 5 to print all students ");System.out.println("Press 6 to EXIT ");c=sc.nextInt();switch(c){ case 1:ob.add(roll,fee,branch,name); break; case 2:ob.branch(roll,fee,branch,name); break; case 3:ob.name(roll,fee,branch,name); break; case 4:ob.roll(roll,fee,branch,name); break; case 5:ob.print(roll,fee,branch,name); break; default :System.out.println("WRONG ENTRY");

}}while(c!=6);}}class process{ int l,g; Scanner we=new Scanner(System.in); void add(int roll[],double fee[],String branch[],String name[]) { for(int z=0;zatulEnter the branch of student # 2ecEnter the roll number of student #2->124Enter the fees of student #2->3000Enter the name of student # 3->tapanEnter the branch of student # 3->eeEnter the roll number of student #3->125Enter the fees of student #3->8000

Press 1 to ADD studentsPress 2 to search a student by BRANCH Press 3 to search a student by NAME Press 4 to search a student by ROLL NUMBER Press 5 to print all students Press 6 to EXIT ->2Enter the Branch nameeeData of the studentName of student is thakurRoll number of student is 125Branch of student is eeFees of the student is Rs.8000.0Press 1 to ADD studentsPress 2 to search a student by BRANCH Press 3 to search a student by NAME Press 4 to search a student by ROLL NUMBER Press 5 to print all students Press 6 to EXIT ->3Enter the name of the studentrahulData of the studentName of student is rahulRoll number of student is 123Branch of student is csFees of course is Rs.2000.0Press 1 to ADD studentsPress 2 to search a student by BRANCH Press 3 to search a student by NAME Press 4 to search a student by ROLL NUMBER Press 5 to print all students Press 6 to EXIT ->4Enter the roll number of student124Data of the studentName of student is atulRoll number of student is 124Branch of student is ecFees of course is Rs.3000.0Press 1 to ADD studentsPress 2 to search a student by BRANCH Press 3 to search a student by NAME Press 4 to search a student by ROLL NUMBER Press 5 to print all students Press 6 to EXIT ->5Data of the student #1Name of student is rahulRoll number of student is 123Branch of student is csFees of course is Rs.2000.0Data of the student #2Name of student is atulRoll number of student is 124Branch of student is ecFees of course is Rs.3000.0Data of the student #3Name of student is tapanRoll number of student is 125Branch of student is eeFees of course is Rs.8000.0Press 1 to ADD studentsPress 2 to search a student by BRANCH Press 3 to search a student by NAME Press 4 to search a student by ROLL NUMBER Press 5 to print all students Press 6 to EXIT ->6

Question 8: Write a class having variable hour,minute,second take two difference time from user. Write method to add them and print the smaler time.Answer:import java.util.Scanner;class time{public static void main(String []args){int hour,minute,second,h,m,s,k;Scanner sc=new Scanner(System.in);process ob=new process();System.out.println("Enter First Time:");System.out.println("Enter Hours");hour=sc.nextInt();System.out.println("Enter Minutes");minute=sc.nextInt();System.out.println("Enter Seconds");second=sc.nextInt();System.out.println("Enter Second Time:");System.out.println("Enter Hours");h=sc.nextInt();System.out.println("Enter Minutes");m=sc.nextInt();System.out.println("Enter Seconds");s=sc.nextInt();do{System.out.println("Press 1 to get the Addition of both times");System.out.println("Press 2 to get the Greater time");System.out.println("Press 3 to exit");k=sc.nextInt();switch(k){case 1:ob.add(hour,minute,second,h,m,s);break;case 2:ob.greater(hour,minute,second,h,m,s);break;case 3:System exit;break;default:System.out.println("Wrong entry");}}while(k!=3);}}class process{void add(int a ,int b,int c ,int x ,int y,int z){ int q,w;int p=(a+x)*3600+(b+y)*60+(c+z); q=p/3600;p=p-q*3600; w=p/60;p=p-w*60;if(q>23)

System.out.println(""+(q-24)+":"+w+":"+p); else{ System.out.println(""+q+":"+w+":"+p); }}

void greater(int a ,int b,int c ,int x ,int y,int z){

int m=a*3600+b*60+c;int n=x*3600+y*60+z;if(m>n){System.out.println("Time 1 is GREATER than Time 2");}else if(m12Enter Minutes->34Enter Seconds->54Enter Time 2Enter Hours->11Enter Minutes->23Enter Seconds->56Press 1 to get the Addition of both timesPress 2 to get the Greater timePress 3 to exit->123:58:50Press 1 to get the Addition of both timesPress 2 to get the Greater timePress 3 to exit->2Time 1 is GREATER than Time 2Press 1 to get the Addition of both timesPress 2 to get the Greater timePress 3 to exit->3

Question 9: Write a class named complex which take two variable & their output should be in the form of a+bi & perform the following operation. Addition & multiplication.Answer:import java.util.Scanner;class complex{public static void main(String []args){ int j;Scanner sc=new Scanner(System.in);process ob=new process();System.out.println("Enter the real part of complex number 1"); int a=sc.nextInt();System.out.println("Enter the imaginary part of complex number 1"); int b=sc.nextInt();System.out.println("Enter the real part of complex number 2"); int c=sc.nextInt();System.out.println("Enter the imaginary part of complex number 2"); int d=sc.nextInt();do{System.out.println("Press 1 to get the sum of both complex numbers");System.out.println("Press 2 to get the product of both complex numbers");System.out.println("Press 3 to EXIT"); int k=sc.nextInt();j=k;switch(k){case 1:ob.sum(a, b,c,d);break;case 2:ob.product(a,b,c,d);break;case 3: System exit;break;default:System.out.println("Wrong Entry");}}while(j!=5);}}class process{void sum(int p,int q,int r,int s){int z=p+r;int y=q+s;System.out.println("Sum of the complex numbers is");System.out.println(""+z+"+"+y+"i");}void product(int p,int q,int r,int s){int z=p*r-s*q;int y=p*s+q*r;System.out.println("Product of the complex numbers is");System.out.println(""+z+"+"+y+"i");}}OUTPUT:Enter the real part of complex number 1->2Enter the imaginary part of complex number 1->3Enter the real part of complex number 2->4Enter the imaginary part of complex number 2->5Press 1 to get the sum of both complex numbersPress 2 to get the product of both complex numbersPress 3 to EXIT->1Sum of the complex numbers is6+8iPress 1 to get the sum of both complex numbersPress 2 to get the product of both complex numbersPress 3 to EXIT->2Product of the complex numbers is-7+22iPress 1 to get the sum of both complex numbersPress 2 to get the product of both complex numbersPress 3 to EXIT->3

Question 10.write a program to set dimensions of circle,rectangle and triangle using contructors and write methods to take dimension of respective type from user. Calculate the area and display.Answer: import java.util.Scanner;class area{ public static void main(String []args) { Scanner sc=new Scanner(System.in); int k; do { System.out.println("Press 1 for the area of CIRCLE"); System.out.println("Press 2 for the area of RECTANGLE"); System.out.println("Press 3 for the area of TRIANGLE"); System.out.println("Press 4 to EXIT"); k=sc.nextInt(); switch(k) { case 1:System.out.println("Enter the Radius of the Circle"); int a=sc.nextInt(); nirvana lithium=new nirvana(a); lithium.circle(); break; case 2: System.out.println("Enter the length of Rectangle"); int b=sc.nextInt(); System.out.println("Enter the breadth of Reactangle"); int c=sc.nextInt(); nirvana sliver=new nirvana(b,c); sliver.rectangle(); break; case 3: System.out.println("Enter the side 1 of Triangle"); int d=sc.nextInt(); System.out.println("Enter the side 2 of Triangle"); int e=sc.nextInt(); System.out.println("Enter the side 3 of Triangle"); int f=sc.nextInt(); nirvana grunge=new nirvana(d,e,f); grunge.triangle(); break; default :System.out.println("Wrong Entry"); } }while(k!=4); }}class nirvana{ int radius,s1,s2,s3; int length; int breadth; nirvana(int a) { radius=a; } nirvana(int a,int b) { length=a; breadth=b; } nirvana(int a,int b,int c) { s1=a; s2=b; s3=c; } void circle() { double a=3.14*radius*radius; System.out.println("The area of CIRCLE is "+a); System.out.println(""); } void rectangle() { double z=length*breadth; System.out.println("The area of Rectangle is "+z); System.out.println(""); } void triangle() { int s=(s1+s2+s3)/2; int w=s*(s-s1)*(s-s2)*(s-s3); double t=Math.sqrt(w); System.out.println(" Area of Triangle is "+t); System.out.println(""); } }

OUTPUT:

Press 1 for the area of CIRCLEPress 2 for the area of RECTANGLEPress 3 for the area of TRIANGLEPress 4 to EXIT->1Enter the Radius of the Circle->5The area of CIRCLE is 78.5Press 1 for the area of CIRCLEPress 2 for the area of RECTANGLEPress 3 for the area of TRIANGLEPress 4 to EXIT->2Enter the length of Rectangle->5Enter the breadth of Reactangle->10The area of Rectangle is 50.0

Press 1 for the area of CIRCLEPress 2 for the area of RECTANGLEPress 3 for the area of TRIANGLEPress 4 to EXIT->3Enter the side 1 of Triangle->5Enter the side 2 of Triangle->6Enter the side 3 of Triangle->7 Area of Triangle is 14.696938

Press 1 for the area of CIRCLEPress 2 for the area of RECTANGLEPress 3 for the area of TRIANGLEPress 4 to EXIT->4

INDEX TOPIC DATE OF SUBMISSION SIGNATURE

1. PROGRAM TO TAKE THE AGE OF PERSON AND DISPLAYING ALL WHOSE AGE IS GREATER THAN AVERAGE. 03-03-2014

2. MENU DRIVEN PROGRAM FOR ADDITION OF ELEMENTS IN ARRAY AT DIFFERENT POSITIONS.03-03-2014

3. MENU DRIVEN PROGRAM FOR ADDITION, DELETION,DISPLAY OF ELEMENTS AT DIFFERENT POSITION.03-03-2014

4. PROGRAM OBJECT ARRAY FOR EMPLOYEE DETAILS.03-03-2014

5. PROGRAM FOR CREATING A NON REDUNADANT AND DUPLICATE ARRAY.03-03-2014

6. PROGRAM TO CALCULATE HCF USING RECURSION.03-03-2014

7. MENU DRIVEN PROGRAM FOR ADDING STUDENTS IN LIST.03-03-2014

8. PROGRAM FOR ADDITION OF TWO TIMES AND FIND SMALLER TIME.03-03-2014

9. PROGRAM FOR COMPLEX ADDITION AND MULTIPLICATION.03-03-2014

10. PROGRAM FOR AREA OF CIRCLE, RECTANGLE AND TRIANLGE.03-03-2014