settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Creating Constructors (OOP)

+8 votes

Add 3 constructors to the Person class from the last task, use constructor chaining to reuse code:

  1. The first should take no arguments and produce a person with name “No name” and age = 1.
  2. The second should accept only an integer number for the age and produce a person with name “No name” and age equal to the passed parameter.
  3. The third one should accept a string for the name and an integer for the age and should produce a person with the given name and age.

Add the following code to your main method :

public static void main(String[] args) throws Exception {
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader reader = new BufferedReader(isr);
    Class personClass = Class.forName("Person");
    Constructor emptyCtor = personClass.getDeclaredConstructor();
    Constructor ageCtor = personClass.getDeclaredConstructor(int.class);
    Constructor nameAgeCtor = personClass
            .getDeclaredConstructor(String.class, int.class);

    String name = reader.readLine();
    int age = Integer.parseInt(reader.readLine());

    Person basePerson = (Person) emptyCtor.newInstance();
    Person personWithAge = (Person) ageCtor.newInstance(age);
    Person personFull = (Person) nameAgeCtor.newInstance(name, age);

    System.out.printf("%s %s%n", basePerson.name, basePerson.age);
    System.out.printf("%s %s%n", personWithAge.name, personWithAge.age);
    System.out.printf("%s %s%n", personFull.name, personFull.age);
}

If you defined the constructors correctly, the test should pass.

asked in Java category by user ak47seo

1 Answer

+1 vote
 
Best answer

Here's the solution:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;

class Person {
    String name;
    int age;

    public Person() {//1st constructor
        this("No name", 1);
    }//1st constructor

    public Person(int age) {//2nd constructor
        this("No name", age);
    }//2nd constructor

    public Person(String name, int age) {//3rd constructor
        this.name = name;
        this.age = age;
    }
}


public class U_17_Classes {
    public static void main(String[] args) throws Exception {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(isr);
        Class personClass = Class.forName("Person");
        Constructor emptyCtor = personClass.getDeclaredConstructor();
        Constructor ageCtor = personClass.getDeclaredConstructor(int.class);
        Constructor nameAgeCtor = personClass
                .getDeclaredConstructor(String.class, int.class);

        String name = reader.readLine();
        int age = Integer.parseInt(reader.readLine());

        Person basePerson = (Person) emptyCtor.newInstance();
        Person personWithAge = (Person) ageCtor.newInstance(age);
        Person personFull = (Person) nameAgeCtor.newInstance(name, age);

        System.out.printf("%s %s%n", basePerson.name, basePerson.age);
        System.out.printf("%s %s%n", personWithAge.name, personWithAge.age);
        System.out.printf("%s %s%n", personFull.name, personFull.age);
    }
}
answered by user andrew
edited by user golearnweb
...