I'm having another problem with a homework assignment. We have to have 2 classes, one has the main program and the other has all the methods for the data received in the first class. Here is what I have. Everything works except the printInfo method always prints the data entered last, e.g. person 3
The first code is my main class, the second is my methods class
import java.util.*; public class FindYoungest { public static void main(String[] args){ Scanner stdin = new Scanner(System.in); PersonInfo person1 = new PersonInfo(); PersonInfo person2 = new PersonInfo(); PersonInfo person3 = new PersonInfo(); //Get all info for person 1 System.out.println("Enter the name of the 1st person: "); person1.setName(stdin.next()); System.out.println("Enter his/her RID: "); person1.setRID(stdin.next()); System.out.println("Enter his/her age: "); person1.setAge(stdin.nextInt()); System.out.println("Enter his/her favorite car: "); person1.setCar(stdin.next()); //Get all info for person 2 System.out.println("Enter the name of the 2nd person: "); person2.setName(stdin.next()); System.out.println("Enter his/her RID: "); person2.setRID(stdin.next()); System.out.println("Enter his/her age: "); person2.setAge(stdin.nextInt()); System.out.println("Enter his/her favorite car: "); person2.setCar(stdin.next()); //Get all info for person 3 System.out.println("Enter the name of the 3rd person: "); person3.setName(stdin.next()); System.out.println("Enter his/her RID: "); person3.setRID(stdin.next()); System.out.println("Enter his/her age: "); person3.setAge(stdin.nextInt()); System.out.println("Enter his/her favorite car: "); person3.setCar(stdin.next()); //Determine who is youngest if(person1.getAge() < person2.getAge() && person1.getAge() < person3.getAge()){ person1.printInfo(); }else if(person2.getAge() < person1.getAge() && person2.getAge() < person3.getAge()){ person2.printInfo(); }else person3.printInfo(); } }
public class PersonInfo { private static String name, RID, car; private static int age; public String getName(){ return name; } public void setName(String getName) { name = getName; } public String getRID(){ return RID; } public void setRID(String getRID) { RID = getRID; } public int getAge(){ return age; } public void setAge(int getAge) { age = getAge; } public String getCar(){ return car; } public void setCar(String getCar) { car = getCar; } public void printInfo() { System.out.println("The details for the youngest are:" + "\nName: " + getName() + "\nRID: " + getRID() + "\nAge: " + getAge() + "\nFavorite Car: " + getCar()); } }