Cześć
Jestem na etapie zgłębiania wiedzy odnośnie Javy i mam aktualnie problem z zadaniem :
mam stworzyć klase Osoba: imie, miasto, wiek i stworzyć 3 metody:
- metodę ktora zwroci najstarsza osobe z podanej jako argument tablicy osob
- metoda ktora zwroci ile osob jest z danego miasta (metoda ma jako arg pobierac tablica osob i nazwe miasta ktora liczymy)
- metoda ktora obliczy sredni wiek osob z podanego jako argument miasta (oraz oczywiscie tablicy osob)
Póki co stworzyłęm klasę Person i PersonCollection, ale nie zabardzo wiem co dalej i jak rozgryść.
Mozę być tak że błądzę z kodem i prosiłbym o wskazówki jak to rozgryść.
public class Person {
private String name;
private String city;
private int age;
public Person(String name, String city, int age) {
this.name = name;
this.city = city;
this.age = age;
}
}
i klasę PersonCollection
public class PersonCollection {
public static void main(String[] args) {
ArrayList<Person> aList = new ArrayList<Person>();
Person person1 = new Person("Diana", "Dublin", 38);
Person person2 = new Person("Arthur","New York", 49);
Person person3 = new Person("Kim", "Katowice", 35);
Person person4 = new Person("Ghaffar","Lublin", 36);
Person person5 = new Person("Zach","Radom", 19);
aList.add(person1);
aList.add(person2);
aList.add(person3);
aList.add(person4);
aList.add(person5);
System.out.println("The size of the list is:" + aList.size());
}
public static void oldestPerson(String[] names, int[] ages)
{
int index = 0;
int oldest = ages[0];
for ( int i=0; i < ages.length; i++)
{
if(ages[i] > oldest)
{index = i;
oldest = ages[i];
}
System.out.println("Person" + names[index] + "is the oldest:" + ages [index]);
}
}