Java Method for Formatting String Fields

class Person contains personal data

Its constructor takes 3 parameters, two strings containing first and last names, and an int representing age

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

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

Create a Person class and paste the entire class in the text box 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 everything, thanks for helping the guys!

+4
source share
8 answers

I will show you a little, just to get you started.

You need to declare variables in your class to store the values โ€‹โ€‹passed to the constructor. Then you 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. } 
+13
source

You don't seem to understand what a method is. Since it sounds like you are doing this for a class, you should probably look through the textbook or discuss with your teacher.

You can also try the official official Java tutorial , especially the first section on classes.

+4
source

You use getName as a variable, but it must be a method .

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

Also see Sun's tutorial on: Defining Methods . However, I am surprised if you have not yet studied / received this information during the course. I will consult with your mentor again.

+3
source

Are you using a version of Windows or Mac on BlueJay? In any case, I will go over the Mac version and hope it works.

Create a new class and enter Person as the class.

Open 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 ya 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 have indicated is

  • Its constructor takes 3 parameters, two strings containing first and last names, and an int representing age

What have you done well with the code

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

So there are no changes .: D

NOT

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

[We could use this instead, but donโ€™t confuse ourselves, it seems the person wanted you to use the first one)

Although we want this class to receive these 3 parameters, so we need 1) have private variables to store these values โ€‹โ€‹2) assign them inside 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 put in your constructor change and 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; } 

Okay, just breathe and watch. We have two sets of each variable !!! Well, one for the class, and the other is what comes with the constructor. To explain everything, there are too many lines now :(, so you will need to read the Java book. [Only the first few chapters ... read it, watching season 1 of 24], most books come with this example of the Person class.

Well, so killed the first call. Let's look at the second one.

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

Therefore, his method will go on to this section for brevity.

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

This will explain it much better if I do it. Ok, I'll give you some time.

Did you read? Not? Go read this Grr! :( Important

So, now that you have done what 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 made by BlueJ, and it looked like this. It gets names (this.lastName and this.firstName NOT lastName and firstName, which will work, but don't confuse yourself with k?)

Full code with 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 ya 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 following this right path? Like? If you did not know anything about coding, variables, data types and methods, I would say that this was a fair assumption:) ... but you really should read the Java Intro book.

The right program? naw ... This BlueJ program is weird.

Google for "NetBeans" is free.

Itโ€™s good that breakfast on the West Coast has passed.

+3
source

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

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

Read the Java Tutorial

+1
source

The code you posted doesn't define the methods you think. Take a look at the basic Java tutorial on how to define a method and how to define and assign a variable.

0
source

You can change your code to compile it, and then you can figure out how to improve it.

So this 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 will have something like:

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

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

Since this is homework, I am trying to show as much new code as possible, but hopefully this helps you.

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

0
source

Here is another option that is somewhat similar to what you originally linked. You can use the String.format method to specify a format string, and then pass the values โ€‹โ€‹to be replaced.

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

Source: https://habr.com/ru/post/1311223/


All Articles