java lab assignment solution 5th sem

Post on 27-Nov-2014

2.351 Views

Category:

Documents

7 Downloads

Preview:

Click to see full reader

DESCRIPTION

this is NSEC Java lab assignment solved paper

TRANSCRIPT

NAME:- YEAR:- CLASS R.NO.:-UNIVERSITY R.NO.:- //1.a)prime number between rangepublic 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 7 11 13 17 19 23 */

//1.b)check whether a string is palindrome or notimport 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 STRINGabbaIt 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 7the required sorted array is17104452*/

/*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 AmedabadAmedabadBangaloreChennaiDelhiKolkataMumbay */

//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 1 0 1 0 1 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......51 1 0 1 0 1 1 0 1 0 1 0 1 0 1 */

//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); //rectangleSystem.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 6the perimeter of square is48the perimeter of circle is75.36the perimeter of rectangle is32the 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 Dimension1020Rectangle area: 200.0Circle area: 314.0Square 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 constructor12 14i2 2i-13 82iUsing parameterize Constructor Give real part of the no :- 12Give img part of the no :- 56Give real part of the 1st no :- 23Give img part of the 2nd no :- 6735 123i-11 -11i-3476 2092iUsing 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:

1Give the employee name:samGive the employee code:sa12

Give the employee address:kolkataGive the employee ph no:234567Give the manager basic:56000Give the no. of Typist :1Give the employee name:ericGive the employee code:er45Give the employee address:gariaGive the employee ph no:234587Give the Typist basic:45000Give the no. of Officer:

1Give the employee name:bndGive the employee code:b43Give the employee address:new yorkGive the employee ph no:9883377134Give the officer basic:70000Manager details

Employee name :samEmployee code :sa12Employee address :kolkataEmployee ph no :234567Enployee basic :56000.0Employee name :5600.0Employee hra: 11200.0Typist Details

Employee name :ericEmployee code :er45Employee address :gariaEmployee ph no :234587Enployee basic :45000.0Employee name :4500.0Employee hra: 9000.0officer Details

Employee name :bndEmployee code :b43Employee address :new yorkEmployee ph no :9883377134Enployee basic :70000.0Employee name :7000.0Employee 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.0volume of cylinder is 3140.0volume 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:124512Involume : 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 45Give the name :raniGive the Marks :6778785667Give the name :ytrGive the Marks :3409988776Give the name :samGive the Marks :1245679078Give the name :rituGive the Marks :4567897889Give the name :oramGive the Marks :5667677845Give the name :vivekGive the Marks :5667676767Give the name :waq

Give the Marks :7878787878Give the name :ilaGive the Marks :2345989585Give the name :fiatGive the Marks :5678895645The best student is

ila23.045.098.095.085.0 */

top related