Question

In Java please Create a class to represent a dog. Name the class “Dog”. It will...

In Java please

Create a class to represent a dog. Name the class “Dog”. It will contain three (3) class attributes, its bark, size, and cuteness.
The bark will be the sound of the dog’s bark, as in “woof!”. Its size will be how tall the dog is from the ground, and that number should always be between 6 and 44 inches. The cuteness is a string describing how cute the dog is, based on this scale:
“ugliest dog ever!”
“pretty ugly dog.”
“ugly dog”
“average”
“cute dog.”
“pretty cute dog.”
“most adorable dog ever!”
Ensure that all of the class variables are private and that they each have an accessor (getter) and a mutator (setter).
Include a default constructor method that takes no parameters but initializes a dog object with a bark sound of “Yelp!”, a size of 20, and a cuteness of “average.”.
Provide an overloaded constructor as well, that will take in three parameters and use those parameters to set all of the class’s fields.
Next, have a method called “UpdateBark” that will change the dog’s cuteness depending on its size, since large dogs can have intimidating deep barks while smaller dogs will have higher pitched and less frightening barks.
If the dog is larger than 16 inches, its cuteness should change in a negative fashion anytime the dog barks (big dogs tend to scare people when they bark). If it is smaller than that, the dog’s cuteness should change in a positive fashion. For example, a larger dog would change from “average.” to “ugly dog.” and a smaller dog would change from “pretty cute!” to “most adorable dog ever!”. The values that you change the cuteness to should come from the list of cuteness above.
We will also have a “toString” method, which will simply return the dog’s bark, size and cuteness.

Create two (2) instances of the “Dog” class in your “Main” method. One will be created using the default constructor and the other should be made using the overloaded constructor.  That means that you should pass the overloaded constructor arguments that come from user input.
Print out each of the dog’s three attributes using the accessors. Then call the “UpdateBark” method for both and print out their fields again, but this time through the “ToString” method.
Example (blue is default dog’s attributes, green is the user’s dog’s):
Creating a default dog…
Finished creating a default dog!
Default dog bark sound is Yelp!
Default dog size is 20 inches.
Default dog’s cuteness is cute dog.
Continued on third page…
Please enter the sound of your dog’s bark: “MOOOOOO!”
Please enter the size of your dog: “10”
Please enter the cuteness of your dog: “ugly dog.”
Your dog bark sound is MOOOOOO!
Your dog size is 10 inches.
Your dog’s cuteness is ugly dog.
Both dogs are doing a scary bark! Their cuteness has been affected!
Default dog bark sound is Yelp!
Default dog size is 20 inches.
Default dog’s cuteness is average.
Your dog bark sound is MOOOOOO!
Your dog size is 10 inches.
Your dog’s cuteness is average.

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

The answer to the given question is given below:

The Java code is given below:

import java.util.*;
/* Dog class */
public class Dog {
    /* instance variables */
    String bark;
    int size;
    String cuteness;

    /* Getters */
    public String getBark() {
        return bark;
    }

    public void setBark(String bark) {
        this.bark = bark;
    }

    public int getSize() {
        return size;
    }

    /* Setters */
    public void setSize(int size) {
        this.size = size;
    }

    public String getCuteness() {
        return cuteness;
    }

    public void setCuteness(String cuteness) {
        this.cuteness = cuteness;
    }

    /* Default constructor */
    public Dog() {
        this.bark = "Yelp!";
        this.size = 20;
        this.cuteness = "average.";
    }

    /* Parameterized constructor */
    public Dog(String bark, int size, String cuteness) {
        this.bark = bark;
        this.size = size;
        this.cuteness = cuteness;
    }

    /**
     * Method for returning the dog detail in the form of string
     * returning the string with comma separated values
     */
    public String ToString() {
        return ( bark + "," + size + "," + cuteness );
    }

    /**
     * Method for update dog bark
     * if dog size > 16 then change in negative fashion
     * if dog size < 16 then change the bark in positive fashion
    */
    public void UpdateBark() {
        switch (cuteness) {
            case "ugliest dog ever!":
                if(size < 16)
                    cuteness = "pretty ugly dog.";
                break;
            case "pretty ugly dog.":
                if(size < 16)
                    cuteness = "ugly dog.";
                else if(size > 16)
                    cuteness = "ugliest dog ever!";
                break;
            case "ugly dog.":
                if(size < 16)
                    cuteness = "average.";
                else if(size > 16)
                    cuteness = "pretty ugly dog.";
                break;
            case "average.":
                if(size < 16)
                    cuteness = "cute dog.";
                else
                    cuteness = "ugly dog.";
                break;
            case "cute dog.":
                if(size < 16)
                    cuteness = "pretty cute dog.";
                else if(size > 16)
                    cuteness = "average.";
                break;
            case "pretty cute dog.":
                if(size < 16)
                    cuteness = "most adorable dog ever!";
                else if(size > 16)
                    cuteness = "cute dog.";
                break;
            case "most adorable dog ever!":
                if(size > 16)
                    cuteness = "pretty cute dog.";
                break;
        }
    }

