Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 3: Inheritance and polymorphism 18 points total; pair-optional This is the only problem in this assignment that you may complete with a partner. See

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Problem 3: Inheritance and polymorphism 18 points total; pair-optional This is the only problem in this assignment that you may complete with a partner. See the rules for working with a partner on pair-optional problems for details about how this type of collaboration must be structured. Imagine that you have a set of related classes that have been defined using inheritance. Here are the key facts about these classes: Class Gee doesn't explicitly extend a class (i.e., it doesn't have an extends clause in its class header). Its class members (i.e., its fields and methods) include: an integer field called c a string field called d a non-static method called how() that takes no inputs and returns a string a non-static method called where() that takes a string and returns an integer its own toString() method that overrides the inherited one. Class Zee extends Gee. In addition to the members that it inherits, it has: a string field called a its own method called how() that overrides the inherited one its own method called why() that takes two integers and returns a double its own equals() method that overrides the inherited one. Class Tee extends Zee. In addition to the members that it inherits, it has: a string field called x its own method called why() that overrides the inherited one. its own toString() method that overrides the inherited one. Class Yee extends Gee. In addition to the members that it inherits, it has: . integer fields called e and f its own method called where() that overrides the inherited one its own equals() method that overrides the inherited one. In addition, you should make the following assumptions: All of the classes employ appropriate encapsulation. Each class has a constructor that takes no parameters and initializes the newly created object's fields with their default values. Each class includes an appropriate accessor method for each field that is declared in that class. For example, the Gee class would have accessor methods called getc) and getD(). Each class includes an appropriate mutator method for each field that is declared in that class. For example, the Gee class would have mutator methods called set() and setD(). Answer the following questions in light of the above information about these classes. Before you begin, you may find it helpful to draw an inheritance hierarchy for the classes, although doing so is not required. 1. (2 points) The information above states that the Gee class has its own toString() method that overrides the inherited one. Where does the toString() method that Gee overrides come from? Be as specific as possible, and explain your answer briefly. 2. (2 points) List all of the fields in a Tee object - both the ones that it declares and the ones (if any) that it inherits. (Note that in our use of the terminology, all of the fields in a superclass are inherited by its subclasses including the private ones.) 3. (5 points) Consider the following code fragment: Tee t1 = new Tee(); System.out.println(t1.why(5, 10)); System.out.println(t1.how()); System.out.println(t1.where("Waldo")); System.out.println("my Tee is" + t1); System.out.println(t1.equals(t1)); Each of the println statements in this code fragment displays the result of a method call. (This includes the fourth println statement, in which the Java interpreter calls a method on our behalf.) However, it is possible that one or more of these methods calls would fail to compile because the necessary method is neither defined in the Tee class nor inherited from a superclass of Tee. In section 3-3 of ps3_parti, we have included a table that you should complete with appropriate information about each of these method calls. We have filled in the first row for you, and you should complete the remaining rows. 4. (3 points) Now imagine that you want to add a non-static method to the Yee class. It should be called total(), it should take no parameters, and it should return the sum of all of the integer fields in the called Yee object. Add a full definition of that method to section 3-4 of your ps3_partI file. Make sure to take into account the fact that the classes employ proper encapsulation. 5. (6 points) For each of the following assignment statements, indicate whether it would be allowed according to the rules of polymorphism, and explain briefly why or why not. a. Geeg = new Tee(); b. Teet = new Zee(); c. Zee z = new Yee(); d. Object o = new Zee(); 3-3) which println statement? which method is called? will the call if the call compiles, compile which version of the (yeso?) method will be called? first one why() yes the Tee version second one third one fourth one fifth one Problem 3: Inheritance and polymorphism 18 points total; pair-optional This is the only problem in this assignment that you may complete with a partner. See the rules for working with a partner on pair-optional problems for details about how this type of collaboration must be structured. Imagine that you have a set of related classes that have been defined using inheritance. Here are the key facts about these classes: Class Gee doesn't explicitly extend a class (i.e., it doesn't have an extends clause in its class header). Its class members (i.e., its fields and methods) include: an integer field called c a string field called d a non-static method called how() that takes no inputs and returns a string a non-static method called where() that takes a string and returns an integer its own toString() method that overrides the inherited one. Class Zee extends Gee. In addition to the members that it inherits, it has: a string field called a its own method called how() that overrides the inherited one its own method called why() that takes two integers and returns a double its own equals() method that overrides the inherited one. Class Tee extends Zee. In addition to the members that it inherits, it has: a string field called x its own method called why() that overrides the inherited one. its own toString() method that overrides the inherited one. Class Yee extends Gee. In addition to the members that it inherits, it has: . integer fields called e and f its own method called where() that overrides the inherited one its own equals() method that overrides the inherited one. In addition, you should make the following assumptions: All of the classes employ appropriate encapsulation. Each class has a constructor that takes no parameters and initializes the newly created object's fields with their default values. Each class includes an appropriate accessor method for each field that is declared in that class. For example, the Gee class would have accessor methods called getc) and getD(). Each class includes an appropriate mutator method for each field that is declared in that class. For example, the Gee class would have mutator methods called set() and setD(). Answer the following questions in light of the above information about these classes. Before you begin, you may find it helpful to draw an inheritance hierarchy for the classes, although doing so is not required. 1. (2 points) The information above states that the Gee class has its own toString() method that overrides the inherited one. Where does the toString() method that Gee overrides come from? Be as specific as possible, and explain your answer briefly. 2. (2 points) List all of the fields in a Tee object - both the ones that it declares and the ones (if any) that it inherits. (Note that in our use of the terminology, all of the fields in a superclass are inherited by its subclasses including the private ones.) 3. (5 points) Consider the following code fragment: Tee t1 = new Tee(); System.out.println(t1.why(5, 10)); System.out.println(t1.how()); System.out.println(t1.where("Waldo")); System.out.println("my Tee is" + t1); System.out.println(t1.equals(t1)); Each of the println statements in this code fragment displays the result of a method call. (This includes the fourth println statement, in which the Java interpreter calls a method on our behalf.) However, it is possible that one or more of these methods calls would fail to compile because the necessary method is neither defined in the Tee class nor inherited from a superclass of Tee. In section 3-3 of ps3_parti, we have included a table that you should complete with appropriate information about each of these method calls. We have filled in the first row for you, and you should complete the remaining rows. 4. (3 points) Now imagine that you want to add a non-static method to the Yee class. It should be called total(), it should take no parameters, and it should return the sum of all of the integer fields in the called Yee object. Add a full definition of that method to section 3-4 of your ps3_partI file. Make sure to take into account the fact that the classes employ proper encapsulation. 5. (6 points) For each of the following assignment statements, indicate whether it would be allowed according to the rules of polymorphism, and explain briefly why or why not. a. Geeg = new Tee(); b. Teet = new Zee(); c. Zee z = new Yee(); d. Object o = new Zee(); 3-3) which println statement? which method is called? will the call if the call compiles, compile which version of the (yeso?) method will be called? first one why() yes the Tee version second one third one fourth one fifth one

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Explain consumer behaviour.

Answered: 1 week ago

Question

Explain the factors influencing consumer behaviour.

Answered: 1 week ago

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago