5 Replies - 1458 Views - Last Post: 30 November 2012 - 05:36 AM Rate Topic: -----

#1 applemad20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-November 12

Person Class, Have A Counter To Increase On Each New Object

Posted 29 November 2012 - 03:56 AM

I have 2 .java programs which are Person.java and PersonTest.java.

On the Person class i need to add a Static Variable and a method to the class. The name of my private static variable will be personCount and this will store the number of persons that have been created by the PersonTest Class.

The variable should be incremented by an instruction in the class constructor, So when a new object is created again the personcount varaible will be incremented.

Now after this is working i need to examine how it works, So i need to add a getPersonCount method to my Person.class. This returns the value of the PersonCount to the PersonTest class.

This is my Person.java
public class Person
{
  //Private Variables
	private String foreName;
	private String surName;
	private int age;
	private Double height;
	private String gender;
	
	
	private static personCount;
	
	//ForeName Get And Set Methods
	public String getForeName()
	{
		return foreName;
	}
	
	public void setForeName(String foreName)
	{
		this.foreName = foreName;
	}
	
	//SurName Get And Set Methods
	public String getSurName()
	{
		return surName;
	}
		
	public void setSurName(String surName)
	{
		this.surName = surName;
	}

	//Age Get And Set Methods
	public int getAge()
	{
		return age;
	}
		
	public void setAge(int age)
	{
		this.age = age;
	}
	
	//height Get And Set Methods
	public Double getHeight()
	{
		return height;
	}
		
	public void setHeight(Double height)
	{
		this.height = height;
	}
	
	//SurName Get And Set Methods
	public String getGender()
	{
		return gender;
	}
			
	public void setGender(String gender)
	{
		this.gender = gender;
	}



	public Person(String foreName, String surName, int age, double height, String gender)
	{
		this.foreName = foreName;
		this.surName = surName;
		this.age = age;
		this.height = height;
		this.gender = gender;	
	}
	

	
	public static void main(String[] args) 
	{
		
	}
}//End of Person Class



Is This A Good Question/Topic? 0
  • +

Replies To: Person Class, Have A Counter To Increase On Each New Object

#2 Dworza  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 172
  • Joined: 13-February 11

Re: Person Class, Have A Counter To Increase On Each New Object

Posted 29 November 2012 - 05:33 AM

And whats the problem? Just add personCount++; (btw - variable type for personCount is missing)to your constructor...And then write getPersonCount method:
private int getPersonCount(){
   return personCount;
}


Was This Post Helpful? 0
  • +
  • -

#3 applemad20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-November 12

Re: Person Class, Have A Counter To Increase On Each New Object

Posted 29 November 2012 - 06:06 AM

What i want is when a new object is created i need this counter to increment.

When Im Printing All The Objects It Should show What Number Object They Are.

public class Person
{
  //Private Variables
	private String foreName;
	private String surName;
	private int age;
	private Double height;
	private String gender;
	
	
	static int personCount;
	
	public int getpersonCount()
	{
    return personCount;
	}	
	
	public int getpersonCount()
	{
    return personCount;
	}
	
	//ForeName Get And Set Methods
	public String getForeName()
	{
		return foreName;
	}
	
	public void setForeName(String foreName)
	{
		this.foreName = foreName;
	}
	
	//SurName Get And Set Methods
	public String getSurName()
	{
		return surName;
	}
		
	public void setSurName(String surName)
	{
		this.surName = surName;
	}

	//Age Get And Set Methods
	public int getAge()
	{
		return age;
	}
		
	public void setAge(int age)
	{
		this.age = age;
	}
	
	//height Get And Set Methods
	public Double getHeight()
	{
		return height;
	}
		
	public void setHeight(Double height)
	{
		this.height = height;
	}
	
	//SurName Get And Set Methods
	public String getGender()
	{
		return gender;
	}
			
	public void setGender(String gender)
	{
		this.gender = gender;
	}



	public Person(String foreName, String surName, int age, double height, String gender)
	{
		this.foreName = foreName;
		this.surName = surName;
		this.age = age;
		this.height = height;
		this.gender = gender;	
	}
	
	public String toString()
	{
		return "Person[forename=" + getForeName() + ", surname=" +  getSurName() +  ", age=" + getAge() + ", height=" + getHeight() +"m" + ", gender=" + getGender() +"]";
	}
	
	public String format()
	{
    return String.format("%10s %10s %10d %10.2f %10s", getForeName() , getSurName() , getAge() , getHeight() , getGender());
	}
	
	public static void main(String[] args) 
	{
		
	}
}//End of Person Class



This Is My PersonTest.class

public class TestPerson
{
	public static void main(String[] args) 
	{
		
		Person person1 = new Person("Jack","Jones",20,2.2,"Male");
		
		Person person2 = new Person("Jill","Williams",67,3.4,"Female");
		
		Person person3 = new Person("Bill","Jones",35,4.3,"Male");
		
		Person person4 = new Person("Ben","Williams",12, 1.8,"Male");
		
		Person person5 = new Person("","",0,0,"");
		

		System.out.println();
		System.out.println("Testing Person Class......");
		System.out.println();
		
		System.out.println("Displaying The Created Persons:");
		System.out.println();
		System.out.println(person1.toString());
		System.out.println(person2.toString());
		System.out.println();
		
		System.out.println("Displaying Created Person Names:");
		System.out.println();
		
		System.out.println(person1.getForeName() + " " + person1.getSurName() );
		System.out.println(person2.getForeName() + " " + person2.getSurName() );
		System.out.println(person3.getForeName() + " " + person3.getSurName() );
		System.out.println(person4.getForeName() + " " + person4.getSurName() );
		System.out.println();
		
		System.out.println("Chainging Heights:");
		System.out.println();
		
		System.out.println("Jacks Grown 1.0 Meters");
		person1.setHeight(3.2);
		System.out.println("Jack's Height Is Now " + person1.getHeight() + "m");
		System.out.println();
		
		System.out.println("Jill's Has Shrinked by 1 meter");
		person2.setHeight(2.2);
		System.out.println("Jill's Height Is Now " + person2.getHeight() + "m");
		
		System.out.println();
		System.out.println("Printing Every Created Persons Details....");
		System.out.println();
		
		System.out.println(person1.format());
		System.out.println(person2.format());
		System.out.println(person3.format());
		System.out.println(person4.format());
		
		//Setting Data For person4 object
		person4.setForeName("Sam");
		person4.setSurName("Signal");
		person4.setAge(59);
		person4.setHeight(2.3);
		person4.setGender("Male");
		
		System.out.println();
		System.out.println("Printing person4 Details....");
		System.out.println();
		System.out.println(person4.format());
		
		System.out.println();
		System.out.println();
		System.out.println("Here Are the 5 Created Person Objects....");
		System.out.println();
		
		
		System.out.println(person1.getpersonCount());
		
	}
}




Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Person Class, Have A Counter To Increase On Each New Object

Posted 29 November 2012 - 11:52 AM

Simply increment your personCount in your constructor

public Person(String foreName, String surName, int age, double height, String gender)  
     {  
         this.foreName = foreName;  
         this.surName = surName;  
         this.age = age;  
         this.height = height;  
         this.gender = gender;     
         ++personCount;
     }  


Was This Post Helpful? 0
  • +
  • -

#5 applemad20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-November 12

Re: Person Class, Have A Counter To Increase On Each New Object

Posted 30 November 2012 - 12:59 AM

View Postpbl, on 29 November 2012 - 11:52 AM, said:

Simply increment your personCount in your constructor

public Person(String foreName, String surName, int age, double height, String gender)  
     {  
         this.foreName = foreName;  
         this.surName = surName;  
         this.age = age;  
         this.height = height;  
         this.gender = gender;     
         ++personCount;
     }  



How would I print this value then in the TestPerson.java file, when I do print it it prints 5 for each person that I have created. This is what I do as a print statement

System.out.println(person1.getPersonCount());

Then the same for person 2-5. But the value for each is 5
Was This Post Helpful? 0
  • +
  • -

#6 applemad20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-November 12

Re: Person Class, Have A Counter To Increase On Each New Object

Posted 30 November 2012 - 05:36 AM

SOLVED NOW....... Thanks :bananaman:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1