Print Name and age

Easy
0/40
117 upvotes
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.
Detailed explanation ( Input/output format, Notes, Images )
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.
Sample Input 1:
Afzal
67
Sample Output 1:
The name of the person is Afzal and the age is 67.
Sample Input 2:
Ali
30
Sample Output 2:
The name of the person is Ali and the age is 30.
Explanation of Sample Input 1:
The input name is Ali and the input age is 30 which is printed in the specified format.
Approaches (1)
Intuitive 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.
Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
Print Name and age
Full screen
Console