Results 1 to 7 of 7
  1. #1
    Estrex is offline Member
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default Help with method class coding?

    I'm supposed to make a method class where it has methods for checking what the first name is, last name is, what the initials are, what the full name is, and how long the name is. All of this is printed out by creating a driver class which uses its methods. The name can be whatever the user inputs it as in the driver, which makes it difficult to create methods for it. This is my code for the method class so far:


    public class Name
    {
    String Goku;
    String Krillin;
    String Popo = Krillin+" "+Goku;
    public Name (String Vegeta, String Piccolo)
    {
    Goku = Vegeta;
    Krillin = Piccolo;
    }
    public String getLastName()
    {
    return Goku;
    }
    public String getFirstName()
    {
    return Krillin;
    }
    public String getFullName()
    {
    return (Popo);
    }
    public String getFirstInitials(String Krillin)
    {
    return (Krillin.charAt(0));
    }
    public String getNameLength()
    {
    return (Popo.length());
    }
    }

    Could any of you critique this and tell me where revisions are necessary? Also, there's a problem with the return Krillin.charAt(0). It says found charAt but expected java.lang.string.

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Help with method class coding?

    correct me if im wrong, but you are returning a char when the method it self returns a string.

    public String will make the compiler want to return a string, void doesnt return anything, int expects an int to be returned etc

  3. #3
    ralph is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Re: Help with method class coding?

    If the name is created via user input why on Earth would you call firstName Goku, lastName Krillin, etc?

    As then you get things like 'Goku = Vegeta', return (Popo), which makes absolutely no sense.

    I'd recommend changing your variable names to more conventional things :)

    So you'll have 'this.lastName = lastName', return firstName, etc.... It will be a great deal more sense I'm sure.

    EDIT: Also, you're getting that error as you're returning a 'String' (You ask for a string to be returned but are actually returning a char.

    Java Code:
    public String getFirstInitials(String Krillin) //Asking to return a 'String'
    {
            return (Krillin.charAt(0)); //Returning a char
    }
    Just ask to return a char instead.....
    Last edited by ralph; 12-14-2012 at 01:46 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with method class coding?

    This is a problem:
    Java Code:
    String Goku;
    String Krillin;
    String Popo = Krillin+" "+Goku;
    You must understand the difference between an object and a variable. Goku, Krillin, and Popo are all String variables not String objects, and in fact the first two variables will be null since they do not refer to any String objects. The third variable will then hold "null null". Note that if later on in your program you give Goku and Krillin Strings to refer to, this will have absolutely no effect on Popo which will still hold "null null" and will always hold this value unless explicitly changed.

  5. #5
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Help with method class coding?

    Quote Originally Posted by Estrex View Post
    Could any of you critique this and tell me where revisions are necessary?
    - Use the a good code convention when writing a code. For example in Java when we write field or variable or parameter names we start with a lowercase letter.
    - Use a meaningful name for your variable. Instead of naming your variable with Goku to store a last name information name it lastName (well, of course I know you are a fan of Dragon Ball series).
    - Add one empty line between method will make your code easier to read.

  6. #6
    Estrex is offline Member
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default Re: Help with method class coding?

    All right, so if the name is created via user input in the driver, then what do I do to start this method? Sorry, I'm new to programming and have not had that much experience with java code.

  7. #7
    Hindenbug is offline Member
    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Default Re: Help with method class coding?

    I'm supposed to make a method class where it has methods for checking what the first name is, last name is, what the initials are, what the full name is, and how long the name is. All of this is printed out by creating a driver class which uses its methods. The name can be whatever the user inputs it as in the driver, which makes it difficult to create methods for it. This is my code for the method class so far:

    Java Code:
    public class Name
    {
        String Goku;
        String Krillin;
        String Popo = Krillin+" "+Goku;  
        //this is kind of a waste of a data member, since you only use this once, you should just return 
        //'Krillin+" "+Goku'. the point of a constructor is to define data members, even if this would 
        //define Popo the value you wanted, which it would not, it should still be defined in the constructor.
    
        //the reason people use accessor methods, 'getDataMember()' is because of data hiding.  you 
        //declare your data members as private so people cannot just access them
        //like 'NameObject.Goku', which you are not doing.  all your data members should be private,
        //as you show through you code you don't want them to be accessed directly.
        //otherwise 90% of the stuff below is obselete.  though I guess you are kind of hiding them by 
        //using these crazy variable names... lol
    
        public Name (String Vegeta, String Piccolo)
        {
            Goku = Vegeta;
            Krillin = Piccolo;
        }
        public String getLastName()
        {
            return Goku;
        }
        public String getFirstName()
        {
            return Krillin;
        }
        public String getFullName()  //should be a toString method.
        {
            return (Popo);    //why do you have this wrapped up in parenthesis?..
        }
    
        public String getFirstInitials(String Krillin)
        {
            return (Krillin.charAt(0));  //this returns a char not a String
        }
        //this is the must puzzling of your methods...  this is going to return the Krillin that is in the parameter
        //instead of the data member that is defined above.. it doesn't seem that that is what you want.  you 
        //could fix it by using the keyword 'this', example 'return this.Krillin.charAt(0);', or just get rid of the 
        //parameter.  also it doesn't need to be wrapped up in parenthesis either... 
    
        public String getNameLength()
        {
            return (Popo.length());  //this returns an int that specifies the length of Popo, not the String.
        }
    }
    Could any of you critique this and tell me where revisions are necessary? Also, there's a problem with the return Krillin.charAt(0). It says found charAt but expected java.lang.string.



    As for "All right, so if the name is created via user input in the driver, then what do I do to start this method? Sorry, I'm new to programming and have not had that much experience with java code. "

    you just have to instantiate an Object via the keyword 'new' in the method that is calling it. 'ClassName classObject = new ClassName(param1, param2);'.
    Last edited by Hindenbug; 12-17-2012 at 02:23 AM.

Similar Threads

  1. Replies: 5
    Last Post: 07-12-2012, 08:44 PM
  2. Problem with Coding Java Class
    By Bentino in forum New To Java
    Replies: 8
    Last Post: 02-15-2012, 01:09 AM
  3. Help needed with Coding Java Class!
    By Bentino in forum New To Java
    Replies: 3
    Last Post: 02-15-2012, 12:38 AM
  4. Replies: 1
    Last Post: 10-17-2011, 01:00 AM
  5. Replies: 4
    Last Post: 10-25-2010, 03:49 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •