Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java method to format String fields

Tags:

java

class Person holds personal data

Its constructor receives 3 parameters, two Strings representing first and last names and an int representing age

public Person(String firstName, String lastName, int age) 

its method getName has no parameters and returns a String with format "Lastname, Firstname"
its method getAge takes no parameters and returns an int representing the current age
its method birthday increases age value by 1 and returns the new age value

Create the class Person and paste the whole class into the textbox below

    public class Person
{   
       private String Firstname;
        private String Lastname;
        private String Name1;
        private int getAge;
        private int birthday;
        private int newAge;
        private int age1;


public Person(String first, String last, int age)
    {
      Firstname = first;
      Lastname = last;
        age1 = age;


    }
       public String getName()
           {
       String getName = "Lastname , Firstname";
       Name1 = Lastname + ", "  + Firstname;
       return Name1;

    }
        public int getAge()
        {
       return age1;

    }

        public int birthday()
        {

       age1++;
       return age1;


    }

}

i fixed it all, thanks for your help guys!

like image 472
Ramuk Avatar asked Nov 28 '22 11:11

Ramuk


1 Answers

I'll show you a little bit, just to get you started.

You need to declare variables in your class to hold the values passed into the constructor. You then need a separate method to return the formatted name.

public class Person
{
    // all of the methods inside this class will have access to these variables
    private String first;
    private String last;
    private int age;

    public Person(String first, String last, int age)
    {
        this.first = first;
        // this.first refers to the "private String first" declared in the class
        // first refers to the local variable passed in as a parameter.
        // now you write the rest of the constructor
    }

    public String getName() {
        // what can you do with first and last here to return the formatted name?
    }

    // TODO: Add other methods to return age, etc.
}
like image 58
Bill the Lizard Avatar answered Dec 05 '22 15:12

Bill the Lizard