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(.()); }}
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:
- I added the
@Overrideannotation to theequalsmethod to indicate that we're overriding a method from the superclassObject. - I added
Object oas a parameter to theequalsmethod. - I added a check to see if the object being compared to
thisisnullor not of the same class. If either is true, the method returnsfalse. - I downcast
Object otoAccount acc. - I replaced the empty statements after the
ifandelsewith a return statement that returns the result of the comparison. - In the
mainmethod, I replaced the empty parentheses in theprintlnstatement withaccObj1.equals(accObj2).
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
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.