0% found this document useful (0 votes)
71 views36 pages

Experiment No.-1: Write A Program To Find The Factorial of A Number Using Command Line Argument

The document contains descriptions of 15 programming experiments involving Java concepts like exception handling, packages, interfaces, classes, arrays, and more. Experiment 1 involves calculating a factorial using command line arguments. Experiment 2 finds the largest of three numbers the same way. Experiment 3 prints a pyramid pattern. Later experiments implement stacks and queues using arrays and interfaces, handle exceptions with and without try/catch, increment employee salaries based on designation, and simulate bank account functions. Each experiment prints the student name and enrollment number at the end.

Uploaded by

Siddharth Nair
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views36 pages

Experiment No.-1: Write A Program To Find The Factorial of A Number Using Command Line Argument

The document contains descriptions of 15 programming experiments involving Java concepts like exception handling, packages, interfaces, classes, arrays, and more. Experiment 1 involves calculating a factorial using command line arguments. Experiment 2 finds the largest of three numbers the same way. Experiment 3 prints a pyramid pattern. Later experiments implement stacks and queues using arrays and interfaces, handle exceptions with and without try/catch, increment employee salaries based on designation, and simulate bank account functions. Each experiment prints the student name and enrollment number at the end.

Uploaded by

Siddharth Nair
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

EXPERIMENT NO.

-1
Write a program to find the Factorial of a number using Command line argument. class factorial { public static void main(String arg[]) { int n, factorial,i: factorial=1; n=Integer.parseInt(arg[0]); for(i=1;i<=n;i++) factorial=factorial*i; System.out.println(FACTORIAL:+factorial); System.out.println(NAME: Siddharth Nair); System.out.println(Enroll No.: A2305309017); }

EXPERIMENT NO.-2
Write a program to find the Largest of the three numbers using Command line argument. class largest { public static void main(String arg[]) { int a = Integer.prseInt(arg[0]); int b= Integer.parseInt(arg[1]); int c= Integer.parseInt(arg[2]); if(a>b && a>c) { System.out.println(a is greater); } else if(b>a && b>c) { System.out.println(b is greater); } else { System.out.println(c is greater); } System.out.println(NAME: Siddharth Nair);

System.out.println(Enroll No.: A2305309017); } }

EXPERIMENT NO.-3
Write a program to print the Pyramid as given. class pyramid { public static void main(String arg[]) { int n=6; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { System.out.print(); } for(int j=1;j<=i;j++) { System.out.println(j); } for(int j=i-1;j>=1;j--) { System.out.println(j); } System.out.println(); }

System.out.println(NAME: Siddharth Nair); System.out.println(Enroll No.: A2305309017); } }

EXPERIMENT NO.-4
Write a program to find the Smallest of three numbers using BufferedReader IO operation. import java.io.*; class smallest { public static void main(String arg[]) throws IOException { int a,b,c; BufferedReader br=new BufferedReader(System.in)); System.out.println(Enter the value of a=); a=Integer.parseInt(br.readLine()); System.out.println(Enter the value of b=); b=Integer.parseInt(br.readLine()); System.out.println(Enter the value of c+); c=integer.parseInt(br.readLina()); if(a<b && a<c) { System.out.println(a is smallest); } else if(b<a && b<c) { System.out.println(b is smallest);

} else { System.out.println(c is smallest); } System.out.println(NAME: Siddharth Nair); System.out.println(Enroll No.: A2305309017); } }

EXPERIMENT NO.-5
Write a program to show use of copy constructor and the memory allocation of reference object. class rect { int l,b;r rect(); { l=b=0; System.out.println(Case of No Atgument); System.out.println(l=+l); System.out.println(b=+b); } rect(int x) { l=b=x; System.out.pritnln(Case of One Argument); System.out.println(l=+l); System.out.pritnln(b=+b); } rect(int x,int y) { l=x; b=y;

System.out.println(Case of Two Argument); System.out.println(l=+l); System.out.println(b=+b); } rect(rect r) { l=r.l; b=r.b; System.out.println(Case of Copy Constructor); System.out.println(l=+l); System.out.println(b=+b); } } class constructor { public static void main(String arg[]) { rect r1=new rect(); rect r2=new rect(5); rect r3=new rect(7,9); rect r4=new rect(r2); System.out.println(NAME: Siddharth Nair); System.out.println(Enroll No.: A2305309017);

} }

EXPERIMENT NO.-6
Write a program to convert number into words. import java.io.*; class alphabet { public static void main(String arg[]) { int n; String arg1[]= ,,one,two,three,four,five,six,seven,eight,nine-; String arg2[]= ,,eleven,tweleve,thirteen,fourteen,fifteen,sixteen,seventeen, eighteen,nineteen-; String arg3[]= ,,ten,twenty,thirty,fourty,fifty,sixty,seventy,eighty,ninty-; String arg4*+= ,,hundred-; System.out.println(Enter the number:); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); n=Integer.parseInt(br.readLine()); if(n<0 || n>999) System.out.println(WRONG INPUT); else { if(n>99 && n<1000) {

int x=n; int d=x/100; int r=x%100; if(r>10 && r<20) { int m=r; m=r%10; System.out.println(arg1*d+++arg4*1+++arg2*m+); } else { int k=r%10; int t=r%10; System.out.println(arg1*d+++arg4*1+++arg3*k+++arg1*t+); } } if(n>19 && n<100) { int x=n; int d=x/10; int r=x%10; System.out.pritnln(arg3*d+++arg1*r+); }

if(n>10 && n<20) { int x=n; int d=n%10; System.out.println(arg[d]); } if(n>0 && n<11) { int x=n; System.out.println(arg1[n]); } if(n==0) { System.out.println(zero); } } System.out.println(NAME: Siddharth Nair); System.out.println(Enroll no.: A2305309017); } }

