Question: Dear expert, I'm learning java constructor and have the following question: What is the difference for the constructor which takes initialname as parameter, and the

Dear expert,

I'm learning java constructor and have the following question:

What is the difference for the constructor which takes initialname as parameter, and the method takes new name with parameter ?

the two methods I refer to are these :

public Pet2(String initialName, int initialAge,double initialWeight)

public void setPet(String newName, int newAge, double newWeight)?

Why do I need to do the things to times ?

Thank you.

public class Pet2

{

private String name;

private int age; //in years

private double weight;//in pounds

public Pet2(String initialName, int initialAge,

double initialWeight)

{

set(initialName, initialAge, initialWeight);

}

public Pet2(String initialName)

{

set(initialName, 0, 0);

}

public Pet2(int initialAge)

{

set("No name yet.", initialAge, 0);

}

public Pet2(double initialWeight)

{

set("No name yet.", 0, initialWeight);

}

public Pet2( )

{

set("No name yet.", 0, 0);

}

public void setPet(String newName, int newAge, double newWeight)

{

set(newName, newAge, newWeight);

}

public void setName(String newName)

{

set(newName, age, weight);//age and weight are unchanged.

}

public void setAge(int newAge)

{

set(name, newAge, weight);//name and weight are unchanged.

}

public void setWeight(double newWeight)

{

set(name, age, newWeight);//name and age are unchanged.

}

private void set(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;

}

}

public String getName( )

{

return name;

}

public int getAge( )

{

return age;

}

public double getWeight( )

{

return weight;

}

public void writeOutput( )

{

System.out.println("Name: " + name);

System.out.println("Age: " + age + " years");

System.out.println("Weight: " + weight + " pounds");

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!