You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用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 use equals() instead of ==. The == operator checks if two strings point to the same object in memory, while equals() compares the actual text content.
  • We need to handle null values for string properties to avoid NullPointerException.
  • Also, your original nbInstance variable is an instance field—this means every HalamanAlamat object has its own copy, so it won't correctly count total instances. I'll fix that to be a static variable 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

  1. Null Handling: We first check if either or both objects are null to avoid errors when accessing properties.
  2. Property Comparison:
    • For strings: Check if both are null, or if neither is null and their contents match via equals().
    • For integers (phone): Directly use == since it's a primitive type.
  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 10:07:28