package pet; import java.util.*; public class Pet { /** * @param args the comman
ID: 3643684 • Letter: P
Question
package pet;
import java.util.*;
public class Pet {
/**
* @param args the command line arguments
*/
private String name;
private int age; //in years
private double weight; //in pounds
Scanner keyboard = new Scanner(System.in);
/**
* The Default constructor
*/
public Pet( )
{
name = "No name yet.";
age = 0;
weight = 0;
}
/**
* The constructor with three parameters
* @param initialName
* @param initialAge
* @param initialWeight
*/
public Pet(String initialName, int initialAge, double initialWeight)
{
name = initialName;
if ((initialAge < 0) || (initialWeight < 0))
{
System.out.println("Error: Negative age or weight.");
System.exit(0);
}
else
{
age = initialAge;
weight = initialWeight;
}
}
/**
* A mutator method with three parameters
* @param newName
* @param newAge
* @param newWeight
*/
public void setPet(String newName, int newAge, double newWeight)
{
name = newName;
if ((newAge < 0) || (newWeight < 0))
{
System.out.println("Error: Negative age or weight.");
System.exit(0);
}
else
{
age = newAge;
weight = newWeight;
}
}
/**
* The constructor with one parameter of String type
* @param initialName
*/
public Pet(String initialName)
{
name = initialName;
age = 0;
weight = 0;
}
/**
* The mutator method with one parameter: String type
* @param newName
*/
public void setName(String newName)
{
name = newName; //age and weight are unchanged.
}
/**
* The constructor with one parameter of int type
* @param initialAge
*/
public Pet(int initialAge)
{
name = "No name yet.";
weight = 0;
if (initialAge < 0)
{
System.out.println("Error: Negative age.");
System.exit(0);
}
else
age = initialAge;
}
/**
* The mutator method with one parameter of int type
* @param newAge
*/
public void setAge(int newAge)
{
if (newAge < 0)
{
System.out.println("Error: Negative age.");
System.exit(0);
}
else
age = newAge;
//name and weight are unchanged.
}
/**
* The constructor with one parameter of double type
* @param initialWeight
*/
public Pet(double initialWeight)
{
name = "No name yet";
age = 0;
if (initialWeight < 0)
{
System.out.println("Error: Negative weight.");
System.exit(0);
}
else
weight = initialWeight;
}
/**
* The mutator method with one parameter of double type
* @param newWeight
*/
public void setWeight(double newWeight)
{
if (newWeight < 0)
{
System.out.println("Error: Negative weight.");
System.exit(0);
}
else
weight = newWeight; //name and age are unchanged.
}
/**
* An accessor method which returns name
* @return name
*/
public String getName( )
{
return name;
}
/**
* An accessor method returns age
* @return age
*/
public int getAge( )
{
return age;
}
/**
* An accessor method returns weight
* @return weight
*/
public double getWeight( )
{
return weight;
}
/**
* readRecord method gets user inputs for data values needed for all instance variables
*/
public void readRecord()
{
// get pets name from user
System.out.println(" Please enter the pet's name, press return when done: ");
name = keyboard.nextLine();
System.out.println("The name you have entered is : " + name);
// get pet's age from user
System.out.println(" Please enter the pet's age in years, press return when done: ");
age = keyboard.nextInt();
System.out.println("The age you have entered is : " + age + " years");
// get pet's weight from user
System.out.println(" Please enter the pet's weight in pounds, press return when done: ");
weight = keyboard.nextDouble();
System.out.println("The weight you have entered is : " + weight + " lbs");
}
/**
* A method writes values of all instance variable out to the screen
*/
public void writeOutput( )
{
System.out.print(name + " " + age + " " + weight);
}
}
Explanation / Answer
Well your going to kick yourself when you see the error. just add a dogs[i]=new Dog(); to beginning of the for loop to get: http://codepad.org/FjDFBTJe Also you need to either include Dog in the pet package or reference the pet package. I just put it into the pet package as it is a pet: http://codepad.org/EsXJBhap If you have any other issues let me know, also if you could change the rating on the previous answer i would appreciate it a lot
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.