I am using a driver class to create an object of another class. When I enter the pet weight or integer the number comes out to 0.0. All of the weight variables are declared as double so I do not know why it is doing this.
import java.util.Scanner; public class PetAssignment { public static void main(String[] args) { String nameAndType; int yrs; double lbs; //Scanner object for the keyboard input Scanner answers = new Scanner(System.in); //Pet objects used for calling accessor methods Pet petName = new Pet(); Pet petType = new Pet(); Pet petAge = new Pet(); Pet petWeight = new Pet(); //A bunch of other code and pet attributes //Input for the weight of pet System.out.print("How many pounds does your pet weight? "); lbs = answers.nextDouble(); petName.setWeight(lbs); //Print out of the user's answers System.out.println(""); System.out.println("You have a "+ petType.getType() + ". That is named " + petName.getName()+ " and is " + petAge.getAge() + " years old and weighs " + petWeight.getWeight() + " lbs."); } } Here is my Pet Class
public class Pet { private String name; private String type; private int age; private double weight; /* * a bunch of other code */ public void setWeight(double petWeight) { weight = petWeight; } /* * a bunch of other code */ public double getWeight() { return weight; } }
Petinstance for every attribute? They're all the same pet, no? You're setting the weight value on yourpetNameinstance and writing it out from yourpetWeightinstance. Just create onePetand use that to set/get all attributes.petWeight.getWeight()will have a default value0.0because you didn't set a value on its instance object.