How can I fix my Pet class to work with the given PetDriver class?

In summary: Pet class so that the code in the main method works correctly. The Pet class should have a constructor that takes two parameters: a String, and an int. The constructor should use these parameters to set the values of the Pet class's private variables name and weight. The Pet class should also have two methods: getName(), which should return the value of the class's private variable name, and getWeight(), which should return the value of the class's private variable weight. In summary, the conversation is about fixing a Pet class by creating a constructor and two methods so that the main method can run correctly. The constructor should take in a String and an int as parameters and use them to set the values of the class
  • #1
Bellatrix6
1
0

Homework Statement


Suppose that you are given the following PetDriver class, which includes a main method:




public class PetDriver{
public static void main(String[] args){
int weight = 40;
Pet doggie = new Pet("Rover", weight);
System.out.println("my pet's name is " + doggie.getName());
System.out.println("my pet's weight is " + doggie.getWeight());
}
}

Executing main produces the following output:

my pet's name is Rover
my pet's weight is 40

Complete the Pet class definition, below, so that the main method shown above works correctly.


Homework Equations





The Attempt at a Solution


This is what I tried, but it didn't work:
public class Pet{
private String name;
private int weight;
public Pet(String who, int pounds){
name = who;
weight = pounds;
}
public String getName(){return name;}

public int getWeight(){return weight;}

}
 
Physics news on Phys.org
  • #2
You said it didn't work, but didn't give any further information, such as whether there were compiler errors, and if there were no compiler errors, what output did you get.

This looks like C++ code--is it? The thing that pops out at me is your constructor for your Pet class. You can't use the assignment operator (=) to assign a string to a variable. I haven't done much with C++ for a few years, so I'm not sure whether there are any methods on the String class that you can use to copy a String value to a variable. If there is one, you'll need to use it. If not, you can use some of the older C run-time functions, such as strcpy().

Mark
 
  • #3


Your attempt at a solution is on the right track, but there are a few errors that are preventing it from working correctly. First, make sure that your Pet class is in the same package as your PetDriver class. This will allow the PetDriver class to access the methods and variables in the Pet class. Additionally, in your PetDriver class, you are calling the constructor for the Pet class, but you are not passing in any arguments. You need to pass in the name and weight of the pet you want to create, like this:

Pet doggie = new Pet("Rover", weight);

Finally, make sure that your Pet class has a default constructor (a constructor with no parameters) in addition to the one you have already defined. This will allow you to create a Pet object without specifying a name or weight, like this:

Pet kitty = new Pet(); // this will use the default constructor and set the name and weight to null and 0, respectively.

Once you make these changes, your code should work correctly. Remember to always test your code and look for any errors or exceptions that may occur. Good luck!
 

Related to How can I fix my Pet class to work with the given PetDriver class?

1. What does the "Cannot Figure out this method" error mean?

The "Cannot Figure out this method" error typically means that there is a problem with the syntax or logic of a specific method in your code. It could be due to a misspelling, missing parentheses, or incorrect parameters being passed.

2. How can I fix the "Cannot Figure out this method" error?

To fix this error, you will need to carefully review the code and check for any syntax errors or incorrect logic. Make sure all method names are spelled correctly and all parentheses and brackets are properly placed. If you are still unable to resolve the error, try looking for similar examples or consult with other programmers for assistance.

3. Why am I getting the "Cannot Figure out this method" error?

This error can occur for a variety of reasons, including misspelled method names, incorrect syntax, or passing the wrong parameters into a method. It could also indicate a logic error in the code. Thoroughly reviewing and debugging your code is the best way to determine the specific cause of the error.

4. Can I ignore the "Cannot Figure out this method" error?

No, it is not recommended to ignore this error. It is a clear indication that there is a problem with your code that needs to be addressed. Ignoring the error could lead to unexpected behavior or other errors later on in your program.

5. How can I prevent the "Cannot Figure out this method" error from occurring?

The best way to prevent this error is to carefully review your code and double-check for any syntax or logic errors before running it. It can also be helpful to break down your code into smaller, manageable chunks and test them individually to catch any errors early on. Additionally, using proper naming conventions and commenting your code can help prevent this error from occurring.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
3
Views
810
Back
Top