Question

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:

  1. Using the UML Class diagram, declare the instance variables.
  2. Create the two mutator methods for the instance variables.

Driver is subclass

then create the code:

  1. There should be no instance variables.
  2. The main() method will be the only method in the class.
  3. Write three lines of code in the main() method:
    1. Instantiate a corgi object using the below syntax:

className objectName = new className(input parameters)

  1. TIP: Refer to the constructors in the Dog and Corgi classes to ensure the input parameters are correct.
  2. Use the objectName.setTopTrick() method to set a top trick for the dog you created.
  3. Embed the objectName.toString() method in a statement that outputs to the console window.

Sample output:

DOG DATA

Java is a Pembroke Welsh Corgi, a cattle herding dog.

The top trick is: ringing the bell to go outside.

The Corgi is 5 years old and weighs 38 pounds.

Dog

type: string
breed: string
name: string

topTrick: string

setTopTrick(trick:string)
toString()

Corgi

weight:int
age:int

setWeight(pounds:int)
setAge(years:int)
toString()

Arrow pointing from table labeled Corgi to table labeled Dog

1st program : Parent Dog

public class Dog {

    // instance variables

    // constructor

    // methods

    // method used to print Dog information

    public String toString() {

        String temp = "\nDOG DATA\n" + name + " is a " + breed +

                ", a " + type + " dog. \nThe top trick is : " +

                topTrick + ".";

        return temp;

    }

}

2nd program Corgi

public class Corgi extends Dog {

    // additional instance variables

    // constructor

    public Corgi(String type, String breed, String name, int pounds, int years) {

        // invoke Dog class (super class) constructor

        super(type, breed, name);

        weight = pounds;

        age = years;

    }

    // mutator methods

    // override toString() method to include additional dog information

    @Override

    public String toString() {

        return (super.toString() + "\nThe Corgi is " + age +

                " years old and weighs " + weight + " pounds.");

    }

}

3rd program Driver (CockerSpaniel)

public class Driver extends Dog {

    // additional instance variables

    // constructor

    public Driver(String type, String breed, String name, int pounds, int years) {

        // invoke Dog class (super class) constructor

        super(type, breed, name);

        weight = pounds;

        age = years;

    }

    // mutator methods

    // override toString() method to include additional dog information

    @Override

    public String toString() {

        return (super.toString() + "\nThe Cocker Spaniel is " + age +

                " years old and weighs " + weight + " pounds.");

    }

}

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

SOURCE CODE IN JAVA:

Dog.java

public class Dog

{

// instance variables

private String type, breed, name, topTrick;

// constructor

public Dog(String type, String breed, String name)

{

this.type=type;

this.breed=breed;

this.name=name;

}

// methods

void setTopTrick(String trick)

{

this.topTrick=trick;

}

// method used to print Dog information

public String toString() {

String temp = "\nDOG DATA\n" + name + " is a " + breed +", a " + type + " dog. \nThe top trick is : " +topTrick + ".";

return temp;

}

}

Corgi.java

public class Corgi extends Dog

{

// additional instance variables

private int weight, age;

// constructor

public Corgi(String type, String breed, String name, int pounds, int years)

{

// invoke Dog class (super class) constructor

super(type, breed, name);

weight = pounds;

age = years;

}

// mutator methods

public void setWeight(int pounds)

{

this.weight=pounds;

}

public void setAge(int years)

{

this.age=years;

}

// override toString() method to include additional dog information

@Override

public String toString()

{

return (super.toString() + "\nThe Corgi is " + age +" years old and weighs " + weight + " pounds.");

}

}

Driver.java

public class Driver extends Dog

{

// additional instance variables

private int weight, age;

// constructor

public Driver(String type, String breed, String name, int pounds, int years)

{

// invoke Dog class (super class) constructor

super(type, breed, name);

weight = pounds;

age = years;

}

// mutator methods

public void setWeight(int pounds)

{

this.weight=pounds;

}

public void setAge(int years)

{

this.age=years;

}

// override toString() method to include additional dog information

@Override

public String toString()

{

return (super.toString() + "\nThe Cocker Spaniel is " + age +" years old and weighs " + weight + " pounds.");

}

}

Main.java

class Main

{

public static void main(String[] args)

{

//testing the classes

Corgi c=new Corgi("cattle herding","Pembroke Welsh Corgi","Java",38,5);

c.setTopTrick("ringing the bell to go outside");

System.out.println(c);

}

}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and...
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
  • Could someone help me out. I am not sure what I should be doing. Seeing it...

    Could someone help me out. I am not sure what I should be doing. Seeing it worked out will allow me to understand what I should be doing and then I can complete it on my own. Usando 2. Complete the Dog Class: a. Using the UML Class diagram to the right declare the instance variables. A text version is available: UML Class Diagram Text Version b. Create a constructor that incorporates the type, breed, and name variables (do not...

  • Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and...

    Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and Bird 2.      A Bird class that is a descendant of Animal 3.      A Dog class that is a descendant of Animal 4.      A “driver” class named driver that instantiates an Animal, a Dog, and a Bird object. The Animal class has: ·        one instance variable, a private String variable named   name ·        a single constructor that takes one argument, a String, used to set...

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

  • 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 questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the...

    java questions: 1. Explain the difference between a deep and a shallow copy. 2. Given the below classes: public class Date {    private String month;    private String day;    private String year;    public Date(String month, String day, String year) {       this.month = month;       this.day = day;       this.year = year;    }    public String getMonth() {       return month;    }    public String getDay() {       return day;    }    public String...

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

  • PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1.    What is...

    PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1.    What is the output of the program as it is written? (Program begins on p. 2) 2. Why would a programmer choose to define a method in an abstract class (such as the Animal constructor method or the getName()method in the code example) vs. defining a method as abstract (such as the makeSound()method in the example)? /********************************************************************** *           Program:          PRG/421 Week 1 Analyze Assignment *           Purpose:         Analyze the coding for...

  • Create a super class called Store which will have below member variables, constructor, and methods Member...

    Create a super class called Store which will have below member variables, constructor, and methods Member variables: - a final variable - SALES_TAX_RATE = 0.06 - String name; /** * Constructor:<BR> * Allows client to set beginning value for name * This constructor takes one parameter<BR> * Calls mutator method setName to set the name of the store * @param name the name of the store */ /** getName method * @return a String, the name of the store */...

  • Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super...

    Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super class. The class includes four private String instance variables: first name, last name, social security number, and state. Write a constructor and get methods for each instance variable. Also, add a toString method to print the details of the person. After that create a class Teacher which extends the class, Person. Add a private instance variable to store number of courses. Write a constructor...

  • JAVA :The following are descriptions of classes that you will create. Think of these as service...

    JAVA :The following are descriptions of classes that you will create. Think of these as service providers. They provide a service to who ever want to use them. You will also write a TestClass with a main() method that fully exercises the classes that you create. To exercise a class, make some instances of the class, and call the methods of the class using these objects. Paste in the output from the test program that demonstrates your classes’ functionality. Testing...

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