Knowee
Questions
Features
Study Tools

Fill the code to override the equals method in Object class and invoke that method for 2 account objects and compare the objects. Code : public class Account{       private int accountNumber;       private String holderName;       private double balance;                    public Account(int accountNumber, String holderName, double balance) {              this.accountNumber = accountNumber;              this.holderName = holderName;              this.balance = balance;       }       //Override the equals method in the Object class       public   equals() {       //Downcast Object o to Account acc        acc = o;        if(this.accountNumber==acc.accountNumber &&                                                                                       this.holderName.equals(acc.holderName) && this.balance==acc.balance)                                 ;         else                                 ;       }}public class Main{       public static void main(String args[]) {              Account accObj1=new Account(1001,"Chandru",50000);              Account accObj2=new Account(1001,"Chandru",50000);                          //Invoke the equals method for the above two objects.              System.out.println(.());                   }}

Question

Fill the code to override the equals method in Object class and invoke that method for 2 account objects and compare the objects. Code : public class Account{       private int accountNumber;       private String holderName;       private double balance;                    public Account(int accountNumber, String holderName, double balance) {              this.accountNumber = accountNumber;              this.holderName = holderName;              this.balance = balance;       }       //Override the equals method in the Object class       public   equals() {       //Downcast Object o to Account acc        acc = o;        if(this.accountNumber==acc.accountNumber &&                                                                                       this.holderName.equals(acc.holderName) && this.balance==acc.balance)                                 ;         else                                 ;       }}public class Main{       public static void main(String args[]) {              Account accObj1=new Account(1001,"Chandru",50000);              Account accObj2=new Account(1001,"Chandru",50000);                          //Invoke the equals method for the above two objects.              System.out.println(.());                   }}

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is the corrected code:

public class Account {
    private int accountNumber;
    private String holderName;
    private double balance;

    public Account(int accountNumber, String holderName, double balance) {
        this.accountNumber = accountNumber;
        this.holderName = holderName;
        this.balance = balance;
    }

    //Override the equals method in the Object class
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Account acc = (Account) o;
        return accountNumber == acc.accountNumber &&
                Double.compare(acc.balance, balance) == 0 &&
                holderName.equals(acc.holderName);
    }
}

public class Main {
    public static void main(String args[]) {
        Account accObj1 = new Account(1001, "Chandru", 50000);
        Account accObj2 = new Account(1001, "Chandru", 50000);

        //Invoke the equals method for the above two objects.
        System.out.println(accObj1.equals(accObj2));
    }
}

Here's what I did:

  1. I added the @Override annotation to the equals method to indicate that we're overriding a method from the superclass Object.
  2. I added Object o as a parameter to the equals method.
  3. I added a check to see if the object being compared to this is null or not of the same class. If either is true, the method returns false.
  4. I downcast Object o to Account acc.
  5. I replaced the empty statements after the if and else with a return statement that returns the result of the comparison.
  6. In the main method, I replaced the empty parentheses in the println statement with accObj1.equals(accObj2).

This problem has been solved

Similar Questions

//Override the equals method in the Object class       public   equals() {       //Downcast Object o to Account acc        acc = o;        if(this.accountNumber==acc.accountNumber &&                                                                                       this.holderName.equals(acc.holderName) && this.balance==acc.balance)                                 ;         else                                 ;       }}

return false accObj1 Object o (Account) accObj2 accObj1 boolean return 1 accObj2 return 0 Object equals (Object) return true Account Fill the code to override the equals method in Object class and invoke that method for 2 account objects and compare the objects. Code : public class Account{       private int accountNumber;       private String holderName;       private double balance;                    public Account(int accountNumber, String holderName, double balance) {              this.accountNumber = accountNumber;              this.holderName = holderName;              this.balance = balance;       }       //Override the equals method in the Object class       public   equals() {       //Downcast Object o to Account acc        acc = o;        if(this.accountNumber==acc.accountNumber &&                                                                                       this.holderName.equals(acc.holderName) && this.balance==acc.balance)                                 ;         else                                 ;       }}public class Main{       public static void main(String args[]) {              Account accObj1=new Account(1001,"Chandru",50000);              Account accObj2=new Account(1001,"Chandru",50000);                          //Invoke the equals method for the above two objects.              System.out.println(.());                   }}

public class Main{       public static void main(String args[]) {              Account accObj1=new Account(1001,"Chandru",50000);              Account accObj2=new Account(1001,"Chandru",50000);                          //Invoke the equals method for the above two objects.              System.out.println(.());                   }}

Select the correct answerIn Java, the equals() method is used for:A. Comparing the memory addresses of two objects.B. Checking if two objects have the same content or state.C. Determining if two objects are of the same class.D. Overriding the default behavior of object comparison.OptionsC and DB and DOnly AOnly D

public class Account{       private int accountNumber;       private String holderName;       private double balance;                    public Account(int accountNumber, String holderName, double balance) {              this.accountNumber = accountNumber;              this.holderName = holderName;              this.balance = balance;       } } public class Main{       public static void main(String args[]) {              Account accObj1=new Account(1001,"Chandru",50000);                           System.out.println(accObj1);                    }}If the above code should give the output as “Account Number : 1001, Holder : Chandru, Balance : 50000”, which method should be overridden and that method should be written in which class.Select one:a.Override toString() method in Account classb.Override tostring() method in Account classc.Override tostring() method in Main classd.Override toString() method in Main class

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.