Question

In: Computer Science

1. Lets construct a Dog class 2.Declare name, breed and weight as their private variables 3.Add...

1. Lets construct a Dog class

2.Declare name, breed and weight as their private variables

3.Add a default constructor

4.Write a constructor with name, breed and weight as arguments

5.Add a toString method to the class

6.Add a equals method

7.Add a copy contructor

8.Add a bark method in Dog class

•This method checks if the weight is greater than 25 , then print (dog.name says woof!) else (dog.name say yip!)

Solutions

Expert Solution

// 1. Lets construct a Dog class
public class Dog {

    // 2.Declare name, breed and weight as their private variables
    private String name;
    private String breed;
    private int weight;

    // 3.Add a default constructor
    public Dog() {
    }

    // 4.Write a constructor with name, breed and weight as arguments
    public Dog(String name, String breed, int weight) {
        this.name = name;
        this.breed = breed;
        this.weight = weight;
    }

    // 5.Add a toString method to the class
    @Override
    public String toString() {
        return "name='" + name + '\'' + ", breed='" + breed + '\'' + ", weight=" + weight;
    }

    // 6.Add a equals method
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Dog dog = (Dog) o;
        return weight == dog.weight && name.equals(dog.name) && breed.equals(dog.breed);
    }

    // 7.Add a copy contructor
    public Dog(Dog dog) {
        this.name = dog.name;
        this.breed = dog.breed;
        this.weight = dog.weight;
    }

    // 8.Add a bark method in Dog class
    public void bark() {
        // This method checks if the weight is greater than 25 , then print (dog.name says woof!) else (dog.name say yip!)
        if (weight > 25) {
            System.out.println(name + " says woof!");
        } else {
            System.out.println(name + " says yip!");
        }
    }

    // Testing the class here. remove this method if not required.
    public static void main(String[] args) {
        Dog dog = new Dog("D", "B", 30);
        dog.bark();
    }
}


Related Solutions

1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class:...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class: • An instance variable called wheelsCount of type int • An instance variable called vType of type String • An instance variable called isTruck of type boolean .  Add getters and setters for the three instance variables  Add the following methods to the Account class: • A void method called initialize that takes a parameter of type int, a String,and one double...
Here is class Dog -String name; -int age -String breed; -boolean adopted; //false if available …………………...
Here is class Dog -String name; -int age -String breed; -boolean adopted; //false if available ………………… +constructor, getters, setters, toString() Write a lambda expression showAdoptable using a standard functional interface that will display the dog’s name age and breed if adopted is false. Help please!!
myLinkedList.java>>>>>>>> public class MyLinkedList implements MiniList<Integer>{ /* Private member variables that you need to declare: **...
myLinkedList.java>>>>>>>> public class MyLinkedList implements MiniList<Integer>{ /* Private member variables that you need to declare: ** The head pointer ** The tail pointer */ public class Node { // declare member variables (data and next) // finish these constructors public Node(int data, Node next) {} public Node(int data) {} // HINT: use this() with next = null } // Initialize the linked list (set head and tail pointers) public MyLinkedList() {} @Override public boolean add(Integer item) { return false; }...
follow pseudo 1) Create class called Node Declare private integer called data (or any other name)...
follow pseudo 1) Create class called Node Declare private integer called data (or any other name) Declare private Node called link (or any other name) 2) Declare constructor Should be public (ex: Node) where: link is equal to null data is equal to zero 3) Declare another constructor public Node with parameters integer d, Node n where: data is equal to d link is equal to n 4) Declare function to set link to next Node link equal to n...
1. Create a console program in C#, * Create a class: "Student.cs" * Add 3 variables:...
1. Create a console program in C#, * Create a class: "Student.cs" * Add 3 variables: StudentName (string), SchoolYear (int), YearsUntilGraduation(int) * Method YTK() = 12 - SchoolYear; 2. Main *Enter name *Enter age *You will attend school:____ years before graduating.
# List the two private member variables (including name and functionality) in the node class. #Write...
# List the two private member variables (including name and functionality) in the node class. #Write a general pattern for a loop statement that traverses all the nodes of a linked list
Create a class DogWalker member List <String>doggies method: void addDog(String name ) //add dog to the...
Create a class DogWalker member List <String>doggies method: void addDog(String name ) //add dog to the List method: void printDogList() //print all dogs in list /* You’ll need an index. Iterate over your list of dog names in a while loop. Use your index to “get” the dog at the current index When you ‘find’ your dog name in the list, print it out */ Method: void findDogUsingWhile(String dogName) /* You’ll need an index. Iterate over your list of dog...
1.For Python Which statement(s) are true regarding private class variables? private variables can be accessible inside...
1.For Python Which statement(s) are true regarding private class variables? private variables can be accessible inside the class. private variables can be accessible outside the class. private variable values can be changed directly outside of the class private variables can be changed outside the class through set methods. 2. In the following code, def A: def __init__(self):      __a = 1      self.__b = 1      self.c = 1      __d__ = 1 # Other methods omitted Which of the following is a private...
Make Animal an abstract class. Create a Kennel Class Create 2-3 Dog objects Create 2-3 Cat...
Make Animal an abstract class. Create a Kennel Class Create 2-3 Dog objects Create 2-3 Cat objects Put your Dog and Cat objects in an Array of Animals Loop over your Animals and print the animal with Species, Age, and the status of the appropriate vaccines. Using the code below: public class Animal { //Declaring instance variables private int age; private boolean RabiesVaccinationStatus; private String name; private String ownerName; //Zero argumented constructor public Animal() { } //Parameterized constructor public Animal(int...
Java Create an abstract Product Class Add the following private attributes: name sku (int) price (double)...
Java Create an abstract Product Class Add the following private attributes: name sku (int) price (double) Create getter/setter methods for all attributes Create a constructor that takes in all attributes and sets them Remove the default constructor Override the toString() method and return a String that represents the building object that is formatted nicely and contains all information (attributes) Create a FoodProduct Class that extends Product Add the following private attributes: expDate (java.util.Date) for expiration date refrigerationTemp (int) if 70...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT