Write a main method in the TestPerson.java file and in this method: 1. Declare an array of 3 Person objects. Instantiate each of the Person objects by obtaining the necessary attribute values from a user through keyboard input. 2. Call the getName(), getAddress() and getPPSNo() methods to print the information of the first person to the screen (it should be displayed exactly as the sample output below). 3. Call the setName(<…>) and setAddress(<…>) method to change the information on the second Person. 4. Use a for loop and the showPerson() method to print all the information for all People to the screen (it should be displayed exactly as the sample output below). 5. Call the toString() method on the 3rd Person object in the array.
时间: 2024-02-13 18:05:43 浏览: 67
Sure, here is an example implementation of the main method in the TestPerson.java file based on your requirements:
```java
import java.util.Scanner;
public class TestPerson {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Person[] persons = new Person[3];
// Instantiate each Person object by obtaining the necessary attribute values from a user
for (int i = 0; i < persons.length; i++) {
System.out.println("Enter details for Person " + (i + 1) + ":");
System.out.print("Name: ");
String name = scanner.nextLine();
System.out.print("Address: ");
String address = scanner.nextLine();
System.out.print("PPS Number: ");
String ppsNo = scanner.nextLine();
persons[i] = new Person(name, address, ppsNo);
}
// Print the information of the first person to the screen
System.out.println("Name: " + persons[0].getName());
System.out.println("Address: " + persons[0].getAddress());
System.out.println("PPS Number: " + persons[0].getPPSNo());
// Change information on the second Person
persons[1].setName("John Doe");
persons[1].setAddress("123 Main Street");
// Print all the information for all People to the screen
for (Person person : persons) {
person.showPerson();
}
// Call the toString() method on the 3rd Person object in the array
System.out.println(persons[2].toString());
}
}
```
Note that this implementation assumes that the `Person` class has the following methods:
```java
public class Person {
private String name;
private String address;
private String ppsNo;
public Person(String name, String address, String ppsNo) {
this.name = name;
this.address = address;
this.ppsNo = ppsNo;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getPPSNo() {
return ppsNo;
}
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
public void showPerson() {
System.out.println("Name: " + name);
System.out.println("Address: " + address);
System.out.println("PPS Number: " + ppsNo);
}
@Override
public String toString() {
return "Person{name='" + name + "', address='" + address + "', ppsNo='" + ppsNo + "'}";
}
}
```
Sample output:
```
Enter details for Person 1:
Name: Alice Smith
Address: 456 Elm Street
PPS Number: 1234567A
Enter details for Person 2:
Name: Bob Johnson
Address: 789 Oak Road
PPS Number: 1234567B
Enter details for Person 3:
Name: Claire Davis
Address: 1010 Maple Avenue
PPS Number: 1234567C
Name: Alice Smith
Address: 456 Elm Street
PPS Number: 1234567A
Name: John Doe
Address: 123 Main Street
PPS Number: 1234567B
Name: Claire Davis
Address: 1010 Maple Avenue
PPS Number: 1234567C
Person{name='Claire Davis', address='1010 Maple Avenue', ppsNo='1234567C'}
```
阅读全文