Java Lab Assignment Solution

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 35

NAME:YEAR:CLASS R.NO.:UNIVERSITY R.NO.

:-

//1.a)prime number between range public class as1a { public static void main(String[] args) { int i,j,r; r=Integer.parseInt(args[0]); for(i=2;i<=r;i++) { for(j=2;j<=i;j++) { if(i%j==0) break; } if(i==j) System.out.print(" " +i + " } } } /*Output: java as1a 25 2 3 5 */

");

11

13

17

19

23

//1.b)check whether a string is palindrome or not import java.io.*; public class as1b { public static void main(String args[])throws IOException {

char c[]=new char[20]; char b[]=new char[20]; int a; int n; String s; boolean ispalindrome=true; System.out.println("enter the STRING"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); s=br.readLine(); c=s.toCharArray(); n=s.length(); for(int i=n-1;i>=0;i-- ) { b[n-i-1]=c[i]; } for(int i=0;i<n;i++) { if(b[i]!=c[i]) { System.out.println(" Not Palindrome"); ispalindrome=false; break; } } if(ispalindrome) System.out.println("It is Palindrome");

} } /*output: enter the STRING abba It is Palindrome*/

/*2. Write a program to arrange a set of integer numbers in a ascending order where input will

be taken through command line argument*/


import java.io.*; class ModifiedSorting { public static void main(String args[])throws IOException { int i,temp,j; int a[]=new int[10]; for(i=0;i<10;i++) { try { a[i]=Integer.parseInt(args[i]); } catch(ArrayIndexOutOfBoundsException e) { System.out.print(""); } } for(i=0;i<10;i++) { for(j=i;j<10;j++) { if(a[i]>a[j]) { temp=a[j]; a[j]=a[i]; a[i]=temp; } } } System.out.println("the required sorted array is"); for(i=0;i<10;i++) { if(a[i]!=0) System.out.println(a[i]); } } } /*java ModifiedSorting 10 1 44 52 7 the required sorted array is 1 7 10 44 52 */

