0

I am new to JAVA. Here is an app from a class I created. I am looking for specific age information from an int ageGV. I want the max and min int value. Here is my approach any suggestions on how to successfully run the if statements I want and display the maximum and minimum ageGV?

public class app {

    public static void main(String[] args) {
        // professor p = new professor("Name", "LName", "Degree", Age); 
        professor p0 = new professor("Chad", "Froes", "Biochemistry", 21);
        professor p1 = new professor("Chad", "Froes", "Biochemistry", 21);
        professor p2 = new professor("Carol", "Hammond", "Modern Art", 43);

         //Oldest professor
        if (p0.ageGV > p1.ageGV && p0.ageGV > p2.ageGV) {
            System.out.println(p0.getOldest());
        } else if (p1.ageGV > p0.ageGV && p1.ageGV > p2.ageGV) {
            System.out.println(p1.getOldest());
        } else if (p2.ageGV > p1.ageGV && p2.ageGV > p0.ageGV) {
            System.out.println(p2.getOldest());
        }

        System.out.println("\n");

        //Youngest sort
        if (p0.ageGV < p1.ageGV && p0.ageGV < p2.ageGV) {
            System.out.println(p0.getYoungest());
        }
        if (p1.ageGV < p0.ageGV && p1.ageGV < p2.ageGV) {
            System.out.println(p1.getYoungest());
        }
        if (p2.ageGV < p1.ageGV && p2.ageGV < p0.ageGV) {
            System.out.println(p2.getYoungest());
        }

    }
}
vandale
  • 3,600
  • 3
  • 22
  • 39
Ross P.
  • 21
  • 1
  • 1
  • 3

2 Answers2

0

As written your code should work fine, except I believe you want to replace .getOldest() and .getYoungest() with .getAge().

HOWEVER, this is not an ideal solution. For N professor objects, you can put them all in a collection like an ArrayList and then sort them based on the age field (the link discusses how to do that). Basically you just need to implement the Comparator interface in your professor class and then call arrayList.sort().

Finally, consider renaming your class Professor to abide by standard Java naming conventions.

Community
  • 1
  • 1
TypeIA
  • 16,916
  • 1
  • 38
  • 52
0

Your code has a flaw as there's no handling of the case where the professor's age is the same in all three cases.

Although dvnrrs has a good suggestion in terms of a solution, I don't think that it's very intuitive for someone just learning Java. Therefore, I'd suggest that you use the Math.max and Math.min functions instead. (API Link)

All the functions do is return the maximum (or minimum) of two input integers. This makes it pretty easy to work out the oldest (or youngest) professor by keeping the current maximum (or minimum) in a variable and then iterating through all the professors and seeing if each one can 'beat' the current maximum (or minimum).

int oldest = p0.getAge();
oldest = Math.max(oldest, p1.getAge());
oldest = Math.max(oldest, p2.getAge());

System.out.println("The oldest professor is " + oldest);

int youngest = p0.getAge();
youngest = Math.min(youngest, p1.getAge());
youngest = Math.min(youngest, p2.getAge());

System.out.println("The youngest professor is " + youngest);

You should add a method to your Professor class (and name it with a capital 'P') like so

public int getAge()
{
    // assuming that ageGV is the instance field that holds each professor's age
    return ageGV;
}
Catchwa
  • 5,845
  • 4
  • 31
  • 57