Write a class using the following specifications and data types, access modifier
ID: 3666385 • Letter: W
Question
Write a class using the following specifications and data types, access modifiers and return types as appropriate. Name the class Person instance variables are name of data type String and ago of data type int default constructor using the defaults of "" for String and 0 for int. formal overloaded constructor accepting the formal parameters nowNamo and ncwAgo. and initializing the instance variables Method named printPerson which only prints the following 2 lines using xxx for the instance variables value. This person's name is: xxx This person s age is: xxxExplanation / Answer
/**
* @author Srininivas Palli
*
*/
public class Person {
String name;
int age;
/**
* default constructor
*/
public Person() {
// TODO Auto-generated constructor stub
name = "";
age = 0;
}
/**
* parameterized constructor
*
* @param newName
* @param newAge
*/
public Person(String newName, int newAge) {
// TODO Auto-generated constructor stub
name = newName;
age = newAge;
}
/**
* method to print the name and age
*/
public void printPerson() {
System.out.println("This persons's name is: " + name);
System.out.println("This persons's age is: " + age);
}
/**
* main to test
*
* @param args
*/
public static void main(String[] args) {
Person person = new Person("Srinivas ", 32);
person.printPerson();
}
}
OUTPUT:
This persons's name is: Srinivas
This persons's age is: 32
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.