/*3. Write a program to accept the following city name as argument in the command line and sort them in alphabetic order city name = Kolkata, Chennai, Mumbay, Delhi, Bangalore, Amedabad */ class Stringsort{ public static void main(String args[]){ int size=args.length; String temp=null; for(int i=0;i<size;i++) { for(int j=i+1;j<size;j++) { if (name[j].compareTo(name[i])<0) { //swaps the string temp=name[i]; name[i]=name[j]; name[j]=temp; } } } for(int i=0;i<size;i++) { System.out.println(name[i]); } } /*output: Java Stringsort Kolkata Chenna i Mumbay Delhi Bangalore Amedabad Amedabad Bangalore Chennai Delhi Kolkata Mumbay */

//4. Write a program to print the days of a month using two static array . import java.io.*; import java.util.Scanner; class fun { int months[]=new int[12]; String mname[]=new String[12]; void name(int year) { months[0]=31; if(year%100==0&&year%400==0) { months[1]=29; } else { if(year%4==0&&year%100!=0) months[1]=29; else months[1]=28; } months[2]=31; months[3]=30; months[4]=31; months[5]=30; months[6]=31; months[7]=31; months[8]=30; months[9]=31; months[10]=30; months[11]=31; mname[0]="January"; mname[1]="February"; mname[2]="March"; mname[3]="April"; mname[4]="May"; mname[5]="June"; mname[6]="July"; mname[7]="August"; mname[8]="September"; mname[9]="October"; mname[10]="November";

mname[11]="December"; for(int i=0;i<12;i++) { System.out.println("\n"+mname[i]+" : "+months[i] ); } } } class calender { public static void main(String args[]) { int n; System.out.println("ENTER THE YEAR::::::::"); Scanner in=new Scanner(System.in); n=in.nextInt(); fun ob=new fun(); ob.name(n); } } /* ENTER THE YEAR:::::::: 1990 January : 31 February : 28 March : 31 April : 30 May : 31 June : 30 July : 31 August : 31 September : 30 October : 31 November : 30 December : 31 */

/*5. a)Write a program on 2d-array to print the following pattern 1 10 101 1 0 1 0. */ import java.util.Scanner; class fun { void pyr(int n) { for(int i=1;i<=n;i++) { for(int j=1;j<=i;j++) { System.out.print(j%2+" "); } System.out.println(); } } } class as5 { public static void main(String args[]) { Scanner in=new Scanner(System.in); System.out.println("Please Enter the no. of rows......"); int n=in.nextInt(); fun ob=new fun(); ob.pyr(n); } } /*Output: Please Enter the no. of rows...... 5 1 10 101 1010 10101 */

//Q.5 b) Write a java program to print

* ** *** :::::::::::: .upto n. where n will be User Input, taken from command line argument.
import java.io.*; class fun { void pyr(int n) { for(int i=1;i<=n;i++) { for(int j=1;j<=i;j++) { System.out.print("*"+" "); } System.out.println(); } } } class pyramidstars { public static void main(String args[]) { int n=Integer.parseInt(args[0]); fun ob=new fun(); ob.pyr(n); } } /* OUTPUT: java pyramidstars 5 * * * * * * * * * * * * * * * */

/*6. Write a program to compute perimeter of class Circle, Rectangle, Square using

pameterised constructor using command line argument*/


import java.io.*; class compute{ compute(int a,int b,int c,int d) { int p=a+b+c+d; System.out.println("the perimeter of quadrilateral is"+p); //quadrilateral } compute(int a,int b) { int p=2*(a+b); //rectangle System.out.println("the perimeter of rectangle is"+p); } compute(int a) { int p=4*a; System.out.println("the perimeter of square is"+p); //square } compute(float r) { double p=2*3.14*r; System.out.println("the perimeter of circle is"+p); //circle } } class peri{ public static void main(String args[]) { int r1=Integer.parseInt(args[0]); compute ob2=new compute(r1); float r2=Float.parseFloat(args[0]); compute ob1=new compute(r2); int r3=Integer.parseInt(args[0]); int r4=Integer.parseInt(args[1]); compute ob3=new compute(r3,r4); int r5=Integer.parseInt(args[0]); int r6=Integer.parseInt(args[1]); int r7=Integer.parseInt(args[2]); int r8=Integer.parseInt(args[3]); compute ob4=new compute(r5,r6,r7,r8); } } /* OUTPUT: java peri 12 4 55 6 the perimeter of square is48 the perimeter of circle is75.36 the perimeter of rectangle is32

the perimeter of quadrilateral is77 */ /*7. Write a program to calculate the area of a Rectangle , a Circle & a Square using pameterised constructor using BufferedReader class object. */ import java.io.*; class area{ double a,b,rar,car,sar; area(double x,double y){ a=x; b=y; } void rectAr(){ rar=a*b; System.out.println("Rectangle area: "+rar); } void circleAr(){ car=3.14*a*a; System.out.println("Circle area: "+car); } void squareAr(){ sar=a*a; System.out.println("Square area: "+sar); } } public class as7 { public static void main(String arg[]){ String s1,s2; double d1,d2; try{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Give the Dimension"); s1=br.readLine(); s2=br.readLine(); d1=Double.parseDouble(s1); d2=Double.parseDouble(s2); area ob=new area(d1,d2); ob.rectAr(); ob.circleAr(); ob.squareAr(); }

catch(Exception e){ System.out.println(e); } } } /*Output: Give the Dimension 10 20 Rectangle area: 200.0 Circle area: 314.0 Square area: 100.0 */

/*8. a)Write a program to design a class Volume and then find out the volume of a Cube, Cylinder and Sphere using method overloading using BufferedReader class object*/ import java.io.*; class volume{ double a,b,vols,volc,volcy; long r; volume(double x,double y){ a=x; b=y; volcy=2*3.14*a*b; System.out.println("Volume of cylinder is :"+volcy); } volume(double x){ a=x; volc=a*a*a; System.out.println("Volume of cube is :"+volc); } volume(long x){ r=x; vols=3.14*r*r*r; System.out.println("Volume of sphere is :"+vols); } } public class as8a { public static void main(String arg[]){ String s1,s2; double d1,d2; long l1; try{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Give the Dimension 4 cube:\n"); s1=br.readLine(); d1=Double.parseDouble(s1); volume cb=new volume(d1); System.out.println("Give the Dimension 4 sphere:\n"); s1=br.readLine(); l1=Long.parseLong(s1); volume sp=new volume(l1);

System.out.println("Give the Dimension 4 Cylinder:\n"); s1=br.readLine(); d1=Double.parseDouble(s1); s2=br.readLine(); d2=Double.parseDouble(s2); volume cy=new volume(d1,d2); } catch(Exception e){ System.out.println(e); } } } /*Output: Give the Dimension 4 cube: 10 Volume of cube is :1000.0 Give the Dimension 4 sphere:10 Volume of sphere is :3140.0 Give the Dimension 4 Cylinder:10 10 Volume of cylinder is :628.0 */

/*8.b)Write a program to design a class Volume and then find out the volume of a Cube, Cylinder and Sphere using method overloading using command line argument. */ import java.io.*; class volume{ double a,b,vols,volc,volcy; long r; volume(double x,double y){ a=x; b=y; volcy=2*3.14*a*b; System.out.println("Volume of cylinder is :"+volcy); } volume(double x){ a=x; volc=a*a*a; System.out.println("Volume of cube is :"+volc); } volume(long x){ r=x; vols=3.14*r*r*r; System.out.println("Volume of sphere is :"+vols); } } public class as8b { public static void main(String arg[]){ double d1,d2; d1=Double.parseDouble(arg[0]); volume cb=new volume(d1); d1=Double.parseDouble(arg[0]); volume sp=new volume(d1); d1=Double.parseDouble(arg[0]); d2=Double.parseDouble(arg[1]); volume cy=new volume(d1,d2); } } /*output: java as8b 10 10 Volume of cube is :1000.0 "Volume of sphere is :3140.0 Volume of cylinder is :628.0 */

/*9. Create a complex class with data members as real and imaginary. Overload three constructors to initialize the data members (i.e. default, normal, and through object initialization). Provide methods which returns object of the complex class as the result for addition, subtraction, multiplication of two complex numbers.*/ import java.io.*; class complex{ int a,b,c,d; int rr,ri; complex() { a=7; b=8; c=5; d=6; } complex(int x,int y,int w,int z) { a=x; b=y; c=w; d=z; } complex(complex ob){ a=ob.a+10; b=ob.b+10; c=ob.c+10; d=ob.d+10; } complex add() { rr=a+c; ri=b+d; return this; } complex sub() { rr=a-c; ri=b-d; return this; } complex mult(){ rr=(a*c-b*d); ri=(a*d+b*c); return this; }

void show() { System.out.println(""+rr+" \t "+ri+"i"); } } public class as9{ public static void main(String arg[]){ int a,b,c,d; try{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Using deafault constructor"); complex a1=new complex(); complex b1=a1.add(); b1.show(); b1=a1.sub(); b1.show(); b1=a1.mult(); b1.show(); System.out.println("\n\nUsing parameterize Constructor "); System.out.println("Give real part of the no :- "); a=Integer.parseInt(br.readLine()); System.out.println("Give img part of the no :- "); b=Integer.parseInt(br.readLine()); System.out.println("Give real part of the 1st no :- "); c=Integer.parseInt(br.readLine()); System.out.println("Give img part of the 2nd no :- "); d=Integer.parseInt(br.readLine()); complex a2=new complex(a,b,c,d); complex b2=a2.add(); b2.show(); b2=a2.sub(); b2.show(); b2=a2.mult(); b2.show(); System.out.println("\n\nUsing passing an object "); complex a3=new complex(a2); complex b3=a3.add(); b3.show(); b3=a3.sub(); b3.show(); b3=a3.mult(); b3.show(); }

catch(Exception e){ System.out.println(e); } } } /* output: Using deafault constructor 12 14i 2 2i -13 82i Using parameterize Constructor Give real part of the no :12 Give img part of the no :56 Give real part of the 1st no :23 Give img part of the 2nd no :67 35 123i -11 -11i -3476 2092i Using passing an object 55 143i -11 -11i -4356 3872i */

/*10. Write a java class which consists of 5 integer data. Overload constructor ( Default & normal) to initialize those integer data members. Provide a method which sorts those integer data members using bubble sort.*/ class num{ int a1,a2,a3,a,a4,a5; num(){ } num(int b1,int b2,int b3,int b4,int b5){ a1=b1; a2=b2; a3=b3; a4=b4; a5=b5; } void sort(){ int[] bub=new int[5]; int temp; a1=bub[0]; a2=bub[1]; a3=bub[2]; a4=bub[3]; a5=bub[4]; for(int i=0;i<5;i++) { if(bub[i]>bub[i+1]){ temp=bub[i]; bub[i]=bub[i+1]; bub[i+1]=temp; } } for(int j=0;j<5;j++) { System.out.println(bub[j]); }

} } public class as10 {

public static void main(String arg[]){ num ob1=new num(); num ob2=new num(9,3,7,8,2); ob2.sort(); } } /* * Output: * 2 3 7 8 9

* */

/*11. Write a program to maintain the office database using single inheritance. Superclass is Employee that contain the information as follows- Emp_code, Emp_name, Address, Ph_no, Da-10%, Hra-20%. Create three subclass of Manager, Typist, officer each class having their own basic pay & da,hra remain same. */ import java.io.*; class Employee{ double basic,da,hra; String Emp_name,Emp_code,Address,Ph_no; void entry() { try{ String s; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Give the employee name: "); Emp_name=br.readLine(); System.out.println("Give the employee code: "); Emp_code=br.readLine(); System.out.println("Give the employee address: "); Address=br.readLine(); System.out.println("Give the employee ph no: "); Ph_no=br.readLine(); } catch(Exception e) { System.out.println(e); } } void show(){ System.out.println("Employee name :"+Emp_name); System.out.println("Employee code :"+Emp_code); System.out.println("Employee address :"+Address); System.out.println("Employee ph no :"+Ph_no); System.out.println("Enployee basic :"+basic); System.out.println("Employee name :"+da); System.out.println("Employee hra: "+hra); } } class Manager extends Employee{ void entM() {

try{ entry(); String s; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Give the manager basic: "); s=br.readLine(); basic=Double.parseDouble(s); da=basic*0.1; hra=basic*0.2; } catch(Exception e) { System.out.println(e); } } } class Typist extends Employee{ void entT() { try{ entry(); String s; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Give the Typist basic: "); s=br.readLine(); basic=Double.parseDouble(s); da=basic*0.1; hra=basic*0.2; } catch(Exception e) { System.out.println(e); } } } class Officer extends Employee{ void entO() { try{

entry(); String s; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Give the officer basic: "); s=br.readLine(); basic=Double.parseDouble(s); da=basic*0.1; hra=basic*0.2; } catch(Exception e) { System.out.println(e); } } } public class as11 { public static void main(String aarg[]) { try { int i,num=0; String s; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Give the no. of manager:\n"); s=br.readLine(); num=Integer.parseInt(s); Manager[] man=new Manager[num]; for(i=0;i<num;i++) { man[i]=new Manager(); man[i].entM(); } System.out.println("Give the no. of Typist :"); s=br.readLine(); num=Integer.parseInt(s); Typist[] typ=new Typist[num]; for(i=0;i<num;i++) { typ[i]=new Typist(); typ[i].entT(); }

System.out.println("Give the no. of Officer:\n"); s=br.readLine(); num=Integer.parseInt(s); Officer[] off=new Officer[num]; for(i=0;i<num;i++) { off[i]=new Officer(); off[i].entO(); } System.out.println("Manager details\n"); for(i=0;i<num;i++) { man[i].show(); } System.out.println("Typist Details\n"); for(i=0;i<num;i++) { typ[i].show(); } System.out.println("officer Details\n"); for(i=0;i<num;i++) { off[i].show(); }

} catch(Exception e){ } } } /* Output: Give the no. of manager: 1 Give the employee name: sam Give the employee code:

sa12 Give the employee address: kolkata Give the employee ph no: 234567 Give the manager basic: 56000 Give the no. of Typist : 1 Give the employee name: eric Give the employee code: er45 Give the employee address: garia Give the employee ph no: 234587 Give the Typist basic: 45000 Give the no. of Officer: 1 Give the employee name: bnd Give the employee code: b43 Give the employee address: new york Give the employee ph no: 9883377134 Give the officer basic: 70000 Manager details Employee name :sam Employee code :sa12 Employee address :kolkata Employee ph no :234567 Enployee basic :56000.0 Employee name :5600.0 Employee hra: 11200.0 Typist Details

Employee name :eric Employee code :er45 Employee address :garia Employee ph no :234587 Enployee basic :45000.0 Employee name :4500.0 Employee hra: 9000.0 officer Details Employee name :bnd Employee code :b43 Employee address :new york Employee ph no :9883377134 Enployee basic :70000.0 Employee name :7000.0 Employee hra: 14000.0 */

/*12. Create a package called Shape. Inside this package define a class named as Figure, which computes the volume of a cube, cylinder and rectangular box using method overloading. Access this class and methods from another file.*/ //Package package shape; import java.io.*; public class figure{ double vol; int i,j,k; public void volume(int a) { i=a; vol=i*i*i; System.out.println("volume of cube is "+vol); } public void volume(int a,int b) { i=a;j=b; vol=3.14*i*i*j; System.out.println("volume of cylinder is "+vol); } public void volume(int a,int b,int c) { i=a;j=b;k=c; vol=i*j*k; System.out.println("volume of rectangular box is "+vol); } } //******************************************************************** import shape.*; import java.io.*; class packpack{ public static void main(String args[]) { figure ob=new figure(); ob.volume(10); ob.volume(10,10); ob.volume(10,10,10); } } /*output: volume of cube is 1000.0 volume of cylinder is 3140.0 volume of rectangular box is 1000.0 */

/*13. Write a program using an interface called Volume. Assume that there is a part in a machine having three dimension s1,s2,s3 and Involume=1/3*pi*s1*s2*s3 Outvolume=4/3* pi*s1*s2*s3 */ import java.io.*; interface volume{ public void input(); public void show(); } class cal implements volume { double s1,s2,s3,invol,outvol; String s; public void input() { try{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Give the dimensiion: "); s=br.readLine(); s1=Double.parseDouble(s); s=br.readLine(); s2=Double.parseDouble(s); s=br.readLine(); s3=Double.parseDouble(s); invol=(3.14*s1*s2*s3)/3; outvol=(4*3.14*s1*s2*s3)/3; } catch(Exception e) { System.out.print(e); } } public void show() { System.out.println("Involume : "+invol+" Outvolume :"+outvol); } }

public class as13{ public static void main(String arg[]){ cal ob=new cal(); ob.input(); ob.show(); } } /* Output: Give the dimensiion: 12 45 12 Involume : 6782.399999999999 Outvolume :27129.599999999995 */

/*14. Define an Exception NoMatchFoundException that is thrown when Kolkata is not found from the following set of strings. city name ={ Kolkata, Chennai, Mumbai, Delhi, Bangalore, Amedabad} */ class NoMatchFoundException extends Exception{ NoMatchFoundException() { System.out.println("/nThis string does not have 'Kolkata' "); } } public class as14 { public static void main(String arg[]) { try{ int flag=0; for(int i=0;i<arg.length;i++) { if(arg[i].equals("kolkata")){ flag=0; } else flag=-1; } if(flag==0){ System.out.println("Found "); } else throw new NoMatchFoundException(); } catch(Exception e) { System.out.println(e); } } } /*

Output: * java as14 delhi mumbai ahmedabad * Exception: NoMatchException * This String does not have 'kolkata' */ /*15. Write a program to handle divide by zero exception with the following message in catch block Exception occurred and set the quotient is -1 and print the result in finally block . */ public class as15{ static int a,b,q=-1; public static void main(String arg[]) { try{ a=Integer.parseInt(arg[0]); b=Integer.parseInt(arg[1]); q=a/b; } catch(Exception e){ System.out.print(e); } finally{ if(q==-1){ System.out.println("Exception occured"); } else System.out.println(q); } } } /* output: * java as15 8 0 * java.lang.ArithmeticException: / by zeroException occured */

/*16. Write a program to declare & instantiate an 2D-array to hold marks obtained by students in different subjects in a class. Assume that there are up to 10 students in a class & there are 5 subjects.Find out the best student according to average marks of all subjects and display all the marks of him/her. */ package assignment; import java.io.*; class student{ double s1,s2,s3,s4,s5,avg; String name,s; void add() { try{ System.out.print("Give the name :"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); name=br.readLine(); System.out.print("Give the Marks :"); s=br.readLine(); s1=Double.parseDouble(s); s=br.readLine(); s2=Double.parseDouble(s); s=br.readLine(); s3=Double.parseDouble(s); s=br.readLine(); s4=Double.parseDouble(s); s=br.readLine(); s5=Double.parseDouble(s); avg=(s1+s2+s3+s4+s5)/5; } catch(Exception e) { System.out.println(e); } } void show() {

System.out.println(name); System.out.println(s1); System.out.println(s2); System.out.println(s3); System.out.println(s4); System.out.println(s5); } } public class as16 { public static void main(String[] args) { try{ student[] ob=new student[10]; int i,j=0; for(i=0;i<10;i++) { ob[i]=new student(); ob[i].add(); } for(i=0;i<9;i++) { if(ob[i].avg>ob[i+1].avg){ j=i; } else j=i+1; } System.out.println("The best student is\n"); ob[j].show(); } catch(Exception e) { System.out.println(e); } } } /* * Give the name :raj

Give the Marks :34 34 34 34 45 Give the name :rani Give the Marks :67 78 78 56 67 Give the name :ytr Give the Marks :34 09 98 87 76 Give the name :sam Give the Marks :12 45 67 90 78 Give the name :ritu Give the Marks :45 67 89 78 89 Give the name :oram Give the Marks :56 67 67 78 45 Give the name :vivek Give the Marks :56 67 67 67 67 Give the name :waq

Give the Marks :78 78 78 78 78 Give the name :ila Give the Marks :23 45 98 95 85 Give the name :fiat Give the Marks :56 78 89 56 45 The best student is ila 23.0 45.0 98.0 95.0 85.0 */

You might also like