Your Tester class main method must use an array or an ArrayList of Person objects. It must
be populated from a text data file, friends_data.txt. Add the following data to your text
file,
Michelle, 12/20/2008, Camilla
Bryan, 3/8/2007, Tom
Camilla, 6/7/2005, Michelle
Tom, 10/15/2007, Bryan
Charlotte, 3/2/2008, Michelle
Each line has the meaning:
Person name, Person date of birth (MM/DD/YYYY), name of best friend
Write a Java program that reads in the data from the friends_data.txt file, allocates a new
Person for each and then stores the newly created object in either an Array or an ArrayList.
Next print out a report showing the following information for each Person,
1. The Person’s name
2. Their popularity counter
3. Their age on May 1, 2014
4. The name of their best friend
5. The age of their best friend on May 1, 2014
Finally, print the name of the most popular Person and the name of the oldest Person.
Person Class
import java.util.ArrayList;
public class Person {
public String personsName;
public String personsFriend;
public String personsBirthday;
public int personsPopularity;
public int popularity = 0;
public Person(String aName, String aFriend, String aBirthday) {
personsName = aName;
personsFriend = aFriend;
personsBirthday = aBirthday;
}
public String getName() {
return personsName;
}
public String getFriendsName() {
return personsFriend;
}
public String getBirthday() {
return personsBirthday;
}
public int getPopularityNumber(String[] friends) {
for (int i = 0; i < 5; i++) {
if (personsName.equals(friends[i])) {
popularity = 1 + popularity;
} else
popularity = popularity;
}
return popularity;
}
public String getFriend() {
return personsFriend;
}
public int getAge() {
int MONTH = 0;
int DAY = 0;
int YEAR = 0;
for (int i = 0; i < 3; i++) {
String[] dates = personsBirthday.split("/");
String month = dates[0];
String day = dates[1];
String year = dates[2];
MONTH = Integer.parseInt(month);
DAY = Integer.parseInt(day);
YEAR = Integer.parseInt(year);
}
int age = (2014 - YEAR);
int moAge = (5 - MONTH);
if (moAge < 0) {
age--;
}
return age;
}
}
PersonTester
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;
public class PersonTester {
public static void main(String[] args) throws FileNotFoundException {
File inputFile = new File("friends_data.txt");
Scanner in = new Scanner(inputFile);
final int SIZE = 5;
Person[] personsOfInterest = new Person[SIZE];
String[] friends = new String[5];
int i = 0;
int popularity = 0;
String person;
while (in.hasNextLine()) {
String line = in.nextLine();
String[] nameBirthdayname = line.split(", ");
person = nameBirthdayname[0];
String birthday = nameBirthdayname[1];
String friend = nameBirthdayname[2];
friends[i] = friend;
personsOfInterest[i] = new Person(person, friend, birthday);
i++;
}
for (i = 0; i < SIZE; i++) {
System.out.println("");
System.out.println("New Person");
System.out.println("--------------------------------------------------------");
System.out.println("Persons Name : " + personsOfInterest[i].getName());
System.out.println("Popularity : " + personsOfInterest[i].getPopularityNumber());
System.out.println("Their age on May 1, 2014 : " + personsOfInterest[i].getAge());
System.out.println("Persons Best Friend : " + personsOfInterest[i].getFriend());
System.out.println("Best Friends Age on May 1,2014 : " + personsOfInterest[i].getName());
System.out.println("--------------------------------------------------------");
}
}
}
I keep getting this error from the compiler:
System.out.println("Popularity : " + personsOfInterest[i].getPopularityNumber());
"method getPopularityNumber in class Person cannot be applied to given type:
Required: java.lang.String[]; found: no arguments; reason: actual and formal argument lists differ in length
How do I fix this? What am I doing wrong?