EXPERIMENT NO.-7
Write a program to implement array stack with the help of Abstract class. abstract class stack1 { int a[]; int top; final int m=5; abstract void push(int x); abstract int pop(); } class stack extends stack1 { stack() { a=new int[m]; top=-1; } void push(int x) { if(top==m-1) System.out.println(OVERFLOW); else a[++top+=x;

} int pop() { int n=0; if(top==-1) System.out.println(UNDERFLOW); else n=a[top--]; return(n); } void display() { for(int i=0;i<m;i++) System.out.println(a[i]) } public static void main(String arg[]) { stack s1=new stack(); s1.push(1); s1.push(2); s1.push(3); s1.push(4); s1.push(5);

s1.push(6); System.out.println(s1.pop()); System.out.println(s1.pop()); System.out.println(s1.pop()); System.out.println(s1.pop()); System.out.println(s1.pop()); System.out.println(s1.pop()); System.out.println(NAME: Siddharth Nair); System.out.println(Enroll No.: A2305309017); } }

EXPERIMENT NO.-8
Write a program to implement array queue with the help of Interface. interface queue1 { final int m=5; void insert(int x); int delete(); } class queue implements queue1 { int a[]; int f,r; queue(); { a=new int[m]; f=-1; r=0; } public void insert(int x) { if(f==m-1) System.out.println(OVERFLOW); else

a[++f]=x; } public int delete() { int t=0; if(f==-1) System.out.println(UNDERFLOW); else { r++; t=a[r]; if(r>f); { f=-1; r=0; } } return(t); } public static void main(String arg[]) { queue q1=new queue(); q1.insert(6);

q1.insert(5); q1.insert(4); q1.insert(3); q1.insert(2); q1.insert(1); System.out.println(q1.delete()); System.out.println(NAME: Siddharth Nair); System.out.println(Enroll no.: A2305309017); } }

EXPERIMENT NO.-9
Write a program to increment the salary on the basis of Designation: MANAGER= 5000; GENERAL MANAGER= 10000; CEO=20,000; WORKER=2000 class emp1 { String desg; int sal,emp_id; public emp1(int id, String des, int sa) { emp_id; desg=des; sal=sa; } public int increment(int sal) { if(desg.equals(MANAGER)) { sal=sal+5000; System.out.println(INCREASED SALARY OF MANAGER); } else if(desg.equals(GENERAL MANAGER)) {

sal=sal+10000; System.out.pritnln(INCREASED SALARY OF GENERAL MANAGER); } else if(desg.equals(CEO)) { sal=sal+2000; } return sal; } public void disp() { System.out.println(SALARY=+sal); System.out.println(EMPLOYYES ID=+emp_id); System.out.println(DESIGNATION=+desg); } } class emp { public static void main(String arg[]) { emp1 e=new emp1(151,GENERAL MANAGER,5245); e.disp();

e.increment(3000); System.out.println(NAME: Siddharth Nair); System.out.println(Enroll No.: A2305309017); } }

EXPERIMENT NO.-10
Create a bank class containing data members: cust_name, acc_type, acc_no, bal_amt. Define member functions to assign initial values, deposit the amount, withdraw the amount. After checking, the balance left is greater than 1,000. class account { String cust_name; String acc_type; double bal_amt; int acc_no; account(String s, String s1, double a, int b) { cust_name=s; acc_type=s1; bal_amt=a; acc_no=b; } void deposit(double a,int b) { if(acc_no==b) bal_amt+=a; } void withdraw(double a)

{ if(bal_amt>1000) bal_amt-=a; else System.out.println(Sorry Process Rejected by Server); } void display() { System.out.println(CUSTOMERS NAME: +cust_name); System.out.println(ACCOUNT TYPE: +acc-type); System.out.println(NET BALANCE: +bal_amt); } } class bank { public static void main(String arg[]) { account t=new account(Siddharth,saving,4545747.0,548); t.display(); t.deposit(1000,548); t.withdraw(5412.0); System.out.println(\n\n Customer Information); t.display();

System.out.println(NAME: Siddharth Nair); System.out.println(Enroll No.: A2305309017); } }

Experiment- 11
Implementing exception without handling. class exceptionwdhandle { public static void main(String s[]) { System.out.println("before exception"); int x= 10/0; System.out.println("After exception"); } }
Output:

Experiment-12
Implementing exception with handling class prga { public static void main(String s[]) { System.out.println("Before exception"); try{ int x= 10/0; }catch(Exception e) { System.out.println(e); int x=0; } System.out.println("After exception"); } }
Output:

Experiment- 13
Program to implement a package program. package p1; class prg13 { public static void main(String s[]) { System.out.println("Package Program"); } }
Output:

Experiment 14
Implementation of queue using interface interface queue { final int m=5; void insert (int x); int delete (); } class intqueue implements queue { int a[]; int f, r; intqueue() { a=new int[m]; f=-1; } public void insert(int x) { if (f==m-1) System.out.println("Overflow"); else a[++f]=x; } public int delete() r=0;

{ int y=0; if (f==-1 && r==0) System.out.println("Underflow"); else { y=a[r]; a[r]=0; r++;

if (r>f) { f=-1; } } return (y); } void display() { System.out.println("The queue is:"); for (int i=0; i<m; i++) System.out.println(a[i]); } } class prg10 { public static void main(String s[]) { intqueue q1= new intqueue(); r=0;

q1.insert(5); q1.insert(13); q1.insert(4); q1.insert(8); q1.insert(1); q1.display(); q1.insert(2); q1.delete(); q1.display(); q1.delete(); q1.display(); System.out.println(Siddharth A2305309017); } }
Output:

Experiment-15

Program to implement exception handling using multiple catch blocks.

class prg30 { public static void main (String s[]) { System.out.println("Before exception"); try { int x[]=new int[10]; // x[11]=15; // int a=15/0; int a= Integer.parseInt("15I"); } catch (ArithmeticException e) { System.out.println(e); } catch (ArrayIndexOutOfBoundsException e) { System.out.println(e); } catch (NumberFormatException e) {

System.out.println(e); } System.out.println("Siddharth A2305309017"); } }


Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy