java file ll
TRANSCRIPT
Program No.1
WAP to display all pime numbers between 1 and 100.
PrimeNo.java
public class PrimeNo{
public static void main(String args[]){
int i,j,n;System.out.println(“Name: Jasveen Singh”);System.out.println(“Enrolment Number: A2305209219”);System.out.println("The Prime Numbers are:");for(i=1;i<=100;i++){
for(j=2;j<=i; j++) {
n=i%j; if(n==0)
{break;
} } if(i==j)
{System.out.println(i);
}}
}}
Program No.2
WAP to find the sum of three numbers using command line arguments.
Sum.java
public class Sum{
public static void main(String args[]){
int a,b,c,d,sum;d=args.length;a=Integer.parseInt(args[0]);b=Integer.parseInt(args[1]);c=Integer.parseInt(args[2]);sum=a+b+c;System.out.println("The sum of three numbers using command line arguments are "+ sum);System.out.println("The size of array is "+ d);
}}
Program No.3
WAP to display the pattern in java
11 2 1
1 2 3 2 11 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
Pattern.java
public class Pattern{
public static void main(String args[]){
int a=Integer.parseInt(args[0]);int i,j,k,m;System.out.println("The pattern is: ");for(i=1;i<=a;i++){
for(j=a-i;j>=i;j--){
System.out.println(" ");} for(k=1;k<=i;k++) {
System.out.println(k);} for(m=i-1;m>=1;m--){
System.out.println(m);} System.out.println();
} } }
Program No.4
WAP to make a constructor Student class in java
prg4.java
class Student{
int rollno;String name;Student(){
rollno=0;name=null;
}Student(int x,String y){
rollno=x;name=y;
}Student(Student t){
this.rollno=t.rollno;this.name=t.name;
}void Showdata(){
System.out.println(" The Rollno is:"+rollno+" "+ "The Name is:"+name);
}}public class prg4{
public static void main(String args[]){
Student s1=new Student();s1.Showdata();Student s2=new Student(1117,"PUNIT");s2.Showdata();Student s3=new Student(s2);s3.Showdata();
}}
Program No.5
WAP To implement stacks in java
Stack.java
class Stack{
int stk[],x;final int m=5;int top;stack(){
stk=new int[m];top=-1;
}public void push(int n){
if(top==m-1)System.out.println("Overflow");else{
top++;stk[top]=n;System.out.println("Element inserted: "+n);
}}public int pop(){
if(top==-1){
System.out.println("Underflow");return x;
}else{
x=stk[top];top--;return x;
}}public static void main(String s1[]){
stack s=new stack();s.push(1);s.push(2);s.push(3);s.push(4);s.push(5);System.out.println(s.pop());System.out.println(s.pop());System.out.println(s.pop());System.out.println(s.pop());System.out.println(s.pop());System.out.println(s.pop());
}}
Program No.6
WAP to create an Employee class.
prg6.java
class Employee{
String empname;int empid,empsal,increment;String empaddress;String Designation;Employee(String name,int id,int sal,String addr,String Desg){
empname=name;empid=id;empsal=sal;empaddress=addr;Designation=Desg;
}public void Increment(){
if(Designation.equals("Manager")){
increment=empsal+5000;}else if(Designation.equals("GM")){
increment=empsal+10000;}else if(Designation.equals("CEO")){
increment=empsal+20000;}else{
increment=empsal+2000;}
}public void display(){
System.out.println("Employee Details");System.out.println("Employee Name:"+" "+empname);System.out.println("Employee Id:"+" "+empid);System.out.println("Employee Salary:"+" "+empsal);
System.out.println("Employee Incremented Salary:"+" "+increment);System.out.println("Employee Address:"+" "+empaddress);System.out.println("Employee Designation:"+" "+Designation);
}}public class prg6{
public static void main(String args[]){
Employee e1;e1=new Employee("Rishabh",1070,5000,"Delhi","Manager");e1.Increment();e1.display();Employee e2=new Employee("{Prateek",1071,5000,"Delhi","GM");e2.Increment();e2.display();Employee e3=new Employee("Karan",1072,5000,"Delhi","CEO");e3.Increment();e3.display();Employee e4=new Employee("Nitish",1073,5000,"Delhi","Worker");e4.Increment();e4.display();
}}
Program No.7
WAP to create a Bank Account in Javaprg7.java
class Bank{
String DepositName,AcType,Address;int Balance;Bank(String Name,String Ac,String addr,int Bal){
DepositName=Name;AcType=Ac;Address=addr;Balance=Bal;
}public void Deposit(int a){
Balance=Balance+a;System.out.println("The Balance after deposit:"+Balance);
}public void Withdrawal(int b){ if(Balance<=1000 ||Balance==0)
{System.out.println("Amount can't be withdrawn");Balance=-50;System.out.println("A fine of 50 is imposed"+Balance);
}else{
Balance=Balance-b;System.out.println("Amount after withdrawal:"+Balance);
}}public void Display(){
System.out.println("Bank Account Details");System.out.println("Depositor Name:"+" "+DepositName);System.out.println("Account Type:"+" "+AcType);System.out.println("Depositor Balance:"+" "+Balance);System.out.println("Depositor Address:"+" "+Address);
}}public class prg7{
public static void main(String args[]){
Bank B1=new Bank("Rishabh","Savings","Delhi",5000);
B1.Deposit(2000);B1.Withdrawal(1000);B1.Display();
}}
Program No.8
WAP to create a package and execute a Java program.
prg8.java
package p1;public class prg8{
public static void main(String args[]){
System.out.println("Java package example");}
}
Program No.9
WAP to create a Result class for calculation of result of student and to display the final result of student.
Result.java
package p2;class Student{
int rollno;String name;Student(int r,String n){
rollno=r;name=n;
}void ShowData(){
System.out.println("The name of the student is: "+name);System.out.println("The roll number is: "+ rollno);
}}class Test extends Student{
int m1,m2;Test(int r,String n,int n1, int n2){
super(r,n);m1=n1;m2=n2;
}void ShowMarks(){
System.out.println("The marks in subject 1: "+m1+" ");System.out.println("The marks in subject 2: "+m2);
}}interface Sports{
final int sw=5;}public class Result extends Test implements Sports{
int res;
Result(int r,String n,int n1,int n2){
super(r,n,n1,n2);}void calculate(){
res=(m1+m2+sw)/3;System.out.println("The Result is: "+res);
}public static void main(String args[]){
Result r1=new Result(1117,"PUNIT",70,80);r1.ShowData();r1.ShowMarks();r1.calculate();
}}
Program No.10
WAP to create an abstract class and use it in calculating the area of a rectangle, square and circle.
prg10.java
abstract class Shape{
int x;abstract void area();
}class Square extends Shape{
int res;Square(int a){
x=a;}void area(){
res=x*x;System.out.println("The area of square is: "+res);
}}class Circle extends Shape{
double r2;Circle(int b){
x=b; }void area(){
r2=3.14*x*x;System.out.println("The area of circle is: "+r2);
}}class Rectangle extends Shape{
int r3,f;Rectangle(int c,int d){
x=c;f=d;
}void area(){
r3=x*f;System.out.println("The area of rectangle is: "+r3);
}}public class prg10{
public static void main(String args[]){
Square s1=new Square(5);s1.area();Circle c1=new Circle(10);c1.area();Rectangle r1=new Rectangle(10,20);r1.area();
}}
Program No.11
WAP to generate and show an exception in Java program
prg11.java
class prg11{
public static void main(String args[]){
System.out.println("Before Exception");int x=10/0;System.out.println("After Exception");
}}
Program No.12
WAP to generate an exception and implement exception handling block for handling the exception.
prg12.java
class prg12{
public static void main(String args[]){
int x;System.out.println("Before Exception");try{
x=10/0;System.out.println(x);
}catch(Exception e){
x=0;System.out.println(x);
}System.out.println("After Exception");
}}
Program No.13
Wap to create your own Exception and illustrate the use of throw keyword.
prg13.java
class MyException extends Exception{
int no;MyException(int n){
no=n;} public String toString(){
String str=null; if(no==1)
str="Number can't be negative.";if(no==2)
str="Number is out of bound";return(str);
}}class Error{
int rollno;Error(int r){
rollno=r;}
void display() {
try{
if(rollno<0) throw new MyException(1);
else if(rollno>999) throw new MyException(2);
elseSystem.out.println("The roll number is: "+rollno);
}catch(Exception e){
System.out.println();}
}}public class prg13{
public static void main(String args[]){
Error p1=new Error(117);p1.display();
}}
Program No.14
WAP to illustrate the working of ‘throws’ keyword in Exception Handling
prg14.java
class prg14{
void display() throws InterruptedException{
throw new InterruptedException("New Exception");} public static void main(String args[]){
prg14 p1=new prg14();try{
p1.display();}catch(Exception e1){
System.out.println(e1);}
}}
Program No.15
WAP to implement a thread.
prg15.java
class prg15{
public static void main(String s[]){
Thread t=Thread.currentThread();System.out.println(t);try{
for(int i=1;i<=10;i++){
System.out.println("I="+i);Thread.sleep(200);
}}catch(Exception e){}t.setName("MY THREAD");t.setPriority(10);System.out.println(t);
}}
Program No.16
WAP to implement a thread using Runnable
prg16.java
class MyThread3 implements Runnable{
Thread t;MyThread3(){
t=new Thread(this); t.start();
}public void run(){
try{
for(int i=0;i<=9;i++){
System.out.println(i);Thread.sleep(100);
}}catch(Exception e){}
}}class prg16{
public static void main(String s[]){
MyThread3 t1=new MyThread3();MyThread3 t2=new MyThread3();
}}
Program No.17
WAP to create and implement own thread.
prg17.java
class MyThread4 extends Thread{
MyThread4(){
start();}public void run(){
try{
for(int i=0;i<=9;i++){
System.out.println(i);Thread.sleep(100);
}}catch(Exception e){}
}}class prg17{
public static void main(String s[]){
MyThread4 t1=new MyThread4();MyThread4 t2 = new MyThread4();
}}