Apok's general Java thread

Status
Not open for further replies.
Thanks, that actually does explain it.

but I have another question: why is the parameter '54' when you call the method inside main?

Your welcome.

I just picked a random integer as a parameter to demonstrate. If in the method parameters you declare that it can take in an int (like the red writing above in the last code example) then you can give any int you want. Doesn't have to be a 54. Or it can be a variable that has a integer stored in it. Same goes for other primitive types as well (ex. int, char, double...) and other things as well. Though if you declare in the method parameters to take in a int but you try to pass a String in your main method to it, you will get an error. Hope that makes sense.
 
I've just been using constructors, mutators and accessors
This particular code won't compile:
Code:
public class AAA
{
    private String accID;
    private String name;
    private double balance;
    public Acc(String accountID, String accountName, double amount)
    {
    accID = accountID;
    name = accountName;
    balance = amount;
    }
    public double getBalance()
    {
        return balance;
    }
    
    public String getName()
    {
        return name;
    }
    public String getID()
    {
        return accID;
    }
    public boolean withdraw(double amount)
    {
        if (balance > amount)
        {
            balance -= amount;
            return true;
        }
        
        else return false;
    }
    public void deposit(double amount)
    {
        balance += amount;
    }
    
    public boolean transfer (Acc account, double amount)
    {
        if (balance > amount)
        {
            balance -= amount;
            account.deposit(amount);
            return true;
        }
        
        else return false;
    }
    public static void main(String[] args)
    {
        Acc arbitrary = new Acc("1234","Arbitrary",1234);
        Acc obtuse = new Acc("12345","Obtuse",54321);
        
        System.out.printf("\nArbitrary balance = " + arbitrary.getBalance());
        System.out.printf("\nObtuse balance = " + obtuse.getBalance());
        
        System.out.printf("\n\nWithdrawing 42 bucks from obtuse");
        obtuse.withdraw(42);
        System.out.printf("\nObtuse balance = " + obtuse.getBalance());
        
        System.out.printf("\n\nTransferring 42 bucks from Obtuse to Arbitrary");
        obtuse.transfer(arbitrary,42);
        System.out.printf("\nObtuse balance = " + obtuse.getBalance());
        System.out.printf("\nArbitrary balance = " + arbitrary.getBalance());
        
        System.out.printf("\n\ndepositing 82 bucks to obtuse");
        obtuse.deposit(84);
        System.out.printf("\nObtuse balance = " + obtuse.getBalance());
    }
}
It tells me the constructor must have a return type.
But, if I use void as the return type, it says "cannot find symol Acc" for everything that references the constructor.
 
Ok what I believe the problem is, is this.....

You created your objects of type "Acc". But you have no class that is named "Acc". That object needs the associated "Acc" class to access. Hopefully that makes sense.

Ill explain what the error message means now.

Since your constructor has a different name than the class, the compiler is thinking it is a method. So it wants you to add a return type to it. Constructors are basically methods with no return type. But constructors gotta have the same name as the class. Thus if you add a return type to it, it is making it a method not a constructor.

Hopefully that helped you and you understood that. I've just finished a class in java programming in school so im no expert.
 
Ok what I believe the problem is, is this.....

You created your objects of type "Acc". But you have no class that is named "Acc". That object needs the associated "Acc" class to access. Hopefully that makes sense.

Ill explain what the error message means now.

Since your constructor has a different name than the class, the compiler is thinking it is a method. So it wants you to add a return type to it. Constructors are basically methods with no return type. But constructors gotta have the same name as the class. Thus if you add a return type to it, it is making it a method not a constructor.

Hopefully that helped you and you understood that. I've just finished a class in java programming in school so im no expert.
Ah, so I just need to have a class for every constructor, with the same name.
 
Ah, so I just need to have a class for every constructor, with the same name.

yeah but you can have more than one constructor in a class even though both constructors are named the same as the class. The thing that differentiates the constructors is what is takes in as parameters. As long as the two constructors takes in different parameters then you can have two constructors in one class.

Also you only have one constructor in that code that you showed above. Constructors don't have a return type.
 
yeah but you can have more than one constructor in a class even though both constructors are named the same as the class. The thing that differentiates the constructors is what is takes in as parameters. As long as the two constructors takes in different parameters then you can have two constructors in one class.
Yeah, I learned about that in my Java class. I probably just missed the part about them needing to have the same name as the class.

Also you only have one constructor in that code that you showed above. Constructors don't have a return type.
Yeah, that's why I didn't get why the compiler was complaining about that.
 
Status
Not open for further replies.
Back
Top Bottom