    /* Main function */
    public static void main(String[] args) {
        /**
         *  object with default values 
         */
        Dog defaultDog = new Dog();
        System.out.println(
                "Default dog bark sound is " + defaultDog.getBark() +
                "\nDefault dog size is " + defaultDog.getSize() + " inches." +
                "\nDefault dog’s cuteness is " + defaultDog.getCuteness() );
        
        int size;
        String bark, cuteness;
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the sound of your dog’s bark: ");
        bark = sc.nextLine();
        System.out.println("Please enter the size of your dog:");
        size = sc.nextInt();
        sc.nextLine();  // this is for ignoring newline character which is pressed just after inserting the size of the dog
        System.out.println("Please enter the cuteness of your dog:");
        cuteness = sc.nextLine();
        /**
         * object with values which are taken as input.
         */
        Dog dog = new Dog(bark, size, cuteness);
        System.out.println(
                "\nYour dog bark sound is " + dog.getBark() +
                "\nYour dog size is " + dog.getSize() + " inches." +
                "\nYour dog’s cuteness is " + dog.getCuteness() );

        System.out.println("\nCuteness has been affected.");
        /**
         * Updating dog's bark with default values
         */
        defaultDog.UpdateBark();
        String defaultDogInfo = defaultDog.ToString();
        String info[] = defaultDogInfo.split(","); // splitting and extracting the values
        System.out.println(
                "\nDefault dog bark sound is " + info[0] +
                "\nDefault dog size is " + info[1] + " inches." +
                "\nDefault dog’s cuteness is " + info[2] );
        /**
         * Updating dog object with input values
         */
        dog.UpdateBark();
        String dogInfo = dog.ToString();
        String data[] = dogInfo.split(",");
        System.out.println(
                "\nYour dog bark sound is " + data[0] +
                "\nYour dog size is " + data[1] + " inches." +
                "\nYour dog’s cuteness is " + data[2] );
    }
}

Sample Input:
MOOOOOO!
10
ugly dog.

Sample output:

Screenshot of the code is given below:

If the answer helped please upvote, it means a lot and for any query please comment.

Add a comment
Know the answer?
Add Answer to:
In Java please Create a class to represent a dog. Name the class “Dog”. It will...
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
  • Exercise #3: Create a class to represent a dog. Name the class “Dog”. It will contain...

    Exercise #3: Create a class to represent a dog. Name the class “Dog”. It will contain three (3) class attributes, its bark, size, and cuteness. The bark will be the sound of the dog’s bark, as in “woof!”. Its size will be how tall the dog is from the ground, and that number should always be between 6 and 44 inches. The cuteness is a string describing how cute the dog is, based on this scale: “ugliest dog ever!” “pretty...

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

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

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

  • Write Python code to create a class called Dog which represents a dog. The Dog class...

    Write Python code to create a class called Dog which represents a dog. The Dog class should have properties of breed (i.e. what type of dog it is), name and weight. The class should also have a constructor that takes these parameters to initialise the dog object, and a method called bark that prints out the word 'Woof!' to the screen. After you have defined the Dog class, create a Dog object called goodDog with a breed of 'terrier', a...

  • STORAGE WARS - Create a class with the name and variables/attributes listed below. Create a default...

    STORAGE WARS - Create a class with the name and variables/attributes listed below. Create a default Constructor with no parameters that assigns default values for each variable. Then, create an overloaded Constructor (with one parameter for each variable). Finally, create a method that returns the total number of items in the storage container. NOTE: this method must not print anything. (50 points) Make sure to clearly label the variables/attributes, Constructors and Methods and to indicate which language you are answering...

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • Project 1. Dog Door You are asked to create a dog door for a client. You...

    Project 1. Dog Door You are asked to create a dog door for a client. You are programming the remote that will do things such as open and close, etc. You must create both the program and write a white paper explaining your design • It should open (saying "The dog door is open.") and close (saying "The dog door is closed.). • It should take into account a dog going outside and coming back in; it should open when...

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

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