Question

Equals and Copy method activity Implement a Dog class with the following features: + Attributes: age, gender, weight + Method

Java
0 0
Add a comment Improve this question Transcribed image text
Answer #1


public class Dog {
  
   int age;
   char gender;
   double weight;
   public Dog(int age, char gender, double weight) {
       this.age = age;
       this.gender = gender;
       this.weight = weight;
   }
   public int getAge() {
       return age;
   }
   public void setAge(int age) {
       this.age = age;
   }
   public char getGender() {
       return gender;
   }
   public void setGender(char gender) {
       this.gender = gender;
   }
   public double getWeight() {
       return weight;
   }
   public void setWeight(double weight) {
       this.weight = weight;
   }
   public Dog(Dog other)
   {
       this.setAge(other.getAge());
       this.setGender(other.getGender());
       this.setWeight(other.getWeight());
   }
   public Dog()
   {}
  
  
   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       Dog other = (Dog) obj;
       if (age != other.age)
           return false;
       if (gender != other.gender)
           return false;
       if (Double.doubleToLongBits(weight) != Double.doubleToLongBits(other.weight))
           return false;
       return true;
   }
   @Override
   public String toString() {
       return "Dog [age=" + age + ", gender=" + gender + ", weight=" + weight + "]";
   }
   public static void main(String[] args)
   {
       Dog[] array = new Dog[5];
       array[0] = new Dog(3,'m',23.3);
       array[1] = new Dog(4,'f',29.7);
       array[2] = new Dog(3,'m',23.3);
       array[3] = new Dog(5,'f',25.7);
       array[4] = new Dog(6,'m',43.3);
      
       for(int i= 0 ;i<array.length;i++)
       {
           for(int j = i+1;j<array.length;j++)
           {
               if(array[i].equals(array[j]))
                   System.out.println(array[i]+ " and "+ array[j]+ " are equal");
           }
       }  
   }
  
  

}


<terminated> Dog [Java Application] C:\Program Files Java\dk1.8.0_112\ bin javaw.exe (Mar 24, 2019, 11:00:32 PM) Dog [age-3,

Add a comment
Know the answer?
Add Answer to:
Equals and Copy method activity Implement a Dog class with the following features: + Attributes: ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

  • Part I: Create a program class named TestArrays This class should have a main() method that...

    Part I: Create a program class named TestArrays This class should have a main() method that behaves as follows: Create an integer array of size 10. Using a loop, fill the elements with increments of 5, starting with 5 in the first element. Using the array elements, sum the second, fifth and seventh elements. Display the sum with a label. Change the fourth element to 86. Subtract 9 from the ninth element. Using a loop, display the elements of the...

  • Chapter 5 Assignment Read directions: In JAVA design and implement a class called Dog that contains...

    Chapter 5 Assignment Read directions: In JAVA design and implement a class called Dog that contains instance data that represents the dog's name and dog's age. Define the Dog constructor to accept and initialize instance data. Include getter and setter methods for the name and age. Include a method to compute and return the age of the dog in "person years" (seven times age of dog. Include a toString method that returns a one-time description of the dog (example below)....

  • programming in JAVA,, Declare a class Dog, objects of which have attributes age and name. The...

    programming in JAVA,, Declare a class Dog, objects of which have attributes age and name. The class has one constructor that initializes both attributes. Dog class has method bark. Objects of Dog respond to bark method by printing of "HOOF" in case when dog object's age is > 5 and "HOOF,  HOOF ,  HOOF " if the age is less than 5.

  • Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and...

    Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and name variables (do not include topTrick). Note: The type refers to what the breed typically does; for example, a corgi would be a “cattle herding dog.” A Shiba Inu would be a “hunting dog.” Create the setTopTrick() mutator method Dog is parent class Corgi and Driver are subclasses Complete the Corgi class: Using the UML Class diagram, declare the instance variables. Create the two...

  • The object class should be a Student with the following attributes: id: integer first name: String...

    The object class should be a Student with the following attributes: id: integer first name: String last name: String write the accessors, mutators, constructor, and toString(). In your main test class you will write your main method and do the following things: Create an array of Student objects with at least 5 students in your array. Write a sort method that must be your original work and included in the main class. The sort method will sort based on student...

  • create a class in Java for a Plane the class will include String Data Fields for...

    create a class in Java for a Plane the class will include String Data Fields for the make, model, and year. (as well as mutators and accessors) Defined constructor and copy constructor. there must be a toString method as well as a copy method. runner program the program will create an array for Plane class. The size of the array will be set to user input. then the user will input the information into each plane object in the array....

  • Java assignment. There are five classes. You figure out the inheritance hierarchy. Animal, Dog, Brindle, Cat,...

    Java assignment. There are five classes. You figure out the inheritance hierarchy. Animal, Dog, Brindle, Cat, Driver Note: Brindle is a kind of dog that has stripes The following classes will have a makeSound method that displays the kind of sound the animal makes. Animal, Dog, Cat In the Brindle class, you will have an instance variable noOfStripes One of the classes should have an instance variable to hold the name of the animal Name One of the classes should...

  • In Java* Please implement a class called "MyPet". It is designed as shown in the following...

    In Java* Please implement a class called "MyPet". It is designed as shown in the following class diagram. Four private instance variables: name (of the type String), color (of the type String), gender (of the type char) and weight(of the type double). Three overloaded constructors: a default constructor with no argument a constructor which takes a string argument for name, and a constructor with take two strings, a char and a double for name, color, gender and weight respectively. public...

  • Using your Dog class from earlier this week, complete the following: Create a new class called...

    Using your Dog class from earlier this week, complete the following: Create a new class called DogKennel with the following: Instance field(s) - array of Dogs + any others you may want Contructor - default (no parameters) - will make a DogKennel with no dogs in it Methods: public void addDog(Dog d) - which adds a dog to the array public int currentNumDogs() - returns number of dogs currently in kennel public double averageAge() - which returns the average age...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT