Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: method in class cannot be applied

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default method in class cannot be applied

    Hi there, I am a total newbie. Learning java at uni. One of our questions on classes asks us to write some method statements. here is a snippet of the code:
    PersonImproved agent1 = new PersonImproved("Natasha", 99);
      System.out.println(agent1);
      agent1.setName("Sophia");
      agent1.setAge(24);
      System.out.println(agent1);
     
      PersonImproved agent2 = new PersonImproved("Boris", 40);
      System.out.println(agent1.equals(agent2));
      System.out.println(agent1.isSameName(agent2));
      /*System.out.println(agent1.isSameAge(agent2));
       agent2.setName("Sophia");
       System.out.println(agent2.isSameName(agent1));
       agent2.setAge(21);
       System.out.println(agent2.isSameAge(agent1));
     
       System.out.println(agent1.isYoungerThan(agent2));
       agent1.setAge(42);
       agent2.setAge(42);
       System.out.println(agent1.isOlderThan(agent2));
       agent2.setAge(99);
       System.out.println(agent2.isOlderThan(agent1));
       System.out.println(agent1.isYoungerThan(agent2));*/
    }}}
     
    class PersonImproved
    {private String name;
      private int age;
     
      public PersonImproved(String newName, int newAge)
      {name = newName;
        if (newAge >= 0)
          age = newAge;
        else
        {System.out.println("ERROR: Used a negative age.");
          System.exit(0);
        }
      }
     
      public String getName()
      {return name;
      }
     
      public void setName(String name)
      {
        this.name = name;
      }
     
      public int getAge()
      {return age;
      }
     
      public void setAge(int age)
      {
        this.age = age;
      }
     
     
     
      public boolean equals(String name, int age)
      {
        return (this.name == name && this.age == age);
      }
     
      public boolean isSameName(String name, int age)
      {
        return (this.name == name);
      }
     
      /*public boolean isSameAge(String name, int age)
       { 
       return (this.age == age);
       }
     
       public boolean isYoungerThan(String name, int age)
       {
       return (this.age < age);
       }
     
       public boolean isOlderThan(String name, int age)
       {
       return (this.age > age);
       }
     
       public String toString()
       {
       return name + " " + age;
       }*/
    }

    I am getting this error when compiling:

    1 error found:
    File: /Users/RichAnge/JPL/H274.java [line: 21]
    Error: /Users/RichAnge/JPL/H274.java:21: isSameName(java.lang.String,int) in PersonImproved cannot be applied to (PersonImproved)

    and I don't know what this means, can anyone inform me of what the error means so I can fix my code. thanks
    (ps: i have some of the code commented out because I am working one line at a time, i hope that is not my problem lol).


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: method in class cannot be applied

    It means the method isSameName requires a String and an int as parameters, but was given a PersonImproved object instead

    ...and welcome to the forum

  3. The Following User Says Thank You to jps For This Useful Post:

    ange (August 30th, 2013)

  4. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: method in class cannot be applied

    thanks for your reply, it make a little bit of sense to me - I'm still learning :-)

  5. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: method in class cannot be applied

    You are welcome, good luck

  6. The Following User Says Thank You to jps For This Useful Post:

    ange (August 30th, 2013)

  7. #5
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: method in class cannot be applied

    all solved, thanks to you explaining that, i tried a couple of new things and I got a green pass! love seeing the green :-)

Similar Threads

  1. **Constructor in class cannot be applied to given types;...
    By bassie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 2nd, 2012, 03:15 PM
  2. Call class method from another class
    By NewbieJavaProgrammer in forum Object Oriented Programming
    Replies: 1
    Last Post: November 21st, 2012, 06:56 AM
  3. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  4. ()Method cannot be applied to ()
    By CrimsonFlash in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 21st, 2011, 11:29 AM
  5. Accessing a method of one class in another class
    By Sai in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 23rd, 2010, 04:06 PM