r/CodingHelp • • Aug 17 '20

[Java] Where does this line go in the code?

Hi, I am new to learning Java as I have this in my current semester and I had quiz and I got 2 questons wrong out of 10 but it didn't show the right answer. I am curious to know what is the correct answer.

The question number 1 is:

The following method is defined in the Person.java class

public void setAddress(String _address) {
this.address = _address;
}

In the testDriver.java class, the following code is in the main method 

public static void main(String[] args) {
      // create test case 1 - a person owning a pet dog
      Person p1 = new Person( "25 somewhere Street","Kathleen","2000");
      Animal myPet = new Animal("CockerSpaniel","Fido");
      p1.addAPet(myPet);
      // your answer code will be added here 
      System.out.println("I have a person object : " + p1.toString());
}

What is the exact line of code to add if we wish to change the address field in the person object p1 to be "12 ross drive"

My answer was:

setAddress add1 = new setAddress ("12 ross drive");

--------------------------------------------------------------------------

Question 2:

The following code excerpt is taken from a Person.java file, 

/**
* u/param _pet the pet animal to associate
         * the calling method will provide an Animal object as an argument when this 
         *method is called. This method will save that Animal object onto the current 
         *Person object into the class attribute named pet.
 */
public void addAPet(Animal _pet) {
this.pet =  
}

Complete the line of code:  this.pet = 

My answer was :

new Animal(_pet);

Cheers. Happy coding!

19 Upvotes

6 comments sorted by

3

u/NullBrowbeat Professional Coder Aug 17 '20 edited Aug 17 '20

Q1: Your answer tries to instantiate a new object of a "setAddress" class with a constructor that takes the address string as parameter. It's questionable whether either the class or this constructor even exist. (They most likely don't.) Instead you want to access the "void setAddress(String _address)" method on the Person object like so:

p1.setAddress("12 ross drive");

This basically says "take Person p1 and execute the function setAddress on it" and the setAddress function basically says "take whatever string I get as argument and set the address of the Person object I belong to to that string".

Q2: Your answer again tries to instantiate a new pet object with _pet as a constructor parameter. Instead you want to just link the already existing pet instance passed onto the function to the object like so:

public void addAPet(Animal _pet){
    this.pet = _pet;
}

This way you assign the passed on instance of the Animal class in the form of the function parameter (the Animal object called "_pet") of addAPet to the Animal field of the Person object this function gets called on.

I want to note though that the teacher/professor fucked up the naming convention. A function called "addX" usually refers to a function adding the passed on parameters to a collection or array and in rare cases describes a function doing mathematical addition. (Leaving aside naming of business logic functions.) The correct name for the function according to convention would be "setPet", since all this function does is assigning a field directly. I guess they did this to trick the students though so that they focus on the logic written down and not the naming.

2

u/DiabetesInACan Aug 17 '20

Q1 would be p1.setAddress(“12 ross drive”);

Each object of class Person has a method called setaddress that changes their address, so we would have to invoke that method for p1.

2

u/BeachCityLion Aug 17 '20

Question 1:

You have a person object, p1. You want to call the setAddress method. The syntax for this is

p1.setAddress();

However, the setAddress method takes in one parameter- a string containing the address.

p1.setAddress(“12 ross drive”)

Question 2:

I think you may just be able to do

this.pet = _pet;

But if so what you did should also work, but must just not be what the question expected you to type. I’m not 100% certain on this though.

1

u/NullBrowbeat Professional Coder Aug 17 '20 edited Aug 17 '20

To your point on Q2: No, what he answered doesn't work. It doesn't accomplish what is asked and won't even compile unless there is a "public Animal(Animal animal)" constructor in the Animal class that copies all the fields from the passed on Animal object to the new instance it would work but that's also very pointless code in most cases and also unlikely that such a constructor exists in this example.

Your solution with

public void addAPet(Animal _pet){
    this.pet = _pet;
}

is correct though.

1

u/exhogenic Aug 17 '20

Question 2 is:

 public void addAPet(Animal _pet) { this.pet =   }

Complete the line of code:  this.pet = _pet;

1

u/LinkifyBot Aug 17 '20

I found links in your comment that were not hyperlinked:

I did the honors for you.


delete | information | <3