Вы находитесь на странице: 1из 65

Assignment 03

Question 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;i<n;i++) { System.out.println("Member "+(i+1)); age[i]=sc.nextInt(); sum=sum+age[i]; } avg=sum/n; System.out.println("peoples age greater then averege-");

for(i=0;i<n;i++) { if(age[i]>avg) {System.out.print("member "+(i+1)); System.out.println(" "+age[i]); }} } }

OUTPUT:
Enter the number of peoples ->5 Enter the age of all peoples:: Member 1 ->12 Member 2 ->12 Member 3 ->34 Member 4 -> 15 Member 5 ->34 peoples age greater then average member 3- 34 member 5- 34

Question 2: Write a menu driven program to the following operation on an array: a)add the element at the first position b)add the element at last position c)add element at given position d)show all elements e)exit Answer:
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<n) { if(q>=0&&q<n) { System.out.println("Enter the number you want to insert-"); p=sc.nextInt(); for(i=size;i>=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;i<n;i++) { System.out.print(" "+a[i]); } break; case 5: System exit; break;

default: System.out.println("WRONG CHOICE..."); System.out.println("Please try again..."); break; }

System.out.println("Do you want to perform again....then press 0"); System.out.println("If not then press anything except 0..."); loop=sc.nextInt(); } }}

OUTPUT:
*****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...

->1
Enter the number you want to insert- ->12 Element 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...

->2
Enter the number you want to insert- ->11 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...

->3
Enter the position where you want to insert- ->2 Enter the number you want to insert ->13

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...

->4
Array 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 -back b)delete -delete first element -delete second element -delete at given position -delete given element -back c)search d)sort e)find largest element in array f)smallest element of array g)average of all elements h)exit

Answer:
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;w<i;w++) { if(y==x[w]) {

break; } else { ++no; } } for(int w=no;w<i-1;w++) {

x[w]=x[w+1]; } --i; } void deletegivenpos(int x[]) { System.out.println("Enter the position"); int cq=ob.nextInt(); for(int w=cq;w<i-1;w++) { x[w]=x[w+1];

} --i; }

void deletefirst(int x[]) { for(int w=0;w<i-1;w++) { x[w]=x[w+1]; } --i;

} void deletelast(int x[]) { x[i-1]=0; --i;

} void search(int x[]) { int no=0; System.out.println("Enter the element"); int pqr=ob.nextInt(); for(int w=0;w<i;w++) { if(pqr==x[w]) { break; } else{ ++no; } } System.out.println("Postion of element "+pqr+" is "+no);

} void sort(int x[]) { int small; for(int w=0;w<i-1;w++) { for(int u=w+1;u<i;u++) { if(x[w]>x[u]) { small=x[w];

x[w]=x[u]; x[u]=small; }

} }

} void smallelement(int x[]) { System.out.println("Smallest element is "+x[0]); } void largeelement(int x[]) { System.out.println ("Largest element is "+x[i-1]); } void average(int x[]) { int sum=0; int ave; for(int w=0;w<i;w++) { sum=sum+x[w]; } ave=sum/i; System.out.println("Average is "+ave);

void addfirst(int x[]) { if(i<l) { System.out.println("Enter the element to be add first"); k= ob.nextInt(); if(x[0]==0) { x[0]=k; ++i; } else { for(int z=(i-1);z>=0;z--) { x[z+1]=x[z]; } x[0]=k; ++i; } } else { System.out.println("Overflow"); } } void addlast(int x[]) { if(i<l)

{ System.out.println("Enter the number to be add at last"); k=ob.nextInt(); x[i]=k; ++i; } else{ System.out.println("Overflow"); } } void addgiven(int x[]) { if(i<l) { System.out.println("Enter the position"); p=ob.nextInt(); System.out.println("Enter the number"); k=ob.nextInt(); for(int r=(i-1);r>=(p-1);r--) { x[r+1]=x[r]; } x[p-1]=k; ++i; } else { System.out.println("Overflow"); }

void show(int x[]) { for(int j=0;j<i;j++) { System.out.println(x[j]); } } }

OUTPUT:
Enter array size 5 Press 1 to Add Press 2 to Delete press 3 to Search Press 4 to Show Press 5 to Sort Press 6 to Find the Largest element Press 7 to Find the smallest element Press 8 to Find the average Press 9 to EXIT

->1 Press 1 to add element in first position Press 2 to add element in last position Press 3 to add element in given position Press 4 to show all elements Press 5 to EXIT ->1 Enter the element to be add first ->2 Press 1 to add element in first position Press 2 to add element in last position Press 3 to add element in given position Press 4 to show all elements Press 5 to EXIT ->2 Enter the number to be add at last ->4 Press 1 to add element in first position Press 2 to add element in last position Press 3 to add element in given position

Press 4 to show all elements Press 5 to EXIT ->3 Enter the position ->1 Enter the number ->23 Press 1 to add element in first position Press 2 to add element in last position Press 3 to add element in given position Press 4 to show all elements Press 5 to EXIT ->2 Enter the number to be add at last ->34 Press 1 to add element in first position Press 2 to add element in last position Press 3 to add element in given position Press 4 to show all elements Press 5 to EXIT

->4 23 2 4 34 Press 1 to add element in first position Press 2 to add element in last position Press 3 to add element in given position Press 4 to show all elements Press 5 to EXIT ->1 Enter the element to be add first ->66 Press 1 to add element in first position Press 2 to add element in last position Press 3 to add element in given position Press 4 to show all elements Press 5 to EXIT ->5 Sub menu exited Press 1 to Add Press 2 to Delete press 3 to Search

Press 4 to Show Press 5 to Sort Press 6 to Find the Largest element Press 7 to Find the smallest element Press 8 to Find the average Press 9 to EXIT ->2 Press 1 to delete the first Press 2 to delete the last Press 3 to delete the given position Press 4 to delete the given element Press 5 to EXIT ->1 Press 1 to delete the first Press 2 to delete the last Press 3 to delete the given position Press 4 to delete the given element Press 5 to EXIT ->2 Press 1 to delete the first

Press 2 to delete the last Press 3 to delete the given position Press 4 to delete the given element Press 5 to EXIT ->5 Sub menu EXITED Press 1 to Add Press 2 to Delete press 3 to Search Press 4 to Show Press 5 to Sort Press 6 to Find the Largest element Press 7 to Find the smallest element Press 8 to Find the average Press 9 to EXIT ->3 Enter the element ->2 Postion of element 2 is 1 Press 1 to Add

Press 2 to Delete press 3 to Search Press 4 to Show Press 5 to Sort Press 6 to Find the Largest element Press 7 to Find the smallest element Press 8 to Find the average Press 9 to EXIT ->4 23 2 4 Press 1 to Add Press 2 to Delete press 3 to Search Press 4 to Show Press 5 to Sort Press 6 to Find the Largest element Press 7 to Find the smallest element Press 8 to Find the average Press 9 to EXIT ->5

Press 1 to Add Press 2 to Delete press 3 to Search Press 4 to Show Press 5 to Sort Press 6 to Find the Largest element Press 7 to Find the smallest element Press 8 to Find the average Press 9 to EXIT ->6 Largest element is 23 Press 1 to Add Press 2 to Delete press 3 to Search Press 4 to Show Press 5 to Sort Press 6 to Find the Largest element Press 7 to Find the smallest element Press 8 to Find the average Press 9 to EXIT

->7 Smallest element is 2 Press 1 to Add Press 2 to Delete press 3 to Search Press 4 to Show Press 5 to Sort Press 6 to Find the Largest element Press 7 to Find the smallest element Press 8 to Find the average Press 9 to EXIT ->8 Average is 9 Press 1 to Add Press 2 to Delete press 3 to Search Press 4 to Show Press 5 to Sort Press 6 to Find the Largest element Press 7 to Find the smallest element

Press 8 to Find the average Press 9 to EXIT ->4 2 4 23 Press 1 to Add Press 2 to Delete press 3 to Search Press 4 to Show Press 5 to Sort Press 6 to Find the Largest element Press 7 to Find the smallest element Press 8 to Find the average Press 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(a<b) { result=ob.recursion(a); } else { result=ob.recursion(b); } System.out.println("HCF of numers "+a+" and "+b+" is "+result); } } class process

{ double c; double d; double recursion(double x) { if(x>0) { if((c%x==0)&&(d%x==0)) return x; else return recursion(x-1); } else return 1; }

OUTPUT:
Enter the First number 81 Enter the second number 36 HCF of numbers 81.0 and 36.0 is 9.0

Question 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;i<n;i++) { a[i]=sc.nextInt(); } for(i=0;i<n;i++) { for(int j=0;j<n;j++) { if(b[j]==a[i])

{ temp=1;break; } else temp=0; } if(temp==0) { b[c]=a[i]; c++; } } for(i=0;i<c;i++) System.out.println(" "+b[i]); }}

OUTPUT:
Enter size of array ->6 Enter the elements of array ->1, 2, 2, 1, 3, 4 1 2 3 4

Question 6: Write a class Employee having data members id,name ,city,basic salary,number of days. Write a method to take inputs and write a method to calculate salary on each day and display: Answer:
import java.util.Scanner; class Employee { public static void main(String args[]) { int a; Scanner sc=new Scanner (System.in); process ob=new process(); System.out.println("Enter the no. of employees"); int m=sc.nextInt(); int id[]=new int[m]; ob.l=m; String name[]=new String[m]; int sal[]=new int[m]; int days[]=new int[m]; String city[]=new String[m]; do { System.out.println("Press 1 to enter the data of employees "); System.out.println("Press 2 to Display employees data"); System.out.println ("Press 3 to Exit"); a=sc.nextInt(); switch(a) {

case 1: ob.add(id,name,sal,days,city); break; case 2: ob.display(id,name,sal,days,city); break; default :System.out.println("wrong choice"); } }while(a!=3); } }

class process { Scanner we =new Scanner(System.in); int l; void add(int id[],String name[],int sal[],int days[],String city[]) { for(int z=0;z<l;z++) { System.out.println("Enter the id of Employee No. "+(z+1)); int a=we.nextInt(); id[z]=a; System.out.println("Enter the name of Employee No. "+(z+1)); String b=we.next(); name[z]=b; System.out.println("Enter the working days of Employee No. "+(z+1)); int c=we.nextInt(); days[z]=c; System.out.println("Enter the city name of Employee No. "+(z+1)); String d=we.next();

city[z]=d; int basic=1000*c; sal[z]=basic; } } void display(int id[],String name[],int sal[],int days[],String city[]) { for(int k=0;k<l;k++) { System.out.println("Data of Employee No. "+(k+1)); System.out.println(""); System.out.println(""); System.out.println("Employee's id "+id[k]); System.out.println("Employee's name "+name[k]); System.out.println("Employee's working days "+days[k]); System.out.println("Employee's salary "+sal[k]); System.out.println("Employee's city "+city[k]); System.out.println(""); System.out.println(""); } } }

OUTPUT: Enter the no. of employees ->3 Press 1 to enter the data of employees

Press 2 to Display employees data Press 3 to Exit ->1 Enter the id of Employee No. 1 ->123 Enter the name of Employee No. 1 ->akshay Enter the working days of Employee No. 1 ->15 Enter the city name of Employee No. 1 ->indore Enter the id of Employee No. 2 ->234 Enter the name of Employee No. 2 ->rahul Enter the working days of Employee No. 2 ->20 Enter the city name of Employee No. 2 ->bhopal Enter the id of Employee No. 3

->345 Enter the name of Employee No. 3 ->atul Enter the working days of Employee No. 3 ->10 Enter the city name of Employee No. 3 ->indore Press 1 to enter the data of employees Press 2 to Display employees data Press 3 to Exit ->2 Data of Employee No. 1 Employee's id 123 Employee's name akshay Employee's working days 15 Employee's salary 15000 Employee's city indore Data of Employee No. 2 Employee's id 234 Employee's name rahul

Employee's working days 20 Employee's salary 20000 Employee's city bhopal Data of Employee No. 3 Employee's id 345 Employee's name atul Employee's working days 10 Employee's salary 10000 Employee's city indore Press 1 to enter the data of employees Press 2 to Display employees data Press 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 student b)search by roll no c)search by a branch d)search by name

choices of menu: 1)add new student 2)search student by roll no 3)search student by branch 4)search student by name 5)print all 6)exit

Answer:
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;z<l;z++) { System.out.println("Enter the name of student # "+(z+1)); String a=we.next(); name[z]=a; System.out.println("Enter the branch of student # "+(z+1)); String b=we.next(); branch[z]=b; System.out.println("Enter the roll number of student #"+(z+1)); int k=we.nextInt(); roll[z]=k; System.out.println("Enter the fees of student #"+(z+1)); double t=we.nextDouble(); fee[z]=t;

} void branch(int roll[],double fee[],String branch[],String name[]) { System.out.println("Enter the Branch name");

String st=we.next(); for(int z=0;z<l;z++) { if(branch[z].equals(st)) { g=z; System.out.println("Data of the student"); System.out.println(""); System.out.println("Name of student is "+name[g]); System.out.println("Roll number of student is "+roll[g]); System.out.println("Branch of student is "+branch[g]); System.out.println("Fees of the student is Rs."+fee[g]); System.out.println(""); System.out.println(""); } } } void name(int roll[],double fee[],String branch[],String name[]) { System.out.println("Enter the name of the student"); String st=we.next(); for(int z=0;z<l;z++) { if(name[z].equals(st)) { g=z; break; } }

System.out.println("Data of the student"); System.out.println(""); System.out.println("Name of student is "+name[g]); System.out.println("Roll number of student is "+roll[g]); System.out.println("Branch of student is "+branch[g]); System.out.println("Fees of course is Rs."+fee[g]); System.out.println(""); System.out.println("");

} void roll(int roll[],double fee[],String branch[],String name[]) { System.out.println("Enter the roll number of student"); int st=we.nextInt(); for(int z=0;z<l;z++) { if(roll[z]==st) { g=z; break; } } System.out.println("Data of the student"); System.out.println(""); System.out.println("Name of student is "+name[g]); System.out.println("Roll number of student is "+roll[g]); System.out.println("Branch of student is "+branch[g]); System.out.println("Fees of course is Rs."+fee[g]); System.out.println("");

System.out.println("");

} void print(int roll[],double fee[],String branch[],String name[]) { for(int g=0;g<l;g++) { System.out.println("Data of the student #"+(g+1)); System.out.println(""); System.out.println("Name of student is "+name[g]); System.out.println("Roll number of student is "+roll[g]); System.out.println("Branch of student is "+branch[g]); System.out.println("Fees of course is Rs."+fee[g]); System.out.println(""); System.out.println(""); } } } OUTPUT: Enter the number of students ->3 Press 1 to ADD students Press 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 ->1 Enter the name of student # 1 rahul Enter the branch of student # 1 cs Enter the roll number of student #1 ->123 Enter the fees of student #1 ->2000 Enter the name of student # 2 ->atul Enter the branch of student # 2 ec Enter the roll number of student #2 ->124 Enter the fees of student #2 ->3000 Enter the name of student # 3

->tapan Enter the branch of student # 3 ->ee Enter the roll number of student #3 ->125 Enter the fees of student #3 ->8000

Press 1 to ADD students Press 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 ->2 Enter the Branch name ee Data of the student Name of student is thakur Roll number of student is 125

Branch of student is ee Fees of the student is Rs.8000.0 Press 1 to ADD students Press 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 ->3 Enter the name of the student rahul Data of the student Name of student is rahul Roll number of student is 123 Branch of student is cs Fees of course is Rs.2000.0 Press 1 to ADD students Press 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 ->4 Enter the roll number of student 124 Data of the student Name of student is atul Roll number of student is 124 Branch of student is ec Fees of course is Rs.3000.0 Press 1 to ADD students Press 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 ->5 Data of the student #1 Name of student is rahul Roll number of student is 123

