//Assignment No.2
Set A:
a) Create an employee class(id, name, deptname, salary). Define a default and parameterized constructor. Use 'this' keyword to initilize instance variabels. Keep a count of objects cretaed. Create objectes using parameterized constructor and display the objects count after each object is created.(Use static member and method). Also display the contents of each objects.
Code:
👇
- import java.util.*;
- class employee
- {
- int id;
- String name;
- String deptname;
- double salary;
- static int cnt=0;
- employee()
- {
- //Default Constructor
- }
- employee(int id, String name, String deptname, double salary)
- {
- this.id = id;
- this.name = name;
- this.deptname = deptname;
- this.salary = salary;
- cnt++;
- }
- static void objnum()
- {
- System.out.println("Count Of Object:"+cnt);
- }
- void display()
- {
- System.out.println(this.id+"\t"+this.name+"\t\t"+this.deptname+"\t\t"+this.salary);
- }
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter Number Of Employees:");
- int n = sc.nextInt();
- employee e[] = new employee[n];
- for(int i=0; i<n; i++)
- {
- System.out.println("Enter"+(i+1)+"employee data");
- System.out.println("Enter Employee ID");
- int id = sc.nextInt();
- System.out.println("Enter Employee Name");
- String en = sc.next();
- System.out.println("Enter DeptName");
- String dn = sc.next();
- System.out.println("Enter Employee Salary");
- double salary = sc.nextDouble();
- e[i] = new employee(id,en,dn,salary);
- employee.objnum();
- }
- System.out.println("Employees Record Are:");
- System.out.println("ID\tEmployee Name\tDept Name\tSalary");
- for(int i=0; i<n; i++)
- {
- e[i].display();
- }
- sc.close();
- }
- }
//Execute
/*Output
*/
b) Define Student class(roll_no, name, percentage) to create n objects of the Student class. Accept details from the user for each object.Define a static method "sortStudent" which the array on the basis of persentage.
Code:
👇
- import java.util.*;
- class student
- {
- int roll_no;
- String sname;
- double spercentage;
- student()
- {
- //Default Constructor
- }
- student(int roll_no, String sname, double spercentage)
- {
- this.roll_no = roll_no;
- this.sname = sname;
- this.spercentage = spercentage;
- }
- void display()
- {
- System.out.println(this.roll_no+"\t"+this.sname+"\t\t"+this.spercentage);
- }
- static void sort(student a[], int n)
- {
- student temp = new student();
- for( int i=0; i<n; i++)
- {
- for(int j=i+1; j<n; j++)
- {
- if(a[i].spercentage > a[j].spercentage)
- {
- temp = a[i];
- a[i] = a[j];
- a[j] = temp;
- }
- }
- }
- }
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter Number Of Students:");
- int n = sc.nextInt();
- student s[] = new student[n];
- for(int i=0; i<n; i++)
- {
- System.out.println("Enter "+(i+1)+" student data");
- System.out.println("Enter Student Roll No");
- int roll_no = sc.nextInt();
- System.out.println("Enter Student Name");
- String sn = sc.next();
- System.out.println("Enter Percentage of student:");
- double spercentage = sc.nextDouble();
- s[i] = new student(roll_no,sn,spercentage);
- }
- System.out.println("\nStudents Record Are:");
- System.out.println("Roll No\tStudent Name\tPercentage");
- for(int i=0; i<n; i++)
- {
- s[i].display();
- }
- student.sort(s,n);
- System.out.println("\nStudents Sorted Record Are:");
- System.out.println("Roll No\tStudent Name\tPercentage");
- for(int i=0; i<n; i++)
- {
- s[i].display();
- }
- sc.close();
- }
- }
//Execute
/*Output
Enter Number Of Students:
2
Enter 1 student data
Enter Student Roll No
1
Enter Student Name
Ajay
Enter Percentage of student:
84
Enter 2 student data
Enter Student Roll No
2
Enter Student Name
Khushal
Enter Percentage of student:
81
Students Record Are:
Roll No Student Name Percentage
1 Ajay 84.0
2 Khushal 81.0
Students Sorted Record Are:
Roll No Student Name Percentage
2 Khushal 81.0
1 Ajay 84.0
*/
c) Write a java program to accept 5 numbers using command line arguments sort and display them.
Code:
👇
- public class sort
- {
- public static void main(String[] args)
- {
- if (args.length == 5)
- {
- int a[] = new int[5];
- for(int i=0; i<5; i++)
- {
- a[i] = Integer.parseInt(args[i]);
- }
- for(int i=0; i<5; i++)
- {
- for(int j=i+1; j<5; j++)
- {
- if(a[i] > a[j])
- {
- int temp = a[i];
- a[i] = a[j];
- a[j] = temp;
- }
- }
- System.out.print(a[i]+" ");
- }
- }
- }
- }
//Execute
/*Output
1 2 4 6 8
*/
d) Write a java program that take input as a person name int the format of first, middle and last name and then print it in the from last, first and middle name, where in the middle name first chracter is capital letter.
Code:
👇
- import java.util.*;
- public class name
- {
- String fname,mname,lname;
- int len;
- void accept()
- {
- Scanner s=new Scanner(System.in);
- System.out.println("Enter First Name :");
- fname=s.next();
- System.out.println("Enter Middle Name :");
- mname=s.next();
- System.out.println("Enter Last Name :");
- lname=s.next();
- len=mname.length();
- String f=mname.substring(0,1);
- String l=mname.substring(1,len);
- f=f.toUpperCase();
- mname=f+l;
- }
- void display()
- {
- System.out.println("Last Name :"+lname);
- System.out.println("First Name :"+fname);
- System.out.println("Middle Name :"+mname);
- }
- public static void main(String a[])
- {
- name n = new name();
- n.accept();
- n.display();
- }
- }
//Execute
/*
Enter First Name :
robert
Enter Middle Name :
john
Enter Last Name :
downey
Last Name :downey
First Name :robert
Middle Name :John
*/
Set B:
a) Write a java program to create a package "Sy" which has a class SYMarks(members-ComputerTotal, MathsTotal, and ElectronicsTotal). Create another package TY which has a class TYMarks(members-Theory, Practicals). Create n objects of Students class(having rollNumber, name,SyMarks and TYMarks). Add the marks of SY and TY computer subjects and calculate the Grade(''A for >= 70, 'B' for>=60, 'C' for>=50, Pass Class for => 40 else 'FAIL') and display the result of the student in proper format.
Code:
👇
//SYMarks Package
- package SY;
- public class SYMarks
- {
- public int ComputerTotal;
- public int MathsTotal;
- public int ElectronicsTotal;
- public SYMarks(int c, int m, int e)
- {
- ComputerTotal = c;
- MathsTotal = m;
- ElectronicsTotal = e;
- }
- public String toString()
- {
- return (ComputerTotal +"\t\t"+MathsTotal+"\t\t"+ElectronicsTotal);
- }
- }
//TYMarks Package
- package TY;
- public class TYMarks
- {
- public int Theory;
- public int Practicals;
- public TYMarks(int t, int p)
- {
- Theory = t;
- Practicals = p;
- }
- public String toString()
- {
- return ("\t"+Theory +"\t\t"+Practicals);
- }
- }
Student Class
- import java.util.*;
- import SY.SYMarks;
- import TY.TYMarks;
- public class Student
- {
- int rollno;
- String studentname, grade;
- SYMarks sym;
- TYMarks tym;
- Student(int r, String name, SYMarks s, TYMarks t)
- {
- rollno = r;
- studentname = name;
- sym = s;
- tym = t;
- }
- public String toString()
- {
- return rollno+"\t"+studentname+"\t"+sym+"\t"+tym+"\t";
- }
- void calculategrade()
- {
- int total = sym.ComputerTotal + tym.Theory + tym.Practicals;
- double per = total / 3;
- if(per >= 70)
- {
- System.out.println("\tA");
- }
- else
- if(per >= 60)
- {
- System.out.println("\tB");
- }
- else
- if(per >= 50)
- {
- System.out.println("\tC");
- }
- else
- if(per >= 40)
- {
- System.out.println("\tPass");
- }
- else
- System.out.println("\tFail");
- }
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- System.out.println("How Many Students:");
- int n = sc.nextInt();
- Student[] stud = new Student[n];
- for(int i=0; i<n; i++)
- {
- System.out.println("Enter "+(i+1)+"Student data:");
- System.out.println("Enter roll_no:\t");
- int roll_no = sc.nextInt();
- System.out.println("Enter Student Name:\t");
- String name = sc.next();
- System.out.println("Enter SY Marks (Comp Total, Maths Total, Elec Total)");
- int ct = sc.nextInt();
- int mt = sc.nextInt();
- int et = sc.nextInt();
- SYMarks sym = new SYMarks(ct,mt,et);
- System.out.println("Enter TY Marks(Theory and Practicals)");
- int th = sc.nextInt();
- int pr = sc.nextInt();
- TYMarks tym = new TYMarks(th,pr);
- stud[i] = new Student(roll_no, name, sym,tym);
- }
- System.out.println("\n>>SUDENT DETAILS<<");
- System.out.println("R.no\tName\tCompTotal\tMathsTotal\tElecTotal\tTheory\tPracticals\t\tGrade");
- for(int i=0; i < n; i++)
- {
- System.out.print(stud[i]);
- stud[i].calculategrade();
- }
- sc.close();
- }
- }
//Execute
/*Output
How Many Students:
2
Enter 1Student data:
Enter roll_no:
1
Enter Student Name:
Robert
Enter SY Marks (Comp Total, Maths Total, Elec Total)
84
81
82
Enter TY Marks(Theory and Practicals)
72
79
Enter 2Student data:
Enter roll_no:
2
Enter Student Name:
John
Enter SY Marks (Comp Total, Maths Total, Elec Total)
97
89
81
Enter TY Marks(Theory and Practicals)
87
83
>>SUDENT DETAILS<<
R.no Name CompTotal MathsTotal ElecTotal Theory Practicals Grade
1 Robert 84 81 82 72 79 A
2 John 97 89 81 87 83 A
*/
b) Define a class CricketPlayer(name, no_of_innings, no_of_times_notout, totalturns, bat_avg). Create an array of n player objects. Calculate the batting average for each player using static method avg(). Define a static sort method which sorts the array on the basis of average.Display the player details in sorted order.
Code:
👇
- import java.util.*;
- public class CricketPlayer
- {
- String name;
- int no_of_innings;
- int no_of_times_notout;
- double totalruns;
- double bat_avg;
- void setdata(String pn, int ni, int no, Double tr)
- {
- name = pn;
- no_of_innings = ni;
- no_of_times_notout = no;
- totalruns = tr;
- }
- static void avg(CricketPlayer a[], int n)
- {
- for(int i=0; i<n; i++)
- {
- a[i].bat_avg = (a[i].totalruns / (a[i].no_of_innings - a[i].no_of_times_notout));
- }
- }
- void display()
- {
- System.out.println(name+"\t\t\t"+no_of_innings+"\t\t"+no_of_times_notout+"\t\t"+totalruns+"\t\t"+bat_avg);
- }
- static void sort(CricketPlayer a[], int n)
- {
- CricketPlayer temp = new CricketPlayer();
- for(int i=0; i<n-1; i++)
- {
- for(int j=i+1; j<n; j++)
- {
- if(a[i].bat_avg < a[j].bat_avg)
- {
- temp = a[i];
- a[i] = a[j];
- a[j] = temp;
- }
- }
- }
- }
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter no of Player:");
- int n = sc.nextInt();
- CricketPlayer cp[] = new CricketPlayer[n];
- for(int i = 0; i < n; i++)
- {
- cp[i] = new CricketPlayer();
- System.out.println("Enter"+(i+1)+" player data:");
- System.out.println("Enter Player Name:");
- String pn = sc.next();
- System.out.println("Enter No of Innings");
- int ni = sc.nextInt();
- System.out.println("Enter No of Times Not Out");
- int no = sc.nextInt();
- System.out.println("Enter Total Runs:");
- Double tr = sc.nextDouble();
- cp[i].setdata(pn,ni,no,tr);
- }
- System.out.println("\n\t\t\t\t**Player Records**\n");
- System.out.println("Player Name \t No of Innings \t No of Times Notout \t Total Runs \t Bat Avg");
- CricketPlayer.avg(cp,n);
- for(int i=0; i < n; i++)
- {
- cp[i].display();
- }
- System.out.println("\n\t\t\t\t**Sorted Player Records**\n");
- CricketPlayer.sort(cp,n);
- System.out.println("Player Name \t No of Innings \t No of Times Notout \t Total Runs \t Bat Avg");
- for(int i=0; i < n; i++)
- {
- cp[i].display();
- }
- sc.close();
- }
- }
Set C:
a) Write a package for String operation which has two classes Con and Comp.Con class has to concatenates two string and comp class compares two strings. Also display proper message on execution.
Code:
👇
b) Create four member variables for Customer class. Assign public,private,protected and default access modifiers respectively to these variables. Try to access these variables from other classes(Same package and Different package).
Code:
👇
//ThE ProFessoR