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 2 of 2

Thread: logical error in output

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default logical error in output

    Hello there,

    I have wrote the below code and its working perfectly,when i tried to add a static field (noOfPerson) to show me the number of persons that is created in the main class, i get a wrong number !
    I would really appreciate it if you review my code for any error.

    public class Person {
     private int age;
     private String name;
     private static int noOfPerson = 0;
     
      public Person(String a_name, int a_age){
         setName(a_name);
         setAge(a_age);
         setNoOfPerson();}
     public Person(){
        this("None", 1);
        setNoOfPerson();
     
     }
     public Person(String a_name){
         this(a_name, 1);
         setNoOfPerson();
     }
     public Person(int a_age){
         this("None",a_age);
          setNoOfPerson();
     }
     
     
     
     public void setName(String a_name){
         name = a_name;
     }
    public String getName(){ return name;}
     
    public void setAge(int a_age){
        age = a_age;
     
    }
     public int getAge(){ return age;}
     public static void  setNoOfPerson(){
         Person.noOfPerson++;
     }
     public static int getNoOfPerson(){ return noOfPerson;}
     
     public String toString() {
       return String.format("%s is %d years old.", getName(), getAge());
     }
     
     }


    class including main method

    public class Person_Test {
        public static void main (String[] args){
            Person employ1 = new Person("Ahmad", 32);
            System.out.println(employ1.toString());
            Person employ2 = new Person("Ali");
            System.out.println(employ2.toString());
            Person employ3 = new Person(28);
            System.out.println(employ3.toString());
            Person employ4 = new Person();
            System.out.println(employ4.toString());
            System.out.println(Person.getNoOfPerson());
        }
    }

    here at the end its suppose to show me (Person.getNoOfPerson()) number 4 but im getting number 7!


    Thanks in advance for your help.
    Best Regards,


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: logical error in output

    Your constructors are calling each other and all of them increment noOfPerson. Only call setNoOfPerson from Person(String a_name, int a_age) as every other constructor calls this constructor.

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

    siyanor (May 28th, 2013)

Similar Threads

  1. i'm making a logical error here but i'm not seeing it.
    By qrts in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2011, 05:03 AM
  2. Logical error and having trouble HELP!
    By DeathStrike in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 3rd, 2011, 07:02 PM
  3. I have a logical error
    By n00b in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2011, 02:52 PM
  4. Sum of series program logical error
    By Neel0075 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 12th, 2011, 11:26 AM
  5. While (logical confusing output)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: December 20th, 2009, 01:17 AM