I wanted to find the youngest age inputted among x amount of pets. I have tried putting 'public static int youngestAge'; however, that just gets me the latest assigned value to the variable 'youngestAge'. I think this is because of the keyword 'static'; so, I tried removing it, but that just results in netbeans telling me 'Non-static variable cannot be referenced from a static context' . I am expecting the code to give the lowest inputted age.
package pet;
import java.util.Scanner;
public class Pet
{
public static String petName;
public static int petAge, petWeight;
public int youngestAge;
public static String setPetName()
{
Scanner input = new Scanner(System.in);
petName= input.next();
return petName;
}
public int setPetAge()
{
Scanner input = new Scanner(System.in);
petAge= input.nextInt();
return petAge;
}
public int setPetWeight()
{
Scanner input = new Scanner(System.in);
petWeight= input.nextInt();
return petWeight;
}
public void getYoungestPet()
{
if (youngestAge<petAge)
youngestAge=petAge;
System.out.println("The youngest age is " + youngestAge);
}
}
package pet;
import java.util.Scanner;
public class PetMain extends Pet
{
public static void main(String[] args)
{
System.out.println("How many pets do you want to enter? " );
Scanner data= new Scanner(System.in);
int petNumber=data.nextInt();
for (int i = 1;i<=petNumber; i++)
{
Pet PetObject = new Pet();
System.out.println("Please enter name for Pet " + i );
PetObject.setPetName();
System.out.println("Your pet's name is : " + petName);
System.out.println(" ");
System.out.println("Please enter " + petName + "'s Age" );
PetObject.setPetAge();
System.out.println("Your pet's age is : " + petAge);
System.out.println(" ");
System.out.println("Please enter " + petName + "'s Weight" );
PetObject.setPetWeight();
System.out.println("Your pet's weight is : " + petWeight);
System.out.println(" ");
if (youngestAge<PetObject.petAge)
youngestAge=PetObject.petAge;
}
System.out.println("The youngest age here is : " + youngestAge );
}