Hi!, could you help me with these questions? any hint,advice,solution would be g
ID: 3554110 • Letter: H
Question
Hi!,
could you help me with these questions?
any hint,advice,solution would be great and thank you for your help!.
Complete and fully test the class Person include the following additional methods.
The class should also include a main method that test the correctness of the program.
getName-returns the name of the person as a string.
getAge-returns the age of the person
setName(first,last)-sets the name of the person,given a first and last name as strings.
setAge(age)-sets he age of the person
createToddler-a static method that returns a special instance of the class to represent a toddler.The instance has the name "a toddler" and the age 2.
createPreschooler-a static method that returns a special instance of the class to represent a preschooler.The instance has the name "a preschooler" and the age 5
createAdolescent-a static method that returns a special instance of the class to represent an adolescent.The instance has the name "an adolescent" and the age 9
createTeenager-a static method that returns a special instance of the class to represen a teenager.The instance has the name "a teenager" and the age 15
This is what i came up with so far.
public class Person
{
private String name;
private int age;
private String lastName;
public void setName(String newName)
{
name = newName;
}
public void setName(String mName,String mlastName)
{
name = mName;
lastName = mlastName;
}
public void setAge(int newAge)
{
if(newAge >=0)
age = newAge;
else
{
System.out.println("ERROR: Age is" + "negative");
System.exit(0);
}
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public static void createToddler(String todName, int todAge)
{
????
}
public static void main(String[] args)
{
Person person1=new Person();
Person person2=new Person();
Person person3=new Person();
person1.setName("Jack");
person1.setAge(20);
System.out.println("person1's name is" + " " + person1.getName());
System.out.println("person1's age is" + " " + person1.getAge());
person2.setName("Eddie","Murphy");
System.out.println("person2's name is" + " " + person2.name + " " + person2.lastName);
}
}
Explanation / Answer
Hi,
Please find solution for your question. I have also pasted the result for a sample run. Please do rate me. Thanks.
public class Person{
private String firstName;
private int age;
private String lastName;
public void setName(String newName){
firstName = newName;
}
public void setName(String mName,String mlastName){
firstName = mName;
lastName = mlastName;
}
public void setAge(int newAge){
if(newAge >=0)
age = newAge;
else{
System.out.println("ERROR: Age is" + "negative");
System.exit(0);
}
}
public String getName(){
return firstName;
}
public int getAge(){
return age;
}
public static Person createToddler(String todName, int todAge){
Person toddler = new Person();
toddler.setName("a toddler");
toddler.setAge(2);
return toddler;
}
public static Person createPreschooler(String todName, int todAge){
Person preSchooler = new Person();
preSchooler.setName("a preschooler");
preSchooler.setAge(5);
return preSchooler;
}
public static Person createAdolescent(String todName, int todAge){
Person adolescent = new Person();
adolescent.setName("an adolescent");
adolescent.setAge(9);
return adolescent;
}
public static Person createTeenager(String todName, int todAge){
Person teenager = new Person();
teenager.setName("a teenager");
teenager.setAge(15);
return teenager;
}
public static void main(String[] args) {
System.out.println("Creating Toddler");
Person toddler = createToddler("a toddler", 2);
System.out.println("Toddler Created");
System.out.println("Name is " + toddler.getName() + " and age is " + toddler.getAge());
System.out.println("**************************************************************");
System.out.println("Creating PreSchooler");
Person preSchooler = createPreschooler("a preschooler",5);
System.out.println("PreSchooler Created");
System.out.println("Name is " + preSchooler.getName() + " and age is " + preSchooler.getAge());
System.out.println("**************************************************************");
System.out.println("Creating Adolescent");
Person adolescent = createAdolescent("an adolescent",9);
System.out.println("Adolescent Created");
System.out.println("Name is " + adolescent.getName() + " and age is " + adolescent.getAge());
System.out.println("**************************************************************");
System.out.println("Creating Teenager");
Person teenager = createTeenager("a teenager",15);
System.out.println("Teenager Created");
System.out.println("Name is " + teenager.getName() + " and age is " + teenager.getAge());
System.out.println("**************************************************************");
}
}
Result :
Creating Toddler
Toddler Created
Name is a toddler and age is 2
**************************************************************
Creating PreSchooler
PreSchooler Created
Name is a preschooler and age is 5
**************************************************************
Creating Adolescent
Adolescent Created
Name is an adolescent and age is 9
**************************************************************
Creating Teenager
Teenager Created
Name is a teenager and age is 15
**************************************************************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.