Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Apok's general Java thread
Closed Thread
Old 04-03-2008, 01:49 PM   #21 (permalink)
countrydude's Avatar
 
Junior Techie

Join Date: Jan 2008

Location: Alberta, Canada

Posts: 65

countrydude is on a distinguished road

Send a message via MSN to countrydude
Default Re: Apok's general Java thread

Quote:
Originally Posted by Apokalipse View Post
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.
__________________
My Personal Site CraigSite.ca
My current specs
-Power Color HD 4850 512MB
-MSI K8N platinum SLI Mobo
-2x1GB Generic DDR RAM
-250GB, 1TB SATA HDD Seagate
-Athlon 64 X2 4200+ Socket 939 oc'ed to 2.8Ghz
countrydude is offline  
Old 04-17-2008, 12:31 AM   #22 (permalink)
Apokalipse's Avatar
 

Join Date: Jun 2003

Location: Melbourne, Australia

Posts: 13,739

Apokalipse has a spectacular aura aboutApokalipse has a spectacular aura about

Default Re: Apok's general Java thread

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.
__________________

1 + 1 = 3 if you define 3 as a result of 1 + 1
Apokalipse is offline  
Old 04-17-2008, 03:33 AM   #23 (permalink)
countrydude's Avatar
 
Junior Techie

Join Date: Jan 2008

Location: Alberta, Canada

Posts: 65

countrydude is on a distinguished road

Send a message via MSN to countrydude
Default Re: Apok's general Java thread

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.
__________________
My Personal Site CraigSite.ca
My current specs
-Power Color HD 4850 512MB
-MSI K8N platinum SLI Mobo
-2x1GB Generic DDR RAM
-250GB, 1TB SATA HDD Seagate
-Athlon 64 X2 4200+ Socket 939 oc'ed to 2.8Ghz
countrydude is offline  
Old 04-17-2008, 07:03 AM   #24 (permalink)
Apokalipse's Avatar
 

Join Date: Jun 2003

Location: Melbourne, Australia

Posts: 13,739

Apokalipse has a spectacular aura aboutApokalipse has a spectacular aura about

Default Re: Apok's general Java thread

Quote:
Originally Posted by countrydude View Post
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.
__________________

1 + 1 = 3 if you define 3 as a result of 1 + 1
Apokalipse is offline  
Old 04-17-2008, 03:03 PM   #25 (permalink)
countrydude's Avatar
 
Junior Techie

Join Date: Jan 2008

Location: Alberta, Canada

Posts: 65

countrydude is on a distinguished road

Send a message via MSN to countrydude
Default Re: Apok's general Java thread

Quote:
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.
__________________
My Personal Site CraigSite.ca
My current specs
-Power Color HD 4850 512MB
-MSI K8N platinum SLI Mobo
-2x1GB Generic DDR RAM
-250GB, 1TB SATA HDD Seagate
-Athlon 64 X2 4200+ Socket 939 oc'ed to 2.8Ghz
countrydude is offline  
Old 04-18-2008, 01:07 AM   #26 (permalink)
Apokalipse's Avatar
 

Join Date: Jun 2003

Location: Melbourne, Australia

Posts: 13,739

Apokalipse has a spectacular aura aboutApokalipse has a spectacular aura about

Default Re: Apok's general Java thread

Quote:
Originally Posted by countrydude View Post
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.

Quote:
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.
__________________

1 + 1 = 3 if you define 3 as a result of 1 + 1
Apokalipse is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Potentially the longest thread in history... Dave Off Topic Discussion 15121 Today 08:43 AM
Yak thread? nOcLuE98 Site Feedback & Suggestions 17 02-18-2008 01:45 AM
(Java) Problem creating and passing an Integer object? Toshiro Programming Discussions 1 10-17-2007 08:00 PM
Dangerous Java Flaw Threatens Virtually Everything Osiris Virus - Spyware Protection / Detection 1 07-13-2007 04:44 PM
Java Game Yek Programming Discussions 5 06-16-2007 04:29 AM