AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-OOPs: Using Array and method overloading in one program in java?

using array and method overloading write a program of five students details like Names,Id´s,mobile number´s and Email-id´s for that students. And while executing if call student name or Id or mobile number or Email. I should get the total details of that particular student.if i call any detail among those details i should get the total details.


Could you plz forward the code.

corejava x 336
OOPs x 49
Posted On : 2016-09-08 19:01:06.0
profile MOHAMMAD SALEEM BASHA - anyforum.in MOHAMMAD SALEEM BASHA
266150
up-rate
2
down-rate

Answers


Let´s consider a call Student as below:

Student.java:
--------------------------------------------------
public class Student {
private Integer id;
private String name;
private String phone;
private String Email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email;
}

}


SearchStudent.java:
-----------------------------------------------------------
import java.util.Scanner;

public class SearchStudent {
public static void main(String[] args) {
Student students[]=new Student[4];
students=setStudents(students);
System.out.println("Enter ID or name or email or phone");
Scanner input=new Scanner(System.in);
String key=input.nextLine();
Student student=search(key, students);
if(null!=student){
System.out.println(student.getId()+" "+student.getName()+" "+student.getPhone()+" "+student.getEmail());
}else{
System.out.println("No Records Found!!");
}
}

public static Student search(String key,Student[] students){
Student student1=null;
for(Student student:students){
if(student.getId().toString().equalsIgnoreCase(key)){
return student;
}else if(student.getName().equalsIgnoreCase(key)){
return student;
}
else if(student.getPhone().equalsIgnoreCase(key)){
return student;
}
else if(student.getEmail().equalsIgnoreCase(key)){
return student;
}
}
return student1;
}

public static Student[] setStudents(Student[] students){
Student student1=new Student();
student1.setId(1);
student1.setName("AAA");
student1.setPhone("1111111111");
student1.setEmail("aaa@anyforum");

Student student2=new Student();
student2.setId(2);
student2.setName("BBB");
student2.setPhone("2222222222");
student2.setEmail("bbb@anyforum");

Student student3=new Student();
student3.setId(3);
student3.setName("CCC");
student3.setPhone("3333333333");
student3.setEmail("ccc@anyforum");

Student student4=new Student();
student4.setId(4);
student4.setName("DDD");
student4.setPhone("4444444444");
student4.setEmail("dddd@anyforum");

students[0]=student1;
students[1]=student2;
students[2]=student3;
students[3]=student4;

return students;
}
}

Note:
---------------------------
There is no need to use method overloading. Still if you want to use then you can make one more search method with Integer and Student[] parameters and removed id comparison from above search method and perform id comparison in overloaded search method and change the main method code like below:

String key=input.nextLine();
Student student=null;
try{
student=search(Integer.parseInt(key), students);
}catch(Exception e){
//means input is not an integer
student=search(key, students);
}

if(null!=student){
System.out.println(student.getId()+" "+student.getName()+" "+student.getPhone()+" "+student.getEmail());
}else{
System.out.println("No Records Found!!");
}

Posted On : 2016-09-09 17:07:34
Satisfied : 1 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523185319357
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in