sample file Code File

Click On this to Download The File  

1.

class Person

{

String name,dob;

double mno;

Person(String name,double mno,String dob)

{

 this.name=name;

 this.mno=mno;

 this.dob=dob;

}

void print_data()

{

 System.out.println("Name:"+name);

 System.out.println("Mobile number:"+mno);

 System.out.println("Date of birth:"+dob);

}

}

class Employee extends Person

{

int eno,salary;

String org,des,doj;

Employee(int eno,String org,String des,int salary,String doj)

{

 super("John",9876543210d,"10/11/1998");

 this.eno=eno;

 this.org=org;

 this.des=des;

 this.salary=salary;

 this.doj=doj;

}

void print_info()

{

 super.print_data();

 System.out.println("Emp number:"+eno);

 System.out.println("Organization Name:"+org);

 System.out.println("Designation:"+org);

 System.out.println("Date of birth:"+dob);

}

}

class Imple_exam

{

public static void main(String args[])

{

 Employee e=new Employee(101,"Infosys","Software developer",100000,"01/01/2023");

 e.print_info();

}

}

2. WAP to print the count of vowels in a given string.

import java.lang.*;

import java.util.*;

class Count_vowels

{

 public static void main(String args[])

{


 String msg,str;

 Scanner s=new Scanner(System.in);

 System.out.println("The given String is: ");

 msg=s.nextLine();

 str=msg.toLowerCase();

 int i,c = 0;

 char ch;

 for(i=0;i<str.length();i++)

 {

 ch=str.charAt(i);

 if(ch=='a'||ch=='e'||ch=='o'||ch=='i'||ch=='u')

 {

 c++;

 }

 }


 System.out.println("Number of vowels in given string: " +c);

 }

}

3.

Write a program to find area of circle, area of rectangle & area of

triangle by overloading functions.

import java.lang.*;

class Shape

{

 void area(int r)

 {

 double a;

 a=3.14*r*r;

 System.out.println("Area of circle is"+a);

 }

 void area(int l,int w)

 {

 double a;

 a=l*w;

 System.out.println("Area of rectangle is"+a);

 }

 void area(float b,float h)

 {

 double a;

 a=0.5*b*h;

 System.out.println("Area of triangle is"+a);

 }

}

class Area_tri

{

 public static void main(String args[])

 {

 Shape s=new Shape();

 s.area(3);

 s.area(3,4);

 s.area(2.0f,5.0f );

 }

}

4. Write a program to demonstrate exception handling using try,

catch and finally block for ArrayIndexOutOfBoundsException.

class Demo_exception

{

 public static void main (String[] args)

{

int a[] ={5,10};

int b=5;

try

{

int x = a[2]/(b-a[1]);

}

catch(ArrayIndexOutOfBoundsException e)

{

System.out.println("Array index error...");

}

finally

{

System.out.println("finally block executed");

int y=a[1]/a[0];

System.out.println(y);

}

}

}

5.

WAP uses a vector class to add 5 names of students initially.

Display a MENU: i) Add new name ii) Display names

Based on the choice entered by the user take appropriate action.

import java.util.*;

class Student_vector

{

public static void main(String args[])

{

Scanner br=new Scanner(System.in);

String str;

Vector v=new Vector();

int i,choice=0;

System.out.println("Enter 5 names:");

for(i=0;i<=4;i++)

{

 str=br.next();

 v.addElement(str);

}

while(choice!=3)

{

System.out.println("1.Add new name\n2.Display name\n3.Exit");

System.out.println("Enter your choice:");

choice=br.nextInt();

switch(choice)

{

case 1: System.out.println("Enter name:");

str=br.next();

v.addElement(str);

break;

case 2:

System.out.println("Current List:");

for(i=0;i<=v.size()-1;i++)

{

System.out.println(v.elementAt(i));

}

case 3:

break;

default:

System.out.println("Invalid choice");

}

}

}

}

6.Write programs to explain concept of abstract methods

1. Define abstract superclass Figure

2. Declare an abstract method area() in superclass

3. Define subclass Rectangle

import java.lang.*;

abstract class Figure

{

double dim1,dim2;

 Figure(double a,double b)

{

dim1=a;

dim2=b;

}

abstract double area();

}

class Rectangle extends Figure

{

double area;

Rectangle(double a,double b)

{

super(a,b);

}

double area()

{

 area=dim1*dim2;

 return area;

}

}

class Figure_abstract

{

public static void main(String args[])

{

Figure f;

Rectangle r= new Rectangle(5,5);

f=r;

System.out.println("Area of Rectangle is:"+f.area());

}

}

7. WAP to define interface Department and Exam. Implement class

Student which implements these interfaces.

interface Department

{

 int m1=18,m2=19,m3=12;


}

interface Exam extends Department

{

int s1=67,s2=45,s3=71;

void total_marks();

}

class Student_inter implements Department,Exam

{

public void total_marks()

{

int ds,cg,dlca;

ds=m1+s1;

cg=m2+s2;

dlca=m3+s3;

System.out.println("Marks of DS"+ds);

System.out.println("Marks of CG"+cg);

System.out.println("Marks of DLCA"+dlca);

}

public static void main(String args[])

{

Student_inter s=new Student_inter();

s.total_marks();

}

}

 

Comments

Popular Posts