{"id":66,"date":"2016-01-10T00:05:28","date_gmt":"2016-01-10T00:05:28","guid":{"rendered":"http:\/\/kaizen-koka.com\/?p=66"},"modified":"2016-01-12T02:03:24","modified_gmt":"2016-01-12T02:03:24","slug":"core-java-assignment-5","status":"publish","type":"post","link":"https:\/\/kaizen-koka.com\/?p=66","title":{"rendered":"Core Java Assignment 5"},"content":{"rendered":"<p>\n\t1) I have two classes Employee and Department as shown below.\n<\/p>\n<pre class=\"brush:java;\">\r\nclass Employee {\r\n\tint id; \r\n\tString name;\r\n\tDepartment dept; \r\n\tdouble salary; \r\n   \/\/setter\/getter methods \r\n   \/\/override equals method \r\n   \/\/override toString\r\n   \/\/override hasCode\r\n   } \r\nclass Department { \r\n\tint deptId; \r\n\tString deptName; \r\n\tString location; \r\n\t\/\/setter\/getter methods\r\n\t\/\/override equals method\r\n\t\/\/override equals method \r\n\t\/\/override toString \r\n}<\/pre>\n<p>\n\t&#8211;&gt; for me, two employee objects are same when empId, empName and his Department are same. So, write a program to accept number of employee and employee details through console and filter the duplicate employees and display the result.\n<\/p>\n<p>\n\t<strong>Solution<\/strong>\n<\/p>\n<pre class=\"brush:java;\">\r\npackage com.bellinfo.batch3.corejava.day8;\r\n\r\nimport java.util.Scanner;\r\n\r\npublic class EmployeeDemoExample1 {\r\n\tScanner scan;\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tEmployeeDemoExample1 ede =new EmployeeDemoExample1();\r\n\t\tStudentDetails[] uniqueSDArray = ede.recieveInputsFromConsole();\r\n\t\tede.displayResults(uniqueSDArray);\r\n\t}\r\n\r\n\t\/**\r\n\t * method to receive inputs from console\r\n\t * @return\r\n\t *\/\r\n\tStudentDetails[] recieveInputsFromConsole(){\r\n\t\tscan = new Scanner(System.in);\r\n\t\tSystem.out.println(&quot;enter student count:&quot;);\r\n\t\tint studentCount = scan.nextInt();\r\n\t\tStudentDetails[] originalStudArray = new StudentDetails[studentCount];\r\n\t\tStudentDetails[] uniqueStudArray = new StudentDetails[studentCount];\r\n\t\tSystem.out.println(&quot;&quot;);\r\n\t\tfor(int i=0;i&lt;=studentCount-1;i++){\r\n\t\t\tSystem.out.println(&quot;Student Details&quot;+i+&quot; :&quot;);\r\n\t\t\tSystem.out.println(&quot;Student Id:&quot;);\r\n\t\t\tint sId =scan.nextInt();\r\n\t\t\tSystem.out.println(&quot;Student Name:&quot;);\r\n\t\t\tString sName = scan.next();\r\n\t\t\tSystem.out.println(&quot;Course Id:&quot;);\r\n\t\t\tint cId = scan.nextInt();\r\n\t\t\tSystem.out.println(&quot;Course Name:&quot;);\r\n\t\t\tString cName = scan.next();\r\n\t\t\tSystem.out.println(&quot;Course Duration:&quot;);\r\n\t\t\tint cDuration = scan.nextInt();\r\n\t\t\t\r\n\t\t\tStudentDetails sd =populateStudentDetails(sId, sName, cId, cName, cDuration);\r\n\t\t\toriginalStudArray[i] =sd;\r\n\t\t}\r\n\t\tuniqueStudArray = eliminateDuplicateStudentRecords(originalStudArray);\r\n\t\treturn uniqueStudArray;\r\n\t}\r\n\t\r\n\t\/***\r\n\t * Method used to process inputs and populate the object required.\r\n\t * @param sId\r\n\t * @param sName\r\n\t * @param cId\r\n\t * @param cName\r\n\t * @param cDuration\r\n\t * @return\r\n\t *\/\r\n\tStudentDetails populateStudentDetails(int sId, String sName, int cId, String cName, int cDuration){\r\n\t\tCourse c = new Course();\r\n\t\tc.setCourseId(cId);\r\n\t\tc.setCourseName(cName);\r\n\t\tc.setCourseDuration(cDuration);\r\n\t\t\r\n\t\tStudentDetails sd = new StudentDetails();\r\n\t\tsd.setStudentId(sId);\r\n\t\tsd.setStudentName(sName);\r\n\t\tsd.setCourse(c);\r\n\t\t\r\n\t\treturn sd;\r\n\t\t\r\n\t}\r\n\t\r\n\t\/**\r\n\t * Logic to eliminate the duplicates\r\n\t * @param orignialStudArray\r\n\t * @return\r\n\t *\/\r\n\tStudentDetails[] eliminateDuplicateStudentRecords(StudentDetails[] orignialStudArray){\r\n\t\tStudentDetails[] filteredArray = new StudentDetails[orignialStudArray.length];\r\n\t\tfilteredArray = orignialStudArray.clone();\r\n\t\tfor(int i=0; i&lt;=orignialStudArray.length-1; i++){\r\n\t\t\tStudentDetails sd1 = orignialStudArray[i];\r\n\t\t\tfor(int j=i+1,y=0;j&lt;=orignialStudArray.length-1;j++){\r\n\t\t\t\tStudentDetails sd2 = orignialStudArray[j];\r\n\t\t\t\tif(sd1.equals(sd2)){\r\n\t\t\t\t\tfilteredArray[j] =null;\r\n\t\t\t\t}\r\n\t\t\t }\r\n\t\t}\r\n\t\t\r\n\t\t\r\n\t\treturn filteredArray;\r\n\t\t\r\n\t}\r\n\t\r\n\t\/**\r\n\t * This method used for displaying the results after filtering the results.\r\n\t * @param array\r\n\t *\/\r\n\tvoid displayResults(StudentDetails[] array){\r\n\t\tSystem.out.println(&quot;****************************************&quot;);\r\n\t\tSystem.out.println(&quot;********Unique Student Records**********&quot;);\r\n\t\tSystem.out.println(&quot;****************************************&quot;);\r\n\t\tSystem.out.println(&quot;Id&quot;+&quot;     &quot;+&quot;Name&quot;+&quot;     &quot;+&quot;CId&quot;+&quot;     &quot;+&quot;CName&quot;+&quot;     &quot;+&quot;CDuration&quot;);\r\n\t\tfor(int i=0;i&lt;=array.length-1;i++){\r\n\t\t\tif(array[i]!=null){\r\n\t\t\t\tStudentDetails sd = array[i];\r\n\t\t\t\tSystem.out.println(sd.toString());\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n\r\n\r\n\r\nclass StudentDetails  {\r\n\tprivate int studentId;\r\n\tprivate String studentName;\r\n\tprivate Course course;\r\n\t\r\n\t\r\n\t\r\n\tpublic int getStudentId() {\r\n\t\treturn studentId;\r\n\t}\r\n\r\n\tpublic void setStudentId(int studentId) {\r\n\t\tthis.studentId = studentId;\r\n\t}\r\n\r\n\tpublic String getStudentName() {\r\n\t\treturn studentName;\r\n\t}\r\n\r\n\tpublic void setStudentName(String studentName) {\r\n\t\tthis.studentName = studentName;\r\n\t}\r\n\r\n\tpublic Course getCourse() {\r\n\t\treturn course;\r\n\t}\r\n\r\n\tpublic void setCourse(Course course) {\r\n\t\tthis.course = course;\r\n\t}\r\n\r\n\tpublic boolean equals(Object otherStudent){\r\n\t\tif(otherStudent ==null){\r\n\t\t\treturn false;\r\n\t\t}\r\n\t\tif(otherStudent instanceof StudentDetails){\r\n\t\t   StudentDetails sdOtherObject = (StudentDetails)otherStudent;\r\n\t\t   if(this.studentId ==sdOtherObject.studentId &amp;&amp; this.studentName.equals(sdOtherObject.studentName)\r\n\t\t\t   &amp;&amp; this.course.equals(sdOtherObject.course)){\r\n\t\t\t   return true;\r\n\t\t   }\r\n\t\t\t\r\n\t\t}\r\n\t\treturn false;\r\n\t\t\r\n\t}\r\n\t\r\n\tpublic int hashCode(){\r\n\t\treturn 31*studentId*studentName.hashCode()*course.hashCode();\r\n\t\t\r\n\t}\r\n\tpublic String toString(){\r\n\t\treturn studentId+&quot;     &quot;+studentName+&quot;     &quot;+ course;\r\n\t}\r\n\t\r\n}\r\nclass Course{\r\n\tprivate int courseId;\r\n\tprivate String courseName;\r\n\tprivate int courseDuration;\r\n\t\r\n\tpublic int getCourseId() {\r\n\t\treturn courseId;\r\n\t}\r\n\tpublic void setCourseId(int courseId) {\r\n\t\tthis.courseId = courseId;\r\n\t}\r\n\tpublic String getCourseName() {\r\n\t\treturn courseName;\r\n\t}\r\n\tpublic void setCourseName(String courseName) {\r\n\t\tthis.courseName = courseName;\r\n\t}\r\n\tpublic int getCourseDuration() {\r\n\t\treturn courseDuration;\r\n\t}\r\n\tpublic void setCourseDuration(int courseDuration) {\r\n\t\tthis.courseDuration = courseDuration;\r\n\t}\r\n\t\r\n\tpublic boolean equals(Object otherCourse){\r\n\t\t if(otherCourse ==null){\r\n\t\t\t return false;\r\n\t\t }\r\n\t\t if(otherCourse instanceof Course){\r\n\t\t\t Course otherObj = (Course) otherCourse;\r\n\t\t\t if(this.courseId==otherObj.courseId &amp;&amp; this.courseName.equals(otherObj.courseName)\r\n\t\t\t\t\t &amp;&amp; this.courseDuration==this.getCourseDuration()){\r\n\t\t\t\t return true;\r\n\t\t\t\t \r\n\t\t\t }\r\n\t\t }\r\n\t\t return false;\r\n\t}\r\n\t\r\n\tpublic int hashCode(){\r\n\t\treturn 31*courseId*courseDuration*courseName.hashCode();\r\n\t\t\r\n\t}\r\n\tpublic String toString(){\r\n\t\treturn courseId+&quot;     &quot;+courseName+&quot;     &quot;+courseDuration;\r\n\t}\r\n\t\r\n\t\r\n}<\/pre>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t<strong>ouput \/ console:<\/strong>\n<\/p>\n<pre class=\"brush:bash;\">\r\n\r\n****************************************\r\n********Unique Student Records**********\r\n****************************************\r\nId     Name     CId     CName     CDuration\r\n1     Siva     121     Java     3\r\n2     Teju     121     .net     3<\/pre>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1) I have two classes Employee and Department as shown below. class Employee { int id; String name; Department dept; double salary; \/\/setter\/getter methods \/\/override equals method \/\/override toString \/\/override [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[8,3,2],"tags":[],"class_list":["post-66","post","type-post","status-publish","format-standard","hentry","category-assignments","category-java","category-technology"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p70lnf-14","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts\/66","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=66"}],"version-history":[{"count":4,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts\/66\/revisions"}],"predecessor-version":[{"id":70,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts\/66\/revisions\/70"}],"wp:attachment":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=66"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=66"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=66"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}