简体   繁体   中英

I'm trying to write a personclass and a personTester in Java

So far for my personClass, I have the following:

package personclass;

public class Personclass {
private static boolean Personclass;
private int PersonCount;
    public int getPersonCount()
    {
        return PersonCount;
    }

    private String FirstName;
    private String LastName;
    private int Age;
    private Double Height;
    private String Gender;

    public Personclass(String foreName, String surName, int age, Double height, String gender)
    {
        FirstName = foreName;
        LastName = surName;
        Age = age;
        Height = height;
        Gender = gender;
    }

    private String getFirstName()
        {
    return FirstName;
}

private void setFirstName(String foreName)
{
    this.FirstName = foreName;
}

private String getLastName()
{
    return LastName;
}

private void setLastName(String surName)
{
    this.LastName = surName;
}

private int getAge()
{
    return Age;
}

private void setAge(int age)
{
    this.Age = age;
}

private Double getHeight()
{
    return Height;
}

private void setHeight(Double height)
{
    this.Height = height;
}

private String getGender()
{
    return Gender;
}

private void setGender(String gender)
{
    this.Gender = gender;
}

/**
 *
 * @param FirstName
 * @param LastName
 * @param Age
 * @param Height
 * @param Gender
 */
public Personclass(String FirstName, String LastName, int Age, double Height, String Gender)
{
    this.FirstName = FirstName;
    this.LastName = LastName;
    this.Age = Age;
    this.Height = Height;
    this.Gender = Gender;
            ++PersonCount;
}

/**
 *
 * @return 
 */

@Override
public String toString()
{
        return "Person[forename=" + getFirstName() + ", surname=" +  getLastName() +  ", age=" + getAge() + ", height=" + getHeight() +"m" + ", gender=" + getGender() +"]";
}
public String format()
{
        return String.format("%10s %10s %10d %10.2f %10s", getFirstName() , getLastName() , getAge() , getHeight() , getGender());
}

public static void main(String[] args) 
{

}
}

And for my personTester, I have the following code:

package personclass;

public class PersonTester
{
public static void main(String[] args) 
{

        Person person1 = new Person("Joe","Smith",25,1.57,"Male");

        Person person2 = new Person("Sain","Davies",18,1.73,"Female");

        Person person3 = new Person("John","White",22,1.60,"Male");

        Person person4 = new Person("Martin","Taylor",26, 1.54,"Male");

        Person person5 = new Person("Jessica","Clarke",19,1.70,"Female");

        System.out.println(person1.toString());
        System.out.println(person2.toString());
        System.out.println(person3.toString());
        System.out.println(person4.toString());
        System.out.println(person5.toString());

}
}

The thing that I'm having trouble with is not being about to print anything out when I try and run the personTester. How would I go about trying to print the five different people out?

You need to make the objects in main() in class PersonTester type Personclass. That should enable you to print the information about the people, through the toString() method in that class.

There can be two possible solutions.

  1. You should make sure that you are running the PersonTester class, but not Personclass . ie, You should be using java PersonTester command if you are running from command line.

  2. The name of your class is Personclass but in your PersonTester class, you are referring to some Person class. You should change the Personclass to something like the following.

    package personclass;

      public class PersonTester{ public static void main(String[] args) { Personclass person1 = new Personclass ("Joe","Smith",25,1.57,"Male"); Personclass person2 = new Personclass ("Sain","Davies",18,1.73,"Female"); Personclass person3 = new Personclass ("John","White",22,1.60,"Male"); Personclass person4 = new Personclass ("Martin","Taylor",26, 1.54,"Male"); Personclass person5 = new Personclass ("Jessica","Clarke",19,1.70,"Female"); System.out.println(person1.toString()); System.out.println(person2.toString()); System.out.println(person3.toString()); System.out.println(person4.toString()); System.out.println(person5.toString()); } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM