如何使用Java实现比较HalamanAlamat类不同实例对象属性值的matches静态方法
Solution for Comparing
HalamanAlamat Instances Got it, let's build that matches method you need. The goal is to check if every property of two HalamanAlamat objects is identical, returning 1 if they all match and 0 otherwise. Here's how to do it properly:
Key Notes First
- For string properties (
name,address,email), always useequals()instead of==. The==operator checks if two strings point to the same object in memory, whileequals()compares the actual text content. - We need to handle
nullvalues for string properties to avoidNullPointerException. - Also, your original
nbInstancevariable is an instance field—this means everyHalamanAlamatobject has its own copy, so it won't correctly count total instances. I'll fix that to be astaticvariable since it should track all instances created.
Complete Modified Code
class HalamanAlamat { private String name; private String address; private int phone; private String email; // Make nbInstance static to track all instances globally private static int nbInstance; HalamanAlamat(){ this.name = "null"; this.address = "null"; this.phone = 0; this.email = "null"; // No need for "this" since it's static nbInstance++; } void setName(String name){ this.name = name; } void setAddress(String address){ this.address = address; } void setPhone(int phone){ this.phone = phone; } void setEmail(String email){ this.email = email; } String getName(){ return this.name; } String getAddress(){ return this.address; } int getPhone(){ return this.phone; } String getEmail(){ return this.email; } // Fixed method name to follow Java conventions (toString instead of tostring) public void toString(){ System.out.println("Nama : " + getName()); System.out.println("Alamat : " + getAddress()); System.out.println("No.Handphone : " + getPhone()); System.out.println("Email : " + getEmail()); } } public class BukuAlamat { // The required static matches method public static int matches(HalamanAlamat halamanA1, HalamanAlamat halamanA2) { // Handle null object cases first if (halamanA1 == null && halamanA2 == null) { return 1; } if (halamanA1 == null || halamanA2 == null) { return 0; } // Compare each property, handling null strings boolean nameMatches = (halamanA1.getName() == null && halamanA2.getName() == null) || (halamanA1.getName() != null && halamanA1.getName().equals(halamanA2.getName())); boolean addressMatches = (halamanA1.getAddress() == null && halamanA2.getAddress() == null) || (halamanA1.getAddress() != null && halamanA1.getAddress().equals(halamanA2.getAddress())); boolean phoneMatches = halamanA1.getPhone() == halamanA2.getPhone(); boolean emailMatches = (halamanA1.getEmail() == null && halamanA2.getEmail() == null) || (halamanA1.getEmail() != null && halamanA1.getEmail().equals(halamanA2.getEmail())); // Return 1 if all properties match, else 0 return (nameMatches && addressMatches && phoneMatches && emailMatches) ? 1 : 0; } public static void main(String[] args) { HalamanAlamat halaman1 = new HalamanAlamat(); halaman1.setName("Rizki okto"); halaman1.setAddress("Ambarita"); halaman1.setPhone(629768328); halaman1.setEmail("rizok@gmail.com"); halaman1.toString(); // Test case 1: Identical object HalamanAlamat halaman2 = new HalamanAlamat(); halaman2.setName("Rizki okto"); halaman2.setAddress("Ambarita"); halaman2.setPhone(629768328); halaman2.setEmail("rizok@gmail.com"); System.out.println("\nMatch result (identical): " + matches(halaman1, halaman2)); // Should print 1 // Test case 2: Different phone number HalamanAlamat halaman3 = new HalamanAlamat(); halaman3.setName("Rizki okto"); halaman3.setAddress("Ambarita"); halaman3.setPhone(123456789); halaman3.setEmail("rizok@gmail.com"); System.out.println("Match result (different phone): " + matches(halaman1, halaman3)); // Should print 0 } }
How It Works
- Null Handling: We first check if either or both objects are
nullto avoid errors when accessing properties. - Property Comparison:
- For strings: Check if both are
null, or if neither isnulland their contents match viaequals(). - For integers (
phone): Directly use==since it's a primitive type.
- For strings: Check if both are
- Return Value: If all properties match, return 1; otherwise, return 0.
I also fixed a small convention issue: renamed tostring() to toString() to follow Java naming standards (capital 'T').
内容的提问来源于stack exchange,提问作者Tomoro
相关产品推荐
相关产品推荐

