Results 1 to 6 of 6
  1. #1
    Turo is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default A newbie to Java needing help on coding error

    Any help is greatly appreciated.

    This is the error I am receiving:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The method Person(String, String, int) is undefined for the type PersonTest
    The method getFirstName() is undefined for the type PersonTest
    The method getLastName() is undefined for the type PersonTest
    The method getAge() is undefined for the type PersonTest
    setAge cannot be resolved or is not a field

    The 2 Code source files are listed below:

    Code
    Java Code:
    // Person.java
    // Creates and manipulates a person with a first name, last name and age
    
    public class Person
    {
       private String firstName;
       private String lastName;
       private int age;
    
       public Person( String first, String last, int years )
       {
          firstName = first;
          lastName = last;
    
          if ( years < 0 )
             age = years;
       } // end Person constructor
    
       public String getFirstName( String FirstName )
       {
          return firstName;
       } // end method getFirstName
    
       public void setFirstName( String first )
       {
          firstName = first;
       } // end method setFirstName
    
       public String getLastName()
       {
          return lastName;
       } // end method getLastName
    
       public void setLastName( String last )
       {
          lastName = last;
       } // end method setLastName
    
       public int getAge()
       {
          return age;
       } // end method getAge
    
       public void setAge( int years )
       {
          if ( years > 0 )
             age = years;
       } // end method setAge
    } // end class Person
    Code 2
    Java Code:
    // PersonTest.java
    // Test application for the Person class
    
    public class PersonTest
    {
       public static void main( String args[] )
       {
          Person person = Person( "John", "Smith", 19 );
    
          System.out.printf( "Created %s %s, age %d\n", 
             getFirstName(), getLastName(), getAge() );
    
          person.setAge = person.getAge() + 1;
          System.out.printf( "Happy Birthday to %s %s\n", 
             person.getFirstName(), person.getLastName() );
       } // end main
    } // end class PersonTest
    Last edited by pbrockway2; 08-28-2011 at 04:14 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Hi, welcome to the forums.

    When you post code, use the "code" tags. Put [code] at the start of the code and [/code] at the end: that way the code is readable when it appears here. There is also a # button in the pane where you compose your post that will add the tags around selected text.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    There are a couple of little errors:

    Java Code:
    Person person = Person( "John", "Smith", 19 );
    That is not how you create a new Person instance: something is missing.

    -----

    Java Code:
    System.out.printf( "Created %s %s, age %d\n", 
             getFirstName(), getLastName(), getAge() );
    Here you are trying to print a person's name and age: but you don't say what person's details you want! Compare it to how you (correctly) print their name in the birthday greeting.

  4. #4
    Turo is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default

    Thank you for the help. I was able to correct the problems you mentioned. I am kind stumped though on the next errors. If I have it listed in the same format as the getLastName. Why would I be getting an error like this?

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The method getFirstName(String) in the type Person is not applicable for the arguments ()
    getAge cannot be resolved or is not a field
    The method getFirstName(String) in the type Person is not applicable for the arguments ()

    at PersonTest.main(PersonTest.java:11)

    Java Code:
    // PersonTest.java
    // Test application for the Person class
    
    public class PersonTest
    {
       public static void main( String args[] )
       {
          Person person = new Person( "John", "Smith", 19 );
    
          System.out.printf( "Created %s %s, age %d\n", 
             person.getFirstName(), person.getLastName(), person.getAge() );
    
          person.getAge = person.getAge() + 1;
          System.out.printf( "Happy Birthday to %s %s\n", 
             person.getFirstName(), person.getLastName() );
       } // end main
    } // end class PersonTest

  5. #5
    Turo is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default

    Ok I was able to correct all the errors except one. Any help is appreciated.

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    getAge cannot be resolved or is not a field

    Java Code:
    person.getAge = person.getAge() + 1;

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    "person.getAge()" is fine: it will return the age of person. And "+1" is fine: it just does some arithmetic on the person's age.

    What goes wrong is "person.getAge=..." because this is not how you set a person's age. You have a method to do that, and you should use that method.

Similar Threads

  1. Java installation error needing soLution!!!
    By ese in forum New To Java
    Replies: 26
    Last Post: 07-31-2011, 08:29 PM
  2. Error in coding
    By bamagirl31 in forum New To Java
    Replies: 3
    Last Post: 05-11-2011, 07:31 PM
  3. Coding newbie, please help
    By skuzzie in forum New To Java
    Replies: 12
    Last Post: 10-11-2010, 04:11 AM
  4. Replies: 4
    Last Post: 06-07-2009, 01:59 AM
  5. Error in my coding
    By one198 in forum New To Java
    Replies: 2
    Last Post: 10-13-2007, 06:07 AM

Posting Permissions

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