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...

  • 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...

  • In Java Create a class Worker. Your Worker class should include the following attributes as String...

    In Java Create a class Worker. Your Worker class should include the following attributes as String variables: Name, Worker ID, Worker Address, Worker City, Worker State Create the constructor that initializes the above worker attributes. Then make another class name HourlyWorker that inherits from the Worker class.   HourlyWOrker has to use the inherited parent class variables and add in hourlySalary and billableHours. Your HourlyWorker class should contain a constructor that calls the constructor from the Worker class to initialize the...

  • 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...

  • Write a class Store which includes the attributes: store name, city. Write another class encapsulating an...

    Write a class Store which includes the attributes: store name, city. Write another class encapsulating an Art Gallery, which inherits from Store. An Art Gallery has the following additional attributes: how many paintings are sold every year and the number of artists submitting artwork. Code the constructor, accessors, mutators, toString and equals method of the super class Store. Code the constructor, accessors and mutators for the subclass Art Gallery. In the Art Gallery class, also code a method returning the...

  • In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program...

    In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...

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
ADVERTISEMENT