Pages

Monday, 7 March 2011

SEM1 Java ass 3


****************************Ass:3.Set:A.Que:1*********************************
Que)       Define a class Employee having private members – id, name, department, salary.
Define default and parameterized constructors. Create a subclass called “Manager” with
private member bonus. Define methods accept and display in both the classes. Create n
objects of the Manager class and display the details of the manager having the maximum
total salary (salary+bonus)

*********************************Java Code***********************************

import java.io.*;
class emp
{
        private int id;
        private String name,dept;
        private float salary;

        public emp()
        {
                id=0;
                dept=" ";
                name=" ";
                salary=0;
        }

        public emp(int i,String d,String nm,float sal)
        {
                id=i;
                dept=d;
                name=nm;
                salary=sal;
        }

        public void accept()throws IOException
        {
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

                System.out.println("Enter ID = ");
                id=Integer.parseInt(br.readLine());
                System.out.println("Enter Name = ");
                name=(br.readLine());
                System.out.println("Enter Department = ");
                dept=(br.readLine());
                System.out.println("Enter Salary = ");
                salary=Float.parseFloat(br.readLine());
        }

        public void display()
        {
                System.out.println("Id = "+id);
                System.out.println("Name = "+name);
                System.out.println("Department = "+dept);
                System.out.println("Salary = "+salary);
        }

        public float retsal()
        {
                return salary;
        }

}

class manager extends emp
{
         private float bonus;

        public manager()
        {
                bonus=0;
        }

        public manager(float b)
        {
                bonus=b;
        }

        public void accept()throws IOException
        {
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

                super.accept();
                System.out.println("Enter Bonus = ");
                bonus=Float.parseFloat(br.readLine());
        }

        public void display()
        {
                super.display();
                System.out.println("Bonus = "+bonus);
        }


        float computesal()
        {
                return(super.retsal()+bonus);
        }

}

class sa1
{
        public static void main(String[] arg)throws IOException
        {
                int i,n;
                float max;

                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

                System.out.println("Enter Number of objects = ");
                n=Integer.parseInt(br.readLine());

                manager m[] = new manager[n];
                float tot[] = new float[n];

                for(i=0;i<n;i++)
                {
                        m[i] = new manager();
                        m[i].accept();
                }

                System.out.println("\nRecords Are\n");

                for(i=0;i<n;i++)
                {
                        m[i].display();
                        tot[i]=m[i].computesal();
                }

                max=tot[0];

                for(i=1;i<n;i++)
                {
                        if(tot[i]>max)
                                max=tot[i];
                }

                System.out.println("\n\nManger with Maximum Salary is \n");

                for(i=0;i<n;i++)
                {
                        if(tot[i] == max)
                        {
                                m[i].display();
                                System.out.println("Maximum salary is = "+max);
                        }
                }
        }
}

/*
[root@localhost remains javapro]# java sa1
Enter Number of objects =
2
Enter ID =
1
Enter Name =
Rutuja
Enter Department =
IT
Enter Salary =
50000
Enter Bonus =
4000
Enter ID =
2
Enter Name =
Vinaya
Enter Department =
Elex
Enter Salary =
40000
Enter Bonus =
6000

Records Are

Id = 1
Name = Rutuja
Department = IT
Salary = 50000.0
Bonus = 4000.0
Id = 2
Name = Vinaya
Department = Elex
Salary = 40000.0
Bonus = 6000.0


Manger with Maximum Salary is

Id = 1
Name = Rutuja
Department = IT
Salary = 50000.0
Bonus = 4000.0
Maximum salary is = 54000.0

*/
******************************************************************************




****************************Ass:3.Set:A.Que:2*********************************
Que)        Define an interface “IntOperations” with methods to check whether an integer is
positive, negative, even, odd, prime and operations like factorial and sum of digits. Define
a class MyNumber having one private int data member. Write a default constructor to
initialize it to 0 and another constructor to initialize it to a value (Use this). Implement the
above interface. Create an object in main. Use command line arguments to pass a value to
the object and perform the above operations using a menu.



