0

The code doesn't show any errors. However, I am getting an unexpected output. I am supposed to get the smallest number for age, but what I keep getting is the last value entered for age. Can you help me point out the mistakes in this code?

Maybe there is some logical error in the getYoungestPet() method?

package pet;

public class Pet 
{
    public static String petName; 
    public static int petAge, petWeight;
    int youngestAge=9999;
    static int test;




    public static String setPetName()
    {
      return petName;
    }

    public int setPetAge()
    {

        return petAge;
    }

    public int setPetWeight()
    {

        return petWeight;
    }


    public int getYoungestPet() //probably an error here..?
    {

        if (petAge<youngestAge)
            youngestAge=petAge;
        return 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 );  
     Scanner input = new Scanner(System.in);
       petName= input.next();        
    System.out.println("Your pet's name is : " + petName);
    System.out.println(" ");

    System.out.println("Please enter " + petName + "'s Age" );
       petAge= input.nextInt();    
    System.out.println("Your pet's age is : " + petAge);
    System.out.println(" ");

    System.out.println("Please enter " + petName + "'s Weight" );
        petWeight= input.nextInt();
    System.out.println("Your pet's weight is : " + petWeight);
    System.out.println(" ");                

        System.out.println(PetObject.getYoungestPet());

    }


}
}

The code is supposed to show the smallest age but it shows the latest entered age.

2
  • 1
    It is happening because you're not setting those properties to your PetObject object! Commented Jul 7, 2019 at 4:27
  • Tip: Never solve the error "non-static cannot be referenced from a static context" by blindly adding static to things. Commented Jul 7, 2019 at 6:04

3 Answers 3

2

you should declare youngestAge as static variable. so that all of the petObject could share the same value.

static int youngestAge=9999;

your setter and getter methods are also not proper.

public static String setPetName()
{
  return petName;
}

should be:

public static void setPetName(String name)
{
  petName=name;
}

Also don't forget to set values into PetObject from main method.

...
petName= input.next(); 
PetObject.setPetName(petName);
...
Sign up to request clarification or add additional context in comments.

Comments

0

There are many things that are problematic with this code.

But just to answer your question directly, think about how many pet objects there could be in this program if every time the for loop runs it recreates the pet object because it is inside the for loop. however, simply moving it outside the for loop will not help because then you will simply keep resetting the values of the same pet object every time you run the for loop. Consider making an array of pet objects.

Also, your code never actually accesses the pet objects instance variables

In addition, there are other problems with your use of static as others have pointed out. cheers.

Comments

0

Each time you are creating a Pet, you are getting a different youngestAge with value 9999 for that object. So each time it is comparing the latest petAge with 9999 and giving you the latest petAge as your enterd petAge is less than 9999.

If you need to store the smallest age, then keep it in a static field. Cause, keeping an extra field to store the smallest age for all object is redundant for memory.

If you want your desired output with the existing design, then do this:

Make youngestAge static:

static int youngestAge=9999;

And also don't forget to make the method static too. There is no need to make it object property anymore, both the field variables, it is using, are static.

public static int getYoungestPet()
{
    if (petAge<youngestAge)
        youngestAge=petAge;
    return youngestAge;
}

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.