Branch of student is cs Fees of course is Rs.2000.0 Data of the student #2 Name of student is atul Roll number of student is 124 Branch of student is ec Fees of course is Rs.3000.0 Data of the student #3 Name of student is tapan Roll number of student is 125 Branch of student is ee Fees of course is Rs.8000.0 Press 1 to ADD students Press 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(m<n) { System.out.println("Time 2 is GREATER than Time 1"); } else if(m==n) { System.out.println("Time 1 is EQUAL to Time 2"); } }}

OUTPUT: Enter Time 1 Enter Hours ->12 Enter Minutes ->34 Enter Seconds ->54

Enter Time 2 Enter Hours ->11 Enter Minutes ->23 Enter Seconds ->56 Press 1 to get the Addition of both times Press 2 to get the Greater time Press 3 to exit ->1 23:58:50 Press 1 to get the Addition of both times Press 2 to get the Greater time Press 3 to exit ->2 Time 1 is GREATER than Time 2 Press 1 to get the Addition of both times Press 2 to get the Greater time Press 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 ->2 Enter the imaginary part of complex number 1 ->3 Enter the real part of complex number 2 ->4 Enter the imaginary part of complex number 2 ->5 Press 1 to get the sum of both complex numbers Press 2 to get the product of both complex numbers Press 3 to EXIT ->1 Sum of the complex numbers is 6+8i

Press 1 to get the sum of both complex numbers Press 2 to get the product of both complex numbers Press 3 to EXIT ->2 Product of the complex numbers is -7+22i Press 1 to get the sum of both complex numbers Press 2 to get the product of both complex numbers Press 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 CIRCLE Press 2 for the area of RECTANGLE Press 3 for the area of TRIANGLE Press 4 to EXIT ->1 Enter the Radius of the Circle ->5 The area of CIRCLE is 78.5 Press 1 for the area of CIRCLE Press 2 for the area of RECTANGLE Press 3 for the area of TRIANGLE Press 4 to EXIT

->2 Enter the length of Rectangle ->5 Enter the breadth of Reactangle ->10 The area of Rectangle is 50.0

Press 1 for the area of CIRCLE Press 2 for the area of RECTANGLE Press 3 for the area of TRIANGLE Press 4 to EXIT ->3 Enter the side 1 of Triangle ->5 Enter the side 2 of Triangle ->6 Enter the side 3 of Triangle ->7 Area of Triangle is 14.696938

Press 1 for the area of CIRCLE Press 2 for the area of RECTANGLE Press 3 for the area of TRIANGLE Press 4 to EXIT ->4

INDEX
DATE OF TOPIC SUBMISSION SIGNATURE

1. PROGRAM TO TAKE THE AGE OF PERSON AND DISPLAYING ALL WHOSE AGE IS GREATER THAN AVERAGE. 2. MENU DRIVEN PROGRAM FOR ADDITION OF ELEMENTS IN ARRAY AT DIFFERENT POSITIONS. 3. MENU DRIVEN PROGRAM FOR ADDITION, DELETION,DISPLAY OF ELEMENTS AT DIFFERENT POSITION. 4. PROGRAM OBJECT ARRAY FOR EMPLOYEE DETAILS. 5. PROGRAM FOR CREATING A NON REDUNADANT AND DUPLICATE ARRAY. 6. PROGRAM TO CALCULATE HCF USING RECURSION. 7. MENU DRIVEN PROGRAM FOR ADDING STUDENTS IN LIST. 8. PROGRAM FOR ADDITION OF TWO TIMES AND FIND SMALLER TIME. 9. PROGRAM FOR COMPLEX ADDITION AND MULTIPLICATION. 10. PROGRAM FOR AREA OF CIRCLE, RECTANGLE AND TRIANLGE.

03-03-2014

03-03-2014

03-03-2014

03-03-2014 03-03-2014

03-03-2014 03-03-2014

03-03-2014

03-03-2014

03-03-2014

Вам также может понравиться