Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My class needs to accept the full name and not just the first name and I\'m not

ID: 3642892 • Letter: M

Question

My class needs to accept the full name and not just the first name and I'm not sure how to do that. I've marked where the change is needed.

import java.util.Scanner;
public class Person2 {
private Name name;
private int age;

Scanner keyboard = new Scanner(System.in);

public Person2()
{}

public Person2(Name n,int a)
{
name.setFirstName(n.getFirstName()); //I need the full name not just the first name
age=a;
}
public String toString()
{
return (name.toString() + " " + age);
}

public void setName(Name name)
{
this.name.setFirstName(name.getFirstName()); //Here too
}
public void setAge(int a)
{
age=a;
}
public void setPerson(Name n,int a)
{
name.setFirstName(n.getFirstName()); //and here
age=a;
}


public void read()
{
name.readName();
System.out.println("Please enter age");
age = keyboard.nextInt();
}

public Name getName()
{
return name;
}
public int getAge()
{
return age;
}

public boolean equals(Person2 n)
{
return (this.name.equals(n.name)&&
this.age==n.age);

}
public boolean hasSameName(Person2 n)
{return name.equals(n.name);
}
public boolean hasSameAge(Person2 n)
{return age==n.age;
}
public boolean isOlderThan(Person2 n)
{return age>n.age;
}

}

Explanation / Answer

setFirstName() in the Name class only accepts Strings as arguments. So there are two ways to go about fixing the error, I'm not sure which way you're supposed to do it for the assignment.

You can either change the parameters for the functions so that you feed them Strings instead of Name objects as so:

public Person2(String n,int a)

{

name.setFirstName(n);

age=a;

}     

public void setName(String name)

{

this.name.setFirstName(name);

}

public void setPerson(String n,int a)

{

name.setFirstName(n);

age=a;

}

Or if you have to use Name objects as parameters rather than strings, you can do this instead:

public Person2(Name n,int a)

{

name.setFirstName(n.getFirstName()); //Since setFirstName() only accepts strings, you need to get the first name String from the Name object using getFirstName()

age=a;

}

public void setName(Name name)

{

this.name.setFirstName(name.getFirstName()); //same goes for here

}

public void setPerson(Name n,int a)

{

name.setFirstName(n.getFirstName()); //and here

age=a;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote