I'm using the Scanner class and need help in setting up get and set methods for where the program asks the user to enter a first name and a last name as String type.( 2 methods, 1 for first, 1 for last) I need to know how to store the names because...

I am creating a client of the first class and need to call the methods in order to display what the user inputted at the end of the program.

How do you type code for this?

Any help is appreciated.

Dani AI

Generated

As noted, you can set the name fields either at construction time or afterwards with setters. Constructors are simple when the values are known once; setters make sense when you need to validate or allow changes later. Below is a compact, practical pattern that shows safe get/set methods for first and last name, plus a small convenience method to return the full name.

public class Person {
    private String firstName = "";
    private String lastName = "";

    public Person() { }

    public Person(String firstName, String lastName) {
        setFirstName(firstName);
        setLastName(lastName);
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = (firstName == null) ? "" : firstName.trim();
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = (lastName == null) ? "" : lastName.trim();
    }

    public String getFullName() {
        if (firstName.isEmpty()) return lastName;
        if (lastName.isEmpty()) return firstName;
        return firstName + " " + lastName;
    }

    @Override
    public String toString() {
        return getFullName();
    }
}

Usage pattern in your client/driver: create a Person (either with the two-arg constructor or with the no-arg constructor and set... calls), then call getFullName() or getFirstName()/getLastName() to display. Quick tips: if you read names from Scanner and names may contain spaces, use nextLine() rather than next(); always trim() and null-check input in setters; avoid exposing fields publicly; and consider making the class immutable (final fields, only constructor, no setters) if names should never change. This follows the same set/get pattern shown in 's example for dates, and follows 's good advice to post code in code blocks.

Recommended Answers

All 4 Replies

First, do you really need set methods? Most of the time you can use constructor arguments to set the fields and set methods aren't required:

Scanner in = new Scanner ( System.in ).useDelimiter ( ' ' );

System.out.print ( "Full name (ex. John Smith): " );
String first = in.next();
String last = in.next();

Client obj = new Client ( first, last );

Then writing the get methods only consists of returning the necessary field. The chances of an object representing a person needing to change the name are very slim, so set methods don't make much sense except in very specific situations.

class MyDate
{
int dd,mm,yy;
public void initDate ()
{
dd=mm=yy=0;
}
public void setDate(int d,int m,int y)
{
dd=d;
mm=m;
yy=y;
}
public void dispDate()
{
System.out.println(

class MyDate
{
int dd,mm,yy;
public void initDate ()
{
dd=mm=yy=0;
}
public void setDate(int d,it m,int y)
{
dd=d;
mm=m;
yy=y;
}
public void dispDate()
{
System.out.println("Date is:"+dd+"-"+mm+"-"+yy);
}
public static vo main (String args[])
{
MyDate D1;
d1=new MyDate();
d1.initDate();
d1.dispDate();
d1.setDate(3,7,90);
d1.dispDate();
}
}

Please use code tags when posting your code. use the rightmost icon above the text input field. The one that says: (CODE)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.