开发者

Java method to format String fields

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开发者_StackOverflow 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!


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.
}


You appear not to understand what a method is. Since it sounds like you are doing this for a class you should probably review your textbook or discuss with your teacher.

You could also try the official Java tutorial, particularly the first section, on Classes.


You're using getName as a variable, but it should be a method.

public String getName() {
    // Implement accordingly.
}

Also see the Sun tutorial on the subject: Defining Methods. I however wonder if you didn't already learn/got this information during the course. I'd consult your tutor about it once again.


In BlueJay , are you using the Windows or Mac Version ? Either way I will go through the Mac version and hope it works.

Create a New Class and enter Person as the class.

Bring up the editor window by Right-Clicking on the class and clicking Open Editor.

Your class should have the following format.

/*** Write a description of class Person here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
 public class Person
 {
// instance variables - replace the example below with your own
private int x;

/**
 * Constructor for objects of class Person
 */
public Person()
{
    // initialise instance variables
    x = 0;
}

/**
 * An example of a method - replace this comment with your own
 * 
 * @param  y   a sample parameter for a method
 * @return     the sum of x and y 
 */
public int sampleMethod(int y)
{
    // put your code here
    return x + y;
}

}

The first requirement you stated is

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

Which you did well with the code

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

So no changes there. :D

NOT

public Person(String first, String last, int age)

[We could use this instead but lets not confuse ourselves it seems the person wanted you to use the first one]

Though we want this class to get these 3 parameters so we need to 1)have private variables to hold these values 2) assign them within the constructor.

Looking back at what BlueJ gave with these lines

// instance variables - replace the example below with your own
private int x;

/**
 * Constructor for objects of class Person
 */
public Person()
{
    // initialise instance variables
    x = 0;
}

We want to place in your change in the constructor and the additional changes 1) and 2)

    // instance variables - replace the example below with your own
// private int x; replace blueJ sample variable
private String firstName; //not the same as the one given in the constructor
private String lastName; //not the same as the one given in the constructor
private int age; //not the same as the one given in the constructor

/**
 * Constructor for objects of class Person
 */
public Person(String firstName, String lastName, int age)
{
    // initialise instance variables
    //x = 0; replace BlueJ sample with
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
}

Ok so just breathe and watch it. We have two sets of each variable !!! Well one is for the class and the other is what is being supplied to the constructor. To explain everything now is just too many lines :( , so you will need to read a Java book [Just the first few chapters ... read it while watching season 1 of 24], a majority of Books comes with this Person class example.

Alright so killed the first requirement. Lets look at the second one.

-its method getName has no parameters and returns a String with format "Lastname, Firstname"

So its a method, for the sake of brevity , jump to this section

http://java.sun.com/docs/books/tutorial/java/javaOO/methods.html

It will explain it a lot better then if I were to do it. OK, I will give you some time.

Did you read it ? No ? Go read it Grr! :( It is important

Ok so now that you have done that the following should make sense to you.

/**
 * The getName Method - put in a little description here
 * 
 * @return     the String with the format "Lastname, Firstname"
 */
public String getName()
{
    // put your code here
    // I did and this is called string concatenation in java
    // Google it:"string concatenation in java"

    return this.lastName + ", " + this.firstName;
}

So I made a method similar to the one BlueJ made and this how it looked. It gets the names (this.lastName and this.firstName NOT lastName and firstName which would work as well but lets not confuse ourselves k?)

The full code with in BlueJ

/**
 * Write a description of class Person here.
 * 
 * @user208639 (Is that your real name ?) 
 * @version (a version number or a date)
 */
 public class Person
 {
 // instance variables - replace the example below with your own
 // private int x; replace blueJ sample variable
 private String firstName;
 private String lastName;
 private int age;

/**
 * Constructor for objects of class Person
 */
public Person(String firstName, String lastName, int age)
{
    // initialise instance variables
    //x = 0; replace BlueJ sample with
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
}

/**
 * The getName Method - put in a little description here
 * 
 * @return     the String with the format "Lastname, Firstname"
 */
public String getName()
{
    // put your code here
    // I did and this is called string concatenation in java
    // Google it:"string concatenation in java"
    return this.lastName + ", " + this.firstName;
}

/**
 * An example of a method - replace this comment with your own
 * 
 * @param  y   a sample parameter for a method
 * @return     the sum of x and y 
 */
public int sampleMethod(int y)
{
    // put your code here
    return x + y;
}

}

Are you going about it the right way ? Sort of ? If you did not know anything about coding,variables,data types and methods I would say it was a fair guess :) ... but you should really read a Java Intro book.

Right program ? naw... This BlueJ program is weird.

Google for "NetBeans" it is free.

Ok it is way past breakfast time on the West Coast.


Java is not javascript, methods are not variables. getName would look like

public String getName(){
  return this.lastName + ", " + this.firstName;
}

Read the Java tutorial


The code you posted is not defining methods like you think it is. Look at a basic Java textbook for how to define a method and how to define and assign a variable.


You could modify your code to get it to compile, and then you can begin to understand how to improve it.

So, thiis could be one private method:

   getAge = age + 1; 
   return getAge; 

And this is another private method:

   birthday = age + 1; 
   newAge = birthday; 
   return newAge; 

So you would have something like:

   getName = "Lastname, Firstname";  
   System.out.print(last + first);  

   System.out.print(myageinc(age));  

   mydobinc(birthday);

Once you get everything broken into small functions, you can more easily figure out what you are doing.

Because this is a homework assignment I am trying to show as little new code as possible, but hopefully this will help you along.

Also, you need to define your variables, tell Java what type of variable it is, before you can assign to it.


Here's another option that is somewhat similar to what you were originally tying to do. You can use the String.format method to specify a format string and then pass in the values to be replaced.

public String getName(){
  return String.format("%s, %s", lastName, firstName);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