Incompatible return type error java

后端 未结 5 1480
礼貌的吻别
礼貌的吻别 2021-01-17 06:52
class BankAccount {
    private String firstname;
    private String lastname;
    private int ssn;
    private int accountnumber = 0;
    private double accountbala         


        
相关标签:
5条回答
  • 2021-01-17 07:12

    Every answer so far is correct and points out problems. Here is one that is not yet mentioned.

    Your equals method does not properly over-ride the parent:

    public boolean equals(BankAccount ba) {
        return this.accountbalance == ba.accountbalance;
    }
    

    The signature of equals on Object is public boolean equals(Object other) so that any objects can be compared to each other. You have changed it to only allow comparing against other BankAccounts, which violates the contract.

    Try this instead:

    public boolean equals(BankAccount ba) {
        if (ba instanceof BankAccount) {
            return this.accountbalance == ((BankAccount) ba).accountbalance;
        }
        else {
            return false;
        }
    }
    
    0 讨论(0)
  • 2021-01-17 07:19
    public void setAccountNumber(int accountnumber) {
        return accountnumber;
    }
    
    public void deposit(double amount) {
        return this.accountbalance=this.accountbalance+amount;
    }
    

    ...Are you trying to return in void-return function???

    This will be correct:

    public int setAccountNumber(int accountnumber) {
    //     ^^^
        return accountnumber;
    }
    
    public double deposit(double amount) {
    //     ^^^^^^
        return this.accountbalance=this.accountbalance+amount;
    }
    
    0 讨论(0)
  • 2021-01-17 07:28

    A void method cannot return someting. And a method which have a return type must return the type specified in method signature.

    Error 1

    For example look this method

     public void setAccountNumber(int accountnumber) {
       return accountnumber;
       }
    

    Yo cannot return from a void method.

    That should be

      public void setAccountNumber(int accountnumber) {
               this.accountnumber =accountnumber;
           }
    

    Same goes for remaining methods too.

    Error 2

    public void deposit(double amount) {
            return this.accountbalance = this.accountbalance + amount;
        }
    

    that return statement is syntactically wrong. You cannot return as it is void. That should be

      public void deposit(double amount) {
                this.accountbalance = this.accountbalance + amount;
            }
    
    0 讨论(0)
  • 2021-01-17 07:29
    public void setAccountNumber(int accountnumber) {
     return accountnumber;
     }
    

    here your return type is void and your returning int.

    0 讨论(0)
  • 2021-01-17 07:31

    Forst Error is that the return type of your meyhod is void and the method returns an int.

     public void setAccountNumber(int accountnumber) {
        return accountnumber;
     }
    

    Change it to :

     public int setAccountNumber(int accountnumber) {
        return accountnumber;
     }
    

    and the second thing is this is not a good name for a method which is used for getting the variable , change the name of method to :

     public int getAccountNumber(int accountnumber) { // why is this argument???
        return accountnumber;
     }
    

    One more thing, your method

    public void deposit(double amount) {
         return this.accountbalance = this.accountbalance + amount;
    }
    

    has the same error. It should be :

    public double deposit(double amount) { //change void to double
        return this.accountbalance + amount; //as suggested by SURESH ATTA
    }
    
    0 讨论(0)
提交回复
热议问题