-1

I was just wondering if I made the instances of the class correctly. Also is there any way that I can test the instances of the class. I tried for example to do System.out.println(Toddler.getAge()) in my main method but I keep getting an error.

public class Person
{
  private String name;
  private int age;


  public Person()
  {
    name = "No name yet";
    age = 0;
  }

  public Person(String userName, int userAge)
  {
    name = userName;
    age = userAge;
  }

  public static Person createAdult(String name, int age)
  { 
    Person newPerson = new Person("An adult", 21); 
    return newPerson;
  }

  public String getName()
  {
    return name;
  }

  public int getAge()
  {
    return age;
  }

  public void setName(String first, String last)
  {
    name = first + last;
  }

  public void setName(String wholeName)
  {
    name = wholeName;
  }

  public void setAge(int userAge)
  {
    age = userAge;
  }

  public static Person createToddler(String name, int age)
  {
    Person Toddler = new Person("A toddler", 2); 
    return Toddler;
  }

  public static Person createPreschooler(String name, int age)
  {
    Person Preschooler = new Person("A preschooler", 5);
    return Preschooler;
  }

  public static Person createAdolescent(String name, int age)
  {
    Person Adolescent = new Person("An adolescent", 9);
    return Adolescent;
  }

  public static Person createTeenager(String name, int age)
  {
    Person Teenager = new Person("A teenager", 15);
    return Teenager;
  }


}
1
  • easiest way :print something in ur Person() constructor at the end.If object created properly then it will print on console.
    – Bacteria
    Jul 11, 2015 at 19:21

3 Answers 3

1

You nowhere mentioned how you were using Toddler.getAge() in main method. But as per the code of Person class, you got to use it like this:

System.out.println(Person.createToddler("Tahir", 25).getName());//for name
System.out.println(Person.createToddler("Tahir", 25).getName());//for age

This code prints the age and name correctly. Person.createToddler("Tahir", 25) returns Person class instance and then using that you can call any of non static methods.

0

In order to check whether an object is an instance of correct class you can use instanceof operator like :-

Person p = new Person();
 System.out.print(p instanceof Person) ; // will return true if p is an instance of Person

Moreover, if you are doing Toddler.getAge() (Toddler code not provided above), that might not work if getAge() is not a static method. You need to have an instance of Toddler class to call getAge() on it.

public int getAge() should be the signature to work like this new Toddler().getAge() OR

public static int getAge() should be the signature to make this Toddler.getAge() work.

0
Person aToddler = Person.createToddler();
Person aTeen = Person.createTeenager();
Person aPerson = new Person("me", 292);// using a constructor
  System.out.println(aPerson.getAge());

Notice the create toddler call in my code has no arguments? Your create toddler method should have no parameters because A. You're not using them, and B. they're unneeded for that method (expect maybe name).

That's how you can create instance variables using the code you provided. Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.