Last Updated: 22 Feb, 2021

Print Name and age

Easy
Asked in company
Google inc

Problem statement

Create a class named Person with a string variable 'name' and an integer variable 'age,' such that these variables are not accessible outside the class and implement a way to initialize the variables and print the variables.

Functions:
1.setValue- that sets the variables value.
2.getValue- that prints the variables value.
Input Format:
The first line of input contains a single string Name, representing the name of the person. 

The second line of input contains a single integer Age, representing the age of the person.
Output Format:
The only line of output prints the name and age by sticking to the sample input format. Mind that the output string won't have preceding or trailing spaces.

Approaches

01 Approach

  • First, create a class named Person with a string variable 'name' and an integer variable 'age,' as private access specifier, and then as public access specifier create a setValue method that sets the variable's value.
  • After that create a getValue method that prints the variable's value.
  • Finally, In the main define two variables one as a string and one as the age that takes name and age as input.
  • Then, create an object of Person class, set the values using the setValue method, and print the values using the getValue method.