سؤال

I need fill in the constructor for a person class. I am new to Java and programming in general. I would like some help maybe understanding the problem and understanding the logic or theory behind the solution. This is what I need to do:

TODO #1: Fill in the rest of the arguments to the Person constructor The Person constructor should take in 3 additional arguments: 1) String pictureName 2) int xCoord 3) int yCoord TODO #2: Fill in the rest of this constructor
You need to create a local variable called picture and assign to picture the picture that we want to use. TODO #3: You need need to move the picture to xCoord and yCoord. You can move the picture object by calling the translate method. the last step is to draw the picture.

And this is what I've done but I get error, and I don't know why. I believe that's what I need to do unless I am missing something. If someone could help me figure this out and and explain this very clear would be exceptionally appreciated. I am new to Java and programming so my apologies if this question is

public class Person
{
    private String name;
    private String friends;

    public Person (String aName, String pictureName, int xCoord, int yCoord) // I have added: String pictureName, int xCoord, and yCoord parameters.
    {
        name = aName;
        friends = "";

      //My solution starts here
        String picture;
        picture = pictureName;
        picture.translate(xCoord, yCoord);
        picture.draw();
      // and ends here.
    }

    public void addFriend(Person friend) 
    {
        friends = friends + friend.name + " ";
    }

    public void unfriend(Person nonFriend)
    {
        friends = friends.replace(nonFriend.name + " ", "");
    }

    public String getFriends() 
    {
        return friends;
    }
}
هل كانت مفيدة؟

المحلول

picture and pictureName are String objects.

The java.lang.String class doesn't have a .translate() method and thus your code fails to compile.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top