1. Create a class Emp, which contains details about the employee and compile and run its
instance.
public class Emp {
int id;
String Name;
double sal;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}//setter getter end here
public Emp() {
this.id = 100;
Name = "Amol";
this.sal = 50000;
}
public Emp(int id, String name, double sal) {
super();
this.id = id;
Name = name;
this.sal = sal;
}
void display()
{
System.out.println("Id is :"+id);
System.out.println("Name is :"+Name);
System.out.println("Salary is :"+sal);
}
}
class test
{
public static void main(String[] args) {
Emp e=new Emp(101,"Akshay",8000);
e.display();
System.out.println();
Emp e1=new Emp(101,"Akshay",8000);
e1.display();
System.out.println();
Emp e2=new Emp(101,"Akshay",8000);
e2.display();
Emp e3=new Emp(101,"Akshay",8000);
e3.display();
}
}
2. Create an inner class for a manager, which contains information about the manager.
Use the appropriate interfaces. Create an anonymous inner class for Technical skills.
public class Empl {
class Manager{
int id;
String Name;
double sal;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}//end here setter and getter
public Manager() {
this.id = 100;
Name = "Amol";
this.sal = 50000;
}
public Manager(int id, String name, double sal) {
super();
this.id = id;
Name = name;
this.sal = sal;
}
//constructor end here
void display()
{
System.out.println("Id is :"+id);
System.out.println("Name is :"+Name);
System.out.println("Salary is :"+sal);
}
}
}
class test1{
public static void main(String[] args) {
Empl e=new Empl();
Empl.Manager m=new Empl().new Manager();
m.display();
}
}
3. Create an appropriate data structures to store employee object and use the
java.util.package properties.
import java.util.ArrayList;
import java.util.*;
public class Emp1 {
int id;
String Name;
double sal;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
} // end here setter and getter
public Emp1() {
this.id = 100;
Name = "Amol";
this.sal = 50000;
}
public Emp1(int id, String name, double sal) {
super();
this.id = id;
Name = name;
this.sal = sal;
}
void display() {
System.out.println("Id is :" + id);
System.out.println("Name is :" + Name);
System.out.println("Salary is :" + sal);
}
}
class Demo
{
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
ArrayList a=new ArrayList();
Emp1[] b =new Emp1[5];
for(int i=0;i<b.length;i++)
{
b[i]=new Emp1();
System.out.println("Enter the Id");
int a1=s.nextInt();
System.out.println("Enter the Name");
String s1=s.next();
System.out.println("Enter the Salary");
double d=s.nextDouble();
Emp1 w=new Emp1(a1,s1,d);
a.add(b[i]);
}
for(int i=0;i<a.size();i++)
{
b[i].display();
}
}
}
4. Create a user defined exception to check whether your employee exist in your data
structure and using the catch and finally block. Redeem an appropriate solution.
import java.util.*;
import org.omg.CosNaming.NamingContextPackage.NotFound;
public class Emp2 implements Comparable{
int id;
String Name;
double sal;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}//end here setter and getter
public Emp2() {
this.id = 100;
Name = "Amol";
this.sal = 50000;
}
public Emp2(int id, String name, double sal) {
super();
this.id = id;
Name = name;
this.sal = sal;
}
void display()
{
System.out.println("Id is :"+id);
System.out.println("Name is :"+Name);
System.out.println("Salary is :"+sal);
}
@Override
public int compareTo(Object arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean equals(Object obk) {
// TODO Auto-generated method stub
Emp2 e3=(Emp2)obk;
if(this.id==e3.id)
{
return true;
}
else
{
return false;
}
}
}
class Demo1
{
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
ArrayList a1=new ArrayList();
Emp2 e=new Emp2(101,"amol",5000);
Emp2 e2=new Emp2(102,"Akshay",5000);
a1.add(e);
a1.add(e2);
System.out.println("Enter the Id");
int a=s.nextInt();
System.out.println("Enter the Name");
String b=s.next();
System.out.println("Enter Salary");
double d=s.nextDouble();
Emp2 e1=new Emp2(a,b,d);
boolean aa=a1.contains(e1);
if(aa==true)
{
try{
throw new NotFoundException();
}catch(NotFoundException e111)
{
System.out.println(e111);
}
}
else
{
a1.add(e1);
e1.display();
System.out.println("Records Suceefully Added");
}
}
}
******************************************************************
public class NotFoundException extends Exception {
@Override
public String toString() {
// TODO Auto-generated method stub
return "Records Already Exits";
}
}
5. Using the collection framework define an appropriate interface to user registration
application.
import java.util.ArrayList;
import java.util.Scanner;
public interface User {
void Reg(int roll_no,String Name,String Address,int Mobile_Number);
void display();
public boolean equals(Object obj);
}
class Demo2 implements User{
static int a;
static String b;
static String c;
static int d;
public Demo2() {
this.a = 1;
this.b = "Amol";
this.c = "Baramati";
this.d = 1234567890;
}
static Scanner s=new Scanner(System.in);
@Override
public void Reg(int roll_no, String Name, String Address, int Mobile_Number) {
// TODO Auto-generated method stub
roll_no=a;
Name=b;
Address=c;
Mobile_Number=d;
}
public Demo2(int a, String b, String c, int d) {
super();
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
@Override
public void display() {
// TODO Auto-generated method stub
System.out.println("Roll_number is a :"+a);
System.out.println("Name is a : "+b);
System.out.println("Address is a : "+c);
System.out.println("Mobile Number is a :"+d);
}
public static void main(String[] args) {
Demo2 d1=new Demo2();
d1.display();
System.out.println();
System.out.println("Enter the Roll_Number");
a=s.nextInt();
System.out.println("Enter the Name");
b=s.next();
System.out.println("Enter the Adress");
c=s.next();
System.out.println("Enter The Mobile Number");
d=s.nextInt();
Demo2 d11=new Demo2(a,b,c,d);
d11.display();
}
}
7. Create Employee & manager classes and make Employee, manager classes objects
persistent.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class Employee implements Serializable {
int id;
String Name;
double sal;
int getId() {
return id;
}
void setId(int id) {
this.id = id;
}
String getName() {
return Name;
}
void setName(String name) {
Name = name;
}
double getSal() {
return sal;
}
void setSal(double sal) {
this.sal = sal;
}// Setter And Getter End Here
public Employee() {
this.id = 100;
Name = "Amol";
this.sal = 90000;
}
public Employee(int id, String name, double sal) {
super();
this.id = id;
Name = name;
this.sal = sal;
}
// Construtor End Here
void display() {
System.out.println("Id is a :" + id);
System.out.println("Name is a :" + Name);
System.out.println("Salary is a :" + sal);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Id is a :" + id+" "+ Name +" "+ " " + sal;
}
} // Employee Class Here
class Manager extends Employee {
int target;
int incentive;
int getTarget() {
return target;
}
void setTarget(int target) {
this.target = target;
}
int getIncentive() {
return incentive;
}
void setIncentive(int incentive) {
this.incentive = incentive;
}// end here setter and getter
public Manager() {
this.target = 50;
this.incentive = 500;
}
public Manager(int a, String b, double p, int target, int incentive) {
super(a, b, p);
this.target = target;
this.incentive = incentive;
}
void display() {
super.display();
System.out.println("Target is a : " + target);
System.out.println("Incentive is a : " + incentive);
}
}// MAnager Class End Here
class Dewmo {
public static void main(String[] args) {
ArrayList a = new ArrayList();
Scanner s = new Scanner(System.in);
System.out.println("Defalut Information Here ");
Manager m = new Manager();
m.display();
System.out.println();
System.out.println();
System.out.println();
int ch;
do {
System.out.println("1) Writing Data to File..!!");
System.out.println("2) Reading Data to File..!!");
System.out.println("Enter your Chocice");
ch = s.nextInt();
switch (ch) {
case 1: {
Manager m1 = new Manager(100, "Akshay", 80000, 60, 7800);
Manager m2 = new Manager(200, "Amol", 90000, 70, 7896);
Manager m3 = new Manager(100, "Akshay", 80000, 60, 7800);
Manager m4 = new Manager(100, "Akshay", 80000, 60, 7800);
Manager m5 = new Manager(100, "Akshay", 80000, 60, 7800);
Manager m6 = new Manager(100, "Akshay", 80000, 60, 7800);
Manager m7 = new Manager(100, "Akshay", 80000, 60, 7800);
Manager m8 = new Manager(100, "Akshay", 80000, 60, 7800);
Manager m9 = new Manager(100, "Akshay", 80000, 60, 7800);
Manager m10 = new Manager(100, "Akshay",80000, 60, 7800);
a.add(m1);
a.add(m2);
a.add(m3);
a.add(m4);
a.add(m5);
a.add(m6);
a.add(m7);
a.add(m8);
a.add(m9);
a.add(m10);
try {
FileOutputStream fio = new FileOutputStream(
"D:\\amol\\khushi\\amui.txt");
ObjectOutputStream o = new ObjectOutputStream(fio);
o.writeObject(a);
System.out.println("Write Suessfully...!!");
o.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
case 2: {
try {
FileInputStream fi = new FileInputStream(
"D:\\amol\\khushi\\amui.txt");
try {
ObjectInputStream oa = new ObjectInputStream(fi);
try {
ArrayList a1 = ((ArrayList) oa.readObject());
// System.out.println(a1);
Iterator it = a1.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
oa.close();
} catch (ClassNotFoundException e6) {
// TODO Auto-generated catch block
e6.printStackTrace();
}
} catch (IOException e6) {
// TODO Auto-generated catch block
e6.printStackTrace();
}
} catch (FileNotFoundException e6) {
// TODO Auto-generated catch block
e6.printStackTrace();
}
break;
}
}
} while (ch != 10);
}
}
9. Create a new array, whose size and component type are not known until runtime, and
then modify the array's components
import java.util.Scanner;
public class Array1 {
int id;
String Name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}//End Here Setter And Getter
public Array1() {
this.id = 100;
this.Name = "Amol";
}
public Array1(int id, String name) {
super();
this.id = id;
Name = name;
} //Constructor End Here
void display()
{
System.out.println("ID is a :" +id);
System.out.println("Number is a : "+Name);
}
}
class demoa
{
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("Enter the size of Array u want");
int n=s.nextInt();
Array1 []a=new Array1[n];
for(int i=0;i<a.length;i++)
{
System.out.println("Enter The Id");
int b=s.nextInt();
System.out.println("Enter The Name");
String h=s.next();
a[i]=new Array1();
a[i].display();
}
}
}