0

Iam new in java and i have a Java Person class and i have created a List of Persons and i am able to print the list but i donot want to print the minimum and maximum age in a person list my code is

public class Person {
    static String name;
    static String lname;
    static double age;

    Person(String name,string lname,double age) {
        Person.name = name;
        Person.lname= lname;
        Person.age = age;  
    }
}

    //Main method
public void testQuestionInput() {
    List<Person> persons = new ArrayList<Person>();
    persons.add(new Person("Foo", "scientist",5));
    persons.add(new Person("Foo", "scientist",4));
    persons.add(new Person("Foo", "teacher",10));
    persons.add(new Person("Bar", "student",11));
    persons.add(new Person("Foo", "scientist",12));

    for (Person person : _persons ) {
        System.out.print(person);
    }
}

I am able to print the list but i want to print a list in which i have to skip the maximum and minimum age in my case i want to print list without

"Foo", "scientist",4 and "Foo", "scientist",12

Thanks help would be appreciated.

6
  • Do you need to keep the order of the list? Also, what if there are ties for oldest or youngest? Commented Oct 14, 2013 at 3:59
  • i want to print a list without minimum and maximum age Commented Oct 14, 2013 at 4:00
  • You're going to have a problem with those static variables. They are going to be global, rather than objective. Commented Oct 14, 2013 at 4:05
  • Please, remove the static modifier of your instance variables. Commented Oct 14, 2013 at 4:06
  • What do you want to do if two people have the same age, and it's the maximum? Show one of them? Or suppress them both? Commented Oct 14, 2013 at 4:06

3 Answers 3

1

As it stands right now, you program won't work...

public class Person {
    static String name;
    static String lname;
    static double age;

    Person(String name,string lname,double age) {
        Person.name = name;
        Person.lname= lname;
        Person.age = age;  
    }
}

The use of static means that, based on your example, all your entries will named Foo scientist and will be age 12

Start by removing the static references...

public class Person {

    String name;
    String lname;
    double age;

    Person(String name,string lname,double age) {
        this.name = name;
        this.lname= lname;
        this.age = age;  
    }
}

Next, you could sort the list...

Collections.sort(persons, new Comparator<Person>() {
    public int compare(Person p1, Person p2) {
        return Double.compare(p1.age, p2.age);
    }
});

Then simply excluding the first and last entries...

for (int index = 1; index < persons.size() - 2; index++) {
    Person p = persons.get(index);
    System.out.println(p.name + " " + p.lname + " @ " + p.age);
}

For example...

The next problem will be, what happens if you have more then one entry with the minimum or maximum age??

After you've sorted the list, start by getting the lower and upper ranges...

int minAge = person.get(0).age;
int maxAge = person.get(person.size() - 1).age;

Then filter out those elements that don't meet the age boundries...

List<Person> withInLimites = new ArrayList<Person>(persons.size());
for (Person p : persons) {
    if (p.age > minAge && p.age < maxAge) {
        withInLimites.add(p);
    }
}

for (Person p : withInLimites) {
    System.out.println(p.name + " " + p.lname + " @ " + p.age);
}
Sign up to request clarification or add additional context in comments.

2 Comments

p1.age - p2.age will return double, i guess you need an cast to int before returning the value
@upog Hopefully that fixed, at least, that problem :P
0

Ignoring the possibility of ties, you can do this by traversing the list twice. The first time to find the minimum and maximum ages and the second time to do the printing:

public class Person {
    String name;
    String lname;
    double age;

    Person(String name, String lname, double age) {
        this.name = name;
        this.lname= lname;
        this.age = age;  
    }
}

public void testQuestionInput() {
    List<Person> persons = new ArrayList<Person>();
    persons.add(new Person("Foo", "scientist",5));
    persons.add(new Person("Foo", "scientist",4));
    persons.add(new Person("Foo", "teacher",10));
    persons.add(new Person("Bar", "student",11));
    persons.add(new Person("Foo", "scientist",12));
    double maxAge = Double.NEGATIVE_INFINITY;
    double minAge = Double.POSITIVE_INFINITY;
    for (Person person : _persons) {
        minAge = Math.min(minAge, person.age);
        maxAge = Math.max(maxAge, person.age);
    }
    for (Person person : _persons) {
        if (person.age != minAge && person.age != maxAge) {
            System.out.printf("%1$s, %2$s, %3$f%n",
               person.name, person.lname, person.age);
        }
    }
}

You would probably need to define a toString() method in class Person to get meaningful output from System.out.print(person) like you have in your current code.

1 Comment

@user1073794 - Oops. I had the initialization of maxAge and minAge reversed. Fixed now.
0

Your program must be like this:

import java.awt.List;
import java.util.ArrayList;

public class Person {
    static String name;
    static String lname;
    static double age;

    Person(String name, String lname, double age) {
        Person.name = name;
        Person.lname = lname;
        Person.age = age;

    }

    // Main method

    public void testQuestionInput() {
        ArrayList<Person> persons = new ArrayList<Person>();
        persons.add(new Person("Foo", "scientist", 5));
        persons.add(new Person("Foo", "scientist", 4));
        persons.add(new Person("Foo", "teacher", 10));
        persons.add(new Person("Bar", "student", 11));
        persons.add(new Person("Foo", "scientist", 12));
        for (Person person : persons) {

            System.out.print(person);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.