Question
What is wrong with this code? The assignment question is: This assignment involves writing a program that models a dog. A dog has a name,
What is wrong with this code?
The assignment question is: This assignment involves writing a program that models a dog. A dog has a name, a breed (which may be unknown), a gender (male or female), an age (in months), and a weight (in pounds). Anda dog may or may not have fleas. Dog behaviors include eating ( chomp chomp chomp ), barking ( woof woof woof ), and scratching fleas if a dog has any ( you can determine what this sound should be). Design a Dog class that models the attributes and behaviors described above. Provide appropriate class constructors, getter methods, setter methods, and any other methods you think are necessary to model a dog. When a dog object is created, in wont have fleas unless you specify otherwise, so youll need to use multiple constructors. Write a main method that creates at least two dogs and invokes their behaviors and displays their attributes.
This is the code:
Dog.java
public class AssignmentTen {
//strings for the properties of a dog private String name,breed,gender; private int age; private double weight; private boolean fleas; public AssignmentTen() { super(); }
//first constructor with all attributes of the dog public AssignmentTen(String name, String breed, String gender, int age, double weight, boolean fleas) { super(); this.name = name; this.breed = breed; this.gender = gender.toUpperCase(); this.age = age; this.weight = weight; this.fleas = fleas; } //second constructor public AssignmentTen(String name, String breed, String gender, int age, double weight) { super(); this.name = name; this.breed = breed; this.gender = gender.toUpperCase(); this.age = age; this.weight = weight; }
//getters and setters for name field public String getName() { return name; }
public void setName(String name) { this.name = name; }
//breed - getters and setters public String getBreed() { return breed; }
public void setBreed(String breed) { this.breed = breed; }
//gender - getters and setters public String getGender() { return gender; }
public void setGender(String gender) { this.gender = gender.toUpperCase(); }
//age - getters and setters public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
//weight - getters and setters public double getWeight() { return weight; }
public void setWeight(double weight) { this.weight = weight; }
//fleas - getters and setters public boolean hasFleas() { return fleas; }
public void setFleas(boolean fleas) { this.fleas = fleas; } //Behavior methods //barking method public void doBark() { System.out.println("chomp...chomp...chomp...."); } //scratching fleas method public void doScratchFleas() { if(hasFleas()) System.out.println("tick...tick...tick.."); else System.out.println("What!...No fleas!"); } //eating method public void doEat() { System.out.println("woof...woof...woof..."); }
@Override public String toString() { return "Dog [name=" + name + ", breed=" + breed + ", gender=" + gender + ", age=" + age + ", weight=" + weight + ", fleas=" + fleas + "]"; }
public static void main(String[] args) { //creating dog objects Dog dog1 = new Dog("Bobby", "Poodle", "male",13, 45,true); //with fleas Dog dog2 = new Dog("Tina", "Pug", "female",9, 26); //without fleas System.out.println(dog1); dog1.doBark(); dog1.doEat(); dog1.doScratchFleas(); System.out.println(" "); System.out.println(dog2); dog2.doBark(); dog2.doEat(); dog2.doScratchFleas(); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started