Skip to main content

Assignment No:2

 //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:

👇

  1. import java.util.*;
  2. class employee
  3. {
  4. int id;
  5. String name;
  6. String deptname;
  7. double salary;
  8. static int cnt=0;
  9. employee()
  10. {
  11. //Default Constructor
  12. }
  13. employee(int id, String name, String deptname, double salary)
  14. {
  15. this.id = id;
  16. this.name = name;
  17. this.deptname = deptname;
  18. this.salary = salary;
  19. cnt++;
  20. }
  21. static void objnum()
  22. {
  23. System.out.println("Count Of Object:"+cnt);
  24. }
  25. void display()
  26. {
  27. System.out.println(this.id+"\t"+this.name+"\t\t"+this.deptname+"\t\t"+this.salary);
  28. }
  29. public static void main(String[] args) 
  30. {
  31. Scanner sc = new Scanner(System.in);
  32. System.out.println("Enter Number Of Employees:");
  33. int n = sc.nextInt();
  34. employee e[] = new employee[n];
  35. for(int i=0; i<n; i++)
  36. {
  37. System.out.println("Enter"+(i+1)+"employee data");
  38. System.out.println("Enter Employee ID");
  39. int id = sc.nextInt();
  40. System.out.println("Enter Employee Name");
  41. String en = sc.next();

  42. System.out.println("Enter DeptName");
  43. String dn = sc.next();

  44. System.out.println("Enter Employee Salary");
  45. double salary = sc.nextDouble();

  46. e[i] = new employee(id,en,dn,salary);
  47. employee.objnum();
  48. }
  49. System.out.println("Employees Record Are:");
  50. System.out.println("ID\tEmployee Name\tDept Name\tSalary");
  51. for(int i=0; i<n; i++)
  52. {
  53. e[i].display();
  54. }
  55. sc.close();
  56. }

//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:

👇

  1. import java.util.*;
  2. class student
  3. {
  4. int roll_no;
  5. String sname;
  6. double spercentage;
  7. student()
  8. {
  9. //Default Constructor
  10. }
  11. student(int roll_no, String sname, double spercentage)
  12. {
  13. this.roll_no = roll_no;
  14. this.sname = sname;
  15. this.spercentage = spercentage;
  16. }
  17. void display()
  18. {
  19. System.out.println(this.roll_no+"\t"+this.sname+"\t\t"+this.spercentage);
  20. }
  21. static void sort(student a[], int n)
  22. {
  23. student temp = new student();
  24. for( int i=0; i<n; i++)
  25. {
  26. for(int j=i+1; j<n; j++)
  27. {
  28. if(a[i].spercentage > a[j].spercentage)
  29. {
  30. temp = a[i];
  31. a[i] = a[j];
  32. a[j] = temp;
  33. }
  34. }
  35. }
  36. }
  37. public static void main(String[] args) 
  38. {
  39. Scanner sc = new Scanner(System.in);
  40. System.out.println("Enter Number Of Students:");
  41. int n = sc.nextInt();
  42. student s[] = new student[n];
  43. for(int i=0; i<n; i++)
  44. {
  45. System.out.println("Enter "+(i+1)+" student data");
  46. System.out.println("Enter Student Roll No");
  47. int roll_no = sc.nextInt();
  48. System.out.println("Enter Student Name");
  49. String sn = sc.next();

  50. System.out.println("Enter Percentage of student:");
  51. double spercentage = sc.nextDouble();

  52. s[i] = new student(roll_no,sn,spercentage);
  53. }
  54. System.out.println("\nStudents Record Are:");
  55. System.out.println("Roll No\tStudent Name\tPercentage");
  56. for(int i=0; i<n; i++)
  57. {
  58. s[i].display();
  59. }
  60. student.sort(s,n);
  61. System.out.println("\nStudents Sorted Record Are:");
  62. System.out.println("Roll No\tStudent Name\tPercentage");
  63. for(int i=0; i<n; i++)
  64. {
  65. s[i].display();
  66. }
  67. sc.close();
  68. }

//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:

👇

  1. public class sort
  2. {
  3. public static void main(String[] args) 
  4. {
  5. if (args.length == 5)
  6. {
  7. int a[] = new int[5];
  8. for(int i=0; i<5; i++)
  9. {
  10. a[i] = Integer.parseInt(args[i]);
  11. }
  12. for(int i=0; i<5; i++)
  13. {
  14. for(int j=i+1; j<5; j++)
  15. {
  16. if(a[i] > a[j])
  17. {
  18. int temp = a[i];
  19. a[i] = a[j];
  20. a[j] = temp;
  21. }
  22. }
  23. System.out.print(a[i]+" ");
  24. }
  25. }
  26. }
  27. }

//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:

👇

  1. import java.util.*;
  2. public class name
  3. {

  4.     String fname,mname,lname;
  5.     int len;
  6.     void accept()
  7.     {
  8.         Scanner s=new Scanner(System.in);
  9.         System.out.println("Enter First Name :");
  10.         fname=s.next();

  11.         System.out.println("Enter Middle Name :");
  12.         mname=s.next();

  13.         System.out.println("Enter Last Name :");
  14.         lname=s.next();

  15.         len=mname.length();
  16.         String f=mname.substring(0,1);
  17.         String l=mname.substring(1,len);
  18.         f=f.toUpperCase();
  19.         mname=f+l;
  20.     }
  21.     void display()
  22.     {
  23.         System.out.println("Last Name :"+lname);
  24.         System.out.println("First Name :"+fname);
  25.         System.out.println("Middle Name :"+mname);
  26.     }
  27.     public static void main(String a[])
  28.     {
  29.         name n = new name();
  30.         n.accept();
  31.         n.display();
  32.     }
  33. }

//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

  1. package SY;
  2. public class SYMarks
  3. {
  4. public int ComputerTotal;
  5. public int MathsTotal;
  6. public int ElectronicsTotal;

  7. public SYMarks(int c, int m, int e)
  8. {
  9. ComputerTotal = c;
  10. MathsTotal = m;
  11. ElectronicsTotal = e;
  12. public String toString()
  13. {
  14. return (ComputerTotal +"\t\t"+MathsTotal+"\t\t"+ElectronicsTotal);
  15. }
  16. }


//TYMarks Package

  1. package TY;
  2. public class TYMarks
  3. {
  4. public int Theory;
  5. public int Practicals;

  6. public TYMarks(int t, int p)
  7. {
  8. Theory = t;
  9. Practicals = p;
  10. public String toString()
  11. {
  12. return ("\t"+Theory +"\t\t"+Practicals);
  13. }
  14. }


Student Class

  1. import java.util.*;
  2. import SY.SYMarks;
  3. import TY.TYMarks;
  4. public class Student
  5. {
  6. int rollno;
  7. String studentname, grade;
  8. SYMarks sym;
  9. TYMarks tym;
  10. Student(int r, String name, SYMarks s, TYMarks t)
  11. {
  12. rollno = r;
  13. studentname = name;
  14. sym = s;
  15. tym = t;
  16. }
  17. public String toString()
  18. {
  19. return  rollno+"\t"+studentname+"\t"+sym+"\t"+tym+"\t";
  20. }
  21. void calculategrade()
  22. {
  23. int total = sym.ComputerTotal + tym.Theory + tym.Practicals;
  24. double per = total / 3;
  25. if(per >= 70)
  26. {
  27. System.out.println("\tA");
  28. }
  29. else
  30. if(per >= 60)
  31. {
  32. System.out.println("\tB");
  33. }
  34. else
  35. if(per >= 50)
  36. {
  37. System.out.println("\tC");
  38. }
  39. else
  40. if(per >= 40)
  41. {
  42. System.out.println("\tPass");
  43. }
  44. else
  45. System.out.println("\tFail");
  46. }
  47. public static void main(String[] args)
  48. {
  49. Scanner sc = new Scanner(System.in);
  50. System.out.println("How Many Students:");
  51. int n = sc.nextInt();

  52. Student[] stud = new Student[n];
  53. for(int i=0; i<n; i++)
  54. {
  55. System.out.println("Enter "+(i+1)+"Student data:");
  56. System.out.println("Enter roll_no:\t");
  57. int roll_no = sc.nextInt();
  58. System.out.println("Enter Student Name:\t");
  59. String name = sc.next();
  60. System.out.println("Enter SY Marks (Comp Total, Maths Total, Elec Total)");
  61. int ct = sc.nextInt();
  62. int mt = sc.nextInt();
  63. int et = sc.nextInt();
  64. SYMarks sym = new SYMarks(ct,mt,et);
  65. System.out.println("Enter TY Marks(Theory and Practicals)");
  66. int th = sc.nextInt();
  67. int pr = sc.nextInt();
  68. TYMarks tym = new TYMarks(th,pr);

  69. stud[i] = new Student(roll_no, name, sym,tym);
  70. }
  71. System.out.println("\n>>SUDENT DETAILS<<");
  72. System.out.println("R.no\tName\tCompTotal\tMathsTotal\tElecTotal\tTheory\tPracticals\t\tGrade");
  73. for(int i=0; i < n; i++)
  74. {
  75. System.out.print(stud[i]);
  76. stud[i].calculategrade();
  77. }
  78. sc.close();
  79. }
  80. }


//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:

👇

  1. import java.util.*;
  2. public class CricketPlayer
  3. {
  4. String name;
  5. int no_of_innings;
  6. int no_of_times_notout;
  7. double totalruns;
  8. double bat_avg;
  9. void setdata(String pn, int ni, int no, Double tr)
  10. {
  11. name = pn;
  12. no_of_innings = ni;
  13. no_of_times_notout = no;
  14. totalruns = tr;
  15. }
  16. static void avg(CricketPlayer a[], int n)
  17. {
  18. for(int i=0; i<n; i++)
  19. {
  20. a[i].bat_avg = (a[i].totalruns / (a[i].no_of_innings - a[i].no_of_times_notout));
  21. }
  22. }
  23. void display()
  24. {
  25. System.out.println(name+"\t\t\t"+no_of_innings+"\t\t"+no_of_times_notout+"\t\t"+totalruns+"\t\t"+bat_avg);
  26. }
  27. static void sort(CricketPlayer a[], int n)
  28. {
  29. CricketPlayer temp = new CricketPlayer();
  30. for(int i=0; i<n-1; i++)
  31. {
  32. for(int j=i+1; j<n; j++)
  33. {
  34. if(a[i].bat_avg < a[j].bat_avg)
  35. {
  36. temp = a[i];
  37. a[i] = a[j];
  38. a[j] = temp;
  39. }
  40. }
  41. }
  42. }
  43. public static void main(String[] args)
  44. {
  45. Scanner sc = new Scanner(System.in);
  46. System.out.println("Enter no of Player:");
  47. int n = sc.nextInt();
  48. CricketPlayer cp[] = new CricketPlayer[n];
  49. for(int i = 0; i < n; i++)
  50. {
  51. cp[i] = new CricketPlayer();
  52. System.out.println("Enter"+(i+1)+" player data:");
  53. System.out.println("Enter Player Name:");
  54. String pn = sc.next();
  55. System.out.println("Enter No of Innings");
  56. int ni = sc.nextInt();
  57. System.out.println("Enter No of Times Not Out");
  58. int no = sc.nextInt();
  59. System.out.println("Enter Total Runs:");
  60. Double tr = sc.nextDouble();
  61. cp[i].setdata(pn,ni,no,tr);
  62. }
  63. System.out.println("\n\t\t\t\t**Player  Records**\n");
  64. System.out.println("Player Name \t No of Innings \t No of Times Notout \t Total Runs \t Bat Avg");
  65. CricketPlayer.avg(cp,n);
  66. for(int i=0; i < n; i++)
  67. {
  68. cp[i].display();
  69. }
  70. System.out.println("\n\t\t\t\t**Sorted Player  Records**\n");
  71. CricketPlayer.sort(cp,n);
  72. System.out.println("Player Name \t No of Innings \t No of Times Notout \t Total Runs \t Bat Avg");
  73. for(int i=0; i < n; i++)
  74. {
  75. cp[i].display();
  76. }
  77. sc.close();
  78. }
  79. }

//Execute
/*Output
Enter no of Player:
2
Enter1 player data:
Enter Player Name:
Ajay
Enter No of Innings
4
Enter No of Times Not Out
2
Enter Total Runs:
250
Enter2 player data:
Enter Player Name:
Khushal
Enter No of Innings
4
Enter No of Times Not Out
3
Enter Total Runs:
300

                                **Player  Records**

Player Name      No of Innings   No of Times Notout      Total Runs      Bat Avg
Ajay                    4               2               250.0           125.0
Khushal                 4               3               300.0           300.0

                                **Sorted Player  Records**

Player Name      No of Innings   No of Times Notout      Total Runs      Bat Avg
Khushal                 4               3               300.0           300.0
Ajay                    4               2               250.0           125.0
*/


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