*********************************Java Code***********************************

import java.io.*;
import java.lang.*;

interface IntOperation
{
        void isPositive(int n);
        void isNegative(int n);
        void isEven(int n);
        void isOdd(int n);
        void isPrime(int n);
        void facto(int n);
        void sum(int n);
}

class MyNumber implements IntOperation
{
        private int num;
        MyNumber()
         {
           num=0;
         }

        MyNumber(int num)
        {
                this.num=num;
        }

        public void isPositive(int num)
        {


                if(num > 0)
                        System.out.println("\n\n"+ num + " is Positive");
                else
                        System.out.println("\n\n"+ num + " is not Positive");

        }

        public void isNegative(int num)
        {
               if(num < 0)
                        System.out.println("\n\n"+ num + " is Negative");
                else
                        System.out.println("\n\n"+ num + " is not Negative");
        }

        public void isEven(int num)
        {
               if(num % 2 == 0)
                        System.out.println("\n\n"+ num + " is Even");
                else
                        System.out.println("\n\n"+ num + " is not Even");

        }

        public void isOdd(int num)
        {


                if(this.num % 2 != 0)
                        System.out.println("\n\n"+ num + " is Odd");
                else
                        System.out.println("\n\n"+ num + " is not Odd");

        }

        public void facto(int num)
        {
                int fact=1,i,no=num;

                System.out.println("n == "+ num);
                if(no < 0)
                {
                        no=no-no-no;
                        System.out.println("n == "+ no);

                }
                for(i=1;i<num+1;i++)
                {
                        fact=fact*i;
                }

                System.out.println("\n\nFactorial is "+fact);
        }

        public void sum(int num)
        {
                int sum=0,no=num;
  
                while(no != 0)
                {
                        sum=sum+(no%10);
                        no=no/10;
                }

                System.out.println("\n\nSum of digits "+sum);

        }

        public void isPrime()
        {
                int i,j=0;

                for(i=2;i<num;i++)
                {
                        if(num%i==0)
                                j=1;
                }
                if(j == 0 || num == 1)
                        System.out.println("\n\n"+ num + " is Prime");
                else
                        System.out.println("\n\n"+ num + " is not Prime");
        }
}

class operation
{
        public static void main(String[] arg)
        {
          IntOperation m;
          MyNumber n1=new MyNumber();
             m=n1;
          int n=Integer.parseInt(args[0]);
              n1.isPositive(n);
              n1.isNegative(n);
              n1.iseven(n);
              n1.isodd(n);
              n1.isprime(n);
              n1.factorial(n);
              n1.sum(n);
        }
}
***********************************Output************************************
[root@localhost 3017java]#java operation -10
Number is Negative
Number is even
Number is prime
fact=1
Sum of digit:0
*******************************************************************************



****************************Ass:3.Set:A.Que:3*********************************
Que)     Define an interface “StackOperations” which declares methods for a static stack.
Define a class “MyStack” which contains an array and top as data members and
implements the above interface. Initialize the stack using a constructor. Write a menu
driven program to perform operations on a stack object.



*********************************Java Code***********************************

import java.io.*;
interface StackOperations
{
int max=10;
        void push(int data);
        void pop();
        int isempty();
        int isfull();

}
class Mystack implements StackOperations
{
        private int arr[]=new int[StackOperations.max];
        private int top;
        Mystack()
        {     
                top=-1;
        }
        public void push(int data)
        {
        arr[++top]=data;
        }
        public void pop()
        {
                System.out.println("Poped element is :"+arr[top]);
                top--;
        }
        public int isempty()
        {
                if(top==-1)
                        return 1;
                else
                        return 0;
        }
        public int isfull()
        {
                if(top==9)
                        return 1;
                else
                        return 0;
        }
        public static  void main(String arg[])throws IOException
        {
int ch,data;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                Mystack s=new Mystack();
                do
                {
                        System.out.println("\n1:Push");
                        System.out.println("2:Pop");
                        System.out.println("3:Exit.");
                        System.out.println("\nEnter your choice:");
                        ch=Integer.parseInt(br.readLine());
                        switch(ch)
                        {
                                case 1:if(s.isfull()==1)
                                       {      
                                               System.out.println("Stack is full");
                                       }
                                       else
                                       {
                     System.out.println("Enter the data :");
                      data=Integer.parseInt(br.readLine());

               s.push(data);
                                       }
                                       break;
                                case 2:if(s.isempty()==1)
                                       {
                                               System.out.println("Stack is empty");
                                       }
                                       else
                                       {
                                               s.pop();
                                       }
                                       break;
                                case 3:System.exit(0);
                                       break;
                                default:System.out.println("\nInvalid choice:");
                        }
        }while(ch!=4);
}
}

**********************************Output*************************************
1:Push
2:Pop
3:Exit
Enter your choice:
1
Enter the Data:
9
1:Push
2:Pop
3:Exit
Enter your choice:
1
Enter the Data:
8
1:Push
2:Pop
3:Exit
Enter your choice:
2
Poped value=8
1:Push
2:Pop
3:Exit
Enter your choice:
3
******************************************************************************


****************************Ass:3.Set:B.Que:1*********************************
Que)      Define a class “Employee” which has members id, name, date of birth. Define
another class “Manager” which has members department name and joining date and
extends Employee. Create n objects of the manager class and clone them. (Use the
Cloneable interface)



*********************************Java Code***********************************

import java.io.*;
class employee
{
 int id;
 String name;
 String dob;
 public employee
  {
   id=0;
   name=null;
   dob=null;
  }
 public employee(int i,String n,String d)
  {
   id=i;
   name=n;
   dob=d;
  }
 void show()
  {
   System.out.println("employee id="+id);
   System.out.println("employee name="+name);
   System.out.println("date of birth="+dob);
  }
}

class manager extends employee implements Clonable
{
 String jdate;
 String depname;
 public manager(int i.String n,String d,String s,String b)
  {
   super(i,n,d);
   jdate=s;
   depname=b;
  }
 public manager clone1()throws CloneNotSupportedException
  {
   manager m=(manager)super.clone();
   return m;
  }
 void display()
  {
   super.show();
   System.out.println("date of joining="+jdate);
   System.out.println("department name="+depname);
  }
}

class testclone
{
 public static void main(String[] args)throws IOException,CloneNotSupportedException
 {
  int n,i,f;
  String nm,d;
  String s,b;
 
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  System.out.println("enter the no. of employee");
  n=Integer.parseInt(br.readLine());
  manager[] marray=new manager[n];
  manager[] marray1=new manager[n];
  for(i=0;i<n;i++)
  {
   System.out.println("enter the id");
   I=Integer.parseInt(br.readLine());
   System.out.println("enter the name");
   nm=br.readLine();
   System.out.println("enter the date of birth");
   d=br.readLine();
   System.out.println("enter the date of joining");
   s=br.readLine();
   System.out.println("enter the department name");
   b=br.readLine();

   marray[i]=new manager(I,nm,d,s,b);
   marray1[i]=marray[i].clone1();
  }
 for(i=0;i<n;i++)
  {
   marray1[i].display();
  }
 }
}

//Output
[root@station5~]#java testclone
enter the no. of employee
1
enter the id
1
enter the name
Vinaya
enter the date of birth
07.04.1990
enter the date of joining
29.11.2011
enter the department name
mechanical

employee id=1
employee name=Vinaya
date of birth=07.04.1990
date of joining=29.11.2011
department name=mechanical
*****************************************************************************


****************************Ass:3.Set:B.Que:2*********************************

Que)     Define an abstract class “Staff” with members name and address. Define two sub-
classes of this class – “FullTimeStaff” (department, salary) and “PartTimeStaff” (number-
of-hours, rate-per-hour). Define appropriate constructors. Create n objects which could
be of either FullTimeStaff or PartTimeStaff class by asking the user’s choice. Display
details of all “FullTimeStaff” objects and all “PartTimeStaff” objects.

*********************************Java Code***********************************

import java.io.*;

abstract class Staff
{
    private String name,add;
    Staff()
    {
        name=add=" ";
    }
    Staff(String n,String a)
    {
        name=n;
        add=a;
    }
    public void accept()throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("NAME :");
        name=br.readLine();
        System.out.println("ADDRESS :");
        add=br.readLine();
    }
    public void display()
    {
        System.out.println("Name :"+name+"\nAddress"+add);
    }
}
class FullTimeStaff extends Staff
{
    private String dept;
    private int salary;
    FullTimeStaff()
    {
        super();
        dept=" ";
        salary=0;
    }
    FullTimeStaff(String n,String a,String d,int s)
    {
        super(n,a);
        dept=d;
        salary=s;
    }
    public void accept() throws IOException
    {
        super.accept();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Dept :");
        dept=br.readLine();
        System.out.println("Salary :");
        salary=Integer.parseInt(br.readLine());
    }
    public void display()
    {
        super.display();
        System.out.println("Department :"+dept+"\nSalary"+salary);
    }
}
class PartTimeStaff extends Staff
{
    private int no_of_hr;
    private double rate_per_hr;
    PartTimeStaff()
    {
        super();
        no_of_hr=0;
        rate_per_hr=0;
    }
    PartTimeStaff(String n,String a,int h,int r)
    {
        super(n,a);
        no_of_hr=h;
        rate_per_hr=r;
    }
    public void accept() throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        super.accept();
        System.out.println("NO OF HOUR :");
        no_of_hr=Integer.parseInt(br.readLine());
        System.out.println("RATE PER HOUR :");
        rate_per_hr=Double.parseDouble(br.readLine());
    }
    public void display()
    {
        super.display();
        System.out.println("No_of_hour :"+no_of_hr+"\nRate_per_hour"+rate_per_hr);
    }
}
class test
{
    public static void main(String arg[]) throws IOException
    {
        int no,n;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("\n1:FullTimeStaff :");
        System.out.println("2:PartTimeStaff :");
        System.out.println("Which objects you want to create(1/2)? ");
        no=Integer.parseInt(br.readLine());
        System.out.println("Enter how many objects you want to create:");
        n=Integer.parseInt(br.readLine());

        Staff m[]=new Staff[n];

        if(no==1)
        {
            for(int i=0;i<m.length;i++)
            {
                m[i]=new FullTimeStaff(); 
                m[i].accept();

            }
            for(int i=0;i<m.length;i++)
            {
                m[i].display();
            }
        }
        else if(no==2)
        {
            for(int i=0;i<m.length;i++)
            {
                m[i]=new PartTimeStaff();
                m[i].accept();

            }
            for(int i=0;i<m.length;i++)
            {
                m[i].display();
            }

        }
        else
        {
            System.out.println("Wrong choice :");
        }
    }
}
/*
[root@localhost remains javapro]# java test

1:FullTimeStaff :
2:PartTimeStaff :
Which objects you want to create(1/2)?
1
Enter how many objects you want to create:
2
NAME :
Trupti
ADDRESS :
Nigadi
Dept :
IT
Salary :
60000
NAME :
Vinaya
ADDRESS :
Pune
Dept :
ELEX
Salary :
50000
Name :Trupti
AddressNigadi
Department :IT
Salary60000
Name :Vinaya
AddressPune
Department :ELEX
Salary50000
[root@localhost remains javapro]# java test

1:FullTimeStaff :
2:PartTimeStaff :
Which objects you want to create(1/2)?
2
Enter how many objects you want to create:
2
NAME :
Rutuja
ADDRESS :
Talegaon
NO OF HOUR :
3
RATE PER HOUR :
10000
NAME :
Pooja
ADDRESS :
Pune
NO OF HOUR :
4
RATE PER HOUR :
5600
Name :Rutuja
AddressTalegaon
No_of_hour :3
Rate_per_hour10000.0
Name :Pooja
AddressPune
No_of_hour :4
Rate_per_hour5600.0
*/
******************************************************************************





No comments:

Post a Comment