011-001-010
Setting Initial Values with a Constructor
Easy
Problem Description
Setting Initial Values with a constructor
In this problem, you will create a program that defines a constructor in a Student class to initialize name and age fields from standard input, then calls introduce() on each Student object and displays the result to standard output.
Learning Objective: Understand the basic definition and usage of constructors
Overview
Define a constructor in a Student class to initialize name and age fields, reading values from standard input.
Specifications
- Define
String nameandint agefields inStudentclass - Constructor receives name and age to initialize fields
introduce()method printsI am <name>, age <age>.- In main, read the number of students N, then read N lines of name and age, create Student objects, and call introduce()
Input Format
N
name1 age1
name2 age2
...
nameN ageN
- Line 1: number of students N
- Next N lines: name (alphabetic) and age (integer), space-separated
Output Format
I am name1, age age1.
I am name2, age age2.
...
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up