The Cat and Dog class below implement the Animal interface. All animals have names, but only dogs do tricks. Implement the describe method, using the instanceof operator to test whether the given animal is a dog. If so, return the name, a space, and the trick that the dog can do, such as "Helmut barks at the moon". If it is any other kind of animal, return the name, a space, and "has no tricks".
public class Animals
{
/**
Describes the given animal.
@param pet an animal
@return a string with the animal's name, a space, and
either the trick that the animal knows (if it is a dog) or a
string "has no tricks"
*/
public static String describe(Animal pet)
{
// TODO: Complete this method
}
// This method is used to check your work
public static String check(String name, String trick)
{
Animal pet;
if (trick == null) pet = new Cat(name);
else pet = new Dog(name, trick);
return describe(pet);
}
Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 1 images
- Create a new project for this program called TestOldMaid and add a class with a main() method. In the project: Copy your Deck and Card class from the earlier project into it. Create a subclass of Deck called OldMaidDeck. It is special because one of the Queens is missing so it only has 51 cards. Create a constructor method that calls the super class constructor, then removes a queen. Override the toString method so it returns the name of the deck and the number of cards in it. Write the test main() method. Create an OldMaidDeck object and deal all the cards to six players. It is ok if not everyone has an equal number of cards. Use arrays or ArrayLists for the players hands. Show the hands of all 6 players. Refer to the web to find out more about the Old Maid card game. Fully document all classes with your name, date and description. And each data member and method is documented. Each block that does something is also documented. Without removing the Queensarrow_forwardCreate a RobotFriend class. RobotFriend is a new toy just on the market. Copy the starter class and the tester from Codecheck. A RobotFriend has a name and replies to various commands Provide a constructor that takes the name of the RobotFriend as a parameter. Remember that a constructor has the same name as the class. What does the class need to remember? That is the instance variable. Provide the following methods: public String getName() gets the name of this RobotFriend public void setName(String newName) sets a new name for this RobotFriend public String whatDoYouNeed() returns a string consisting of name + " needs a battery charge" where name is the name of this RobotFriend (which was supplied in the constructor) public String doCommand(String whatToDo) returns a string consisting of "Your friend " + name + " does not " + whatToDo where name is the name of this RobotFriend and whatToDo is the parameterarrow_forwardSally and Harry implement two different compareTo methods for a classthey are working on together. Sally’s compareTo method returns −1, 0, or +1,depending on the relationship between two objects. Harry’s compareTo methodreturns −6, 0, or +3. Which method is suitable?arrow_forward
- Below for each class you find a UML and description of the public interface. Implementing the public interface as described is madatory. There's freedom on how to implement these classes.The private properties and private methods are under your control.. There are multiple ways of implementing all these classes. Feel free to add private properties and methods. For each object, it's mandatory to create a header file (.h), implementation file (.cpp) and a driver. Blank files are included. The header should have the class definition in it. The implementation file should contain the implementations of the methods laid out in the header fine. And finally the Driver should test/demonstrate all the features of the class. It's best to develop the driver as the class is being written. Check each section to see if there are added additional requirements for the driver. Two test suites are included so that work can be checked. It's important to implement the drivers to test and demonstrate…arrow_forwardDraw the UML diagram for the class. Implement the class. Write a test program that creates an Account object with an account ID of 1122, a balance of $20,000, and an annual interest rate of 4.5%. Use the withdraw method to withdraw $2,500, use the deposit method to deposit $3,000, and print the balance, the monthly interest, and the date when this account was created.arrow_forwardTake the tax program from the last homework assignment and implement it using classes and objects instead. To do so, create a class definition for a class called Customer with attributes income and tax. It should also have a set method for income, and a calcTax() method to assign tax and return it. As a reminder, the tax is calculated as follows: Income Tax Due $0 - $50,000 5% $50,000-$100,000 $2,500 + 10% of (income > $50,000) > $100,000 $7,500 + 15% of (income > $100,000) Create a Customer object in main, have the user enter the income and assign this to the income variable of the Customer object, and then call the calcTax() method for the Customer object and print the tax due.arrow_forward
- In this exercise, we are going to model some behaviors of a square. Since the Square object extends the Rectangle object, we see that a lot of the information we need is stored in the superclass and we will need to access it using the super keyword. Your job is to complete the Square class, as specified within the class. Upon completion, thoroughly test out your code using the SquareTester class. ----------------------------------- public class SquareTester{public static void main(String[] args){// Start here!}} ----------------------------------- public class Rectangle { private double width;private double height; public Rectangle(double w, double h){width = w;height = h;} public double getWidth(){return width;} public void setWidth(double w){width = w;} public double getHeight(){return height;} public void setHeight(double h){height = h;} public String toString(){return "Rectangle with width " + width + " and height " + height;}} ----------------------------------- public class…arrow_forwardCreate the class Suitcase. Suitcase has things and a maximum weight limit, which defines the greatest total allowed weight of the things contained within the Suitcase object. Add the following methods to your class: A constructor, which is given a maximum weight limit public void addThing(Thing thing), which adds the thing in the parameter to your suitcase. The method does not return any value. public String toString(), which returns a string in the form "x things (y kg)" The things are saved into an ArrayList object: ArrayList<Thing> things = new ArrayList<Thing>(); The class Suitcase has to make sure the thing's weight does not cause the total weight to exceed the maximum weight limit. The method addThing should not add a new thing if the total weight happens to exceed the maximum weight limit. Below, you find an example of how the class can be used: public class Main { public static void main(String[] args) { Thing book = new Thing("Happiness…arrow_forwardThe goal of this coding exercise is to create two classes BookstoreBook and LibraryBook. Both classes have these attributes: author: Stringtiltle: Stringisbn : String- The BookstoreBook has an additional data member to store the price of the book, and whether the book is on sale or not. If a bookstore book is on sale, we need to add the reduction percentage (like 20% off...etc). For a LibraryBook, we add the call number (that tells you where the book is in the library) as a string. The call number is automatically generated by the following procedure:The call number is a string with the format xx.yyy.c, where xx is the floor number that is randomly assigned (our library has 99 floors), yyy are the first three letters of the author’s name (we assume that all names are at least three letters long), and c is the last character of the isbn.- In each of the classes, add the setters, the getters, at least three constructors (of your choosing) and override the toString method (see samplerun…arrow_forward
- Create a Class Student in which we have three instance variables emp_name, emp_id, and Salary. Write getter and setters for each instance variable. Write another Display method that displays the record of Employee. In Main create five Employee’s Objects. Set their values as required and display as well. In the end Display top three employees with respect to Salary (Display those who have more Salary among all).arrow_forwardWrite a class Die representing a single die. Your class should include: An instance variable for the number of sides on the die. A constructor that allows you to specify the number of sides on the die. This method shouldcheck that there are at least 2 sides; if not, it should default to having 6 sides. A toString method that returns a description of the die, including its number of sides. A roll method that randomly returns one of the numbers on the die. Each number should have an equal probabilityof being returned. Assume that the numbers on the die range from 1 to the number of sides. A testDie method that takes one parameter for the number of times to roll the die. This method should roll the diethat number of times, keeping track of the results. Once all rolls are complete, the method should display statisticson how many times each number appeared, and the corresponding percentage. Example: Die myD4 = new Die(4); myD4.testDie(100000);might result in something like: Results from…arrow_forwardOverride the testOverriding() method in the Triangle class. Make it print “This is theoverridden testOverriding() method”. (add it to triangle class don't make a new code based on testOverriding in SimpleGeometricObject class.) (this was the step before this one) Add a void testOverriding() method to the SimpleGeometricObject class. Make it print“This is the testOverriding() method of the SimpleGeometricObject class”arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY