My class needs to accept the full name and not just the first name and I\'m not
ID: 3642910 • 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
This should help you out: import java.util.*; public class Random { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Please enter your name: "); String name = input.nextLine(); String[] names = name.split(" "); if(names.length != 2) { System.out.println("ERROR! Please enter your first and last name"); return; } System.out.println("First: " + names[0]); System.out.println("Last: " + names[1]); } }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.