Results 1 to 4 of 4
Thread: equal() method
- 03-09-2010, 03:30 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 1
- Rep Power
- 0
equal() method
The program should read in the student number, first name and last name from user input. Every time user inputs student number it should call equals() method and check to see if this student number already exist. I don't know how can i call the method in my Test file.
Student.java
StudentTest.javaJava Code:public class Student{ private String firstName; private String lastName; private String studentNumber; private Object getFirstName; firstName,lastName,studentNumber */ Student(String fName,String lName,String sNumber) { firstName=fName; lastName=lName; studentNumber=sNumber; } public Student() { } public void setFirstName(String fName) { firstName=fName; } public String getFirstName() { return firstName; } public void setLastName(String lName) { lastName=lName; } public String getLastName() { return lastName; } public void setStudentNumber(String sNumber) { studentNumber=sNumber; } public String getStudentNumber() { return studentNumber; } public String toString() { return String.format("%-8s %-9s %-8s\n",firstName,lastName,studentNumber ); }//end method toString public boolean equals (Object o) { if (o instanceof Student) { Student s = (Student) o; if (s.getFirstName.equals(firstName)) return true; } return false; } }
Java Code:import java.util.Scanner; public class StudentTest { public static void main( String[] args) { Student[] students = new Student[2]; Scanner inputs = new Scanner(System.in); for(int i = 0 ; i < students.length; i++) { System.out.println("Please enter Student Number"); String fName = inputs.nextLine(); System.out.println("Please enter Student First Name"); String lName = inputs.nextLine(); System.out.println("Please enter Student Last Name"); String sNumber = inputs.nextLine(); students[i] = new Student(fName, lName, sNumber); } for(int i = 0 ; i < students.length; i++) { System.out.print(students[i].toString()); } } }
- 03-09-2010, 01:25 PM #2
paah, why do you assign the student number to fName and the first name to lName and the Last Name to sNumber?
if you read only two students then assign the first student to s1 and the second to s2 and declare your equals method with
public static boolean equals (Student s1, Student s2)
so you can call it with Student.equals (s1, s2)
and the check for equals with
if (s1.getStudentNumber().equals(s2.getStudentNumber( )))
but this is a bad implementation, because this code will not work for more then two students. so, i suggest you use a more generic collection than arrays and as already mentioned a better code for more students. a more generic solution could pass a reference to the collection and the new student to your equals-method and then inside the method iterate through the collection to find out if the number already exists. good luck.
- 03-09-2010, 04:24 PM #3
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Your Student class looks good, but in your testing class, I would also suggest switching from an array to a more generic collection, possibly a List.
First plan out what you have to do.....
1) Read in a Student
-----a) Construct a Student class containing the read in data.
2) See if Student was already entered
-----b) Go through the List of Unique Students already entered. Compare each one to the one that was just entered using the equals() method.
-----c) If it wasn't already entered, Add it to the Unique Students List.
3) Loop back to Step 1.
To answer your main question though, you can call the .equals() method by calling it on the instances of the Students you have created. Example...
Java Code:Student s1 = new Student("Cal", "Robot", "20030903"); Student s2 = new Student("Tec", "Man", "20948411"); if (s1.equals(s2)){ System.out.println("Same"); }
- 03-09-2010, 05:57 PM #4
OK. First of all, Object specifies an equals(Object other) method. All it does is look at the internal JVM references to see if they are the same. It is the same as saying "this == other".
Second, the "right" way to do this is to override the equals() method in your Student class. I assume this is homework, and your instructor expects you to do it this way. Note that equals() is NOT a static method.
Here is a standard implementation:
Java Code:@Override public boolean equals(Object other) { // Same object if (this == other) { return true; } // If other is null, instanceof returns false if (!(other instanceof Student)) { return false; } // Compare this to other using student number, return true or false you have to do this yourself }
Similar Threads
-
ComparisonFailure on equal(to the eye) strings
By staffan in forum New To JavaReplies: 2Last Post: 03-12-2009, 02:57 PM -
Problem using equal() method
By ookie833 in forum New To JavaReplies: 3Last Post: 11-18-2008, 05:19 AM -
Entering a while loop with a not equal to string
By bri1547 in forum New To JavaReplies: 9Last Post: 07-09-2008, 07:10 AM -
checking if there are equal numbers
By nalinda in forum New To JavaReplies: 1Last Post: 11-18-2007, 06:21 AM -
checking if there are equal numbers
By nalinda in forum New To JavaReplies: 0Last Post: 11-18-2007, 02:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks