Question

In: Computer Science

java Goal: This lab will give you practice writing code that uses inheritance and Java interfaces....

java

Goal: This lab will give you practice writing code that uses inheritance and
Java interfaces.
Please make sure you have a partner for this lab.
No code is provided with this lab. Write code to experimentally resolve the
following questions about Java. You will need to show your code to the TA or
lab assistant.
Part I: Assignments and Casting (1 point)
------------------------------------------
Let Y be a subclass of X, and let y and x be variables of classes Y and X
respectively. From lecture, you know that the assignment "x = y" is valid, but
the assignment "y = (Y) x" requires a cast to compile, and will cause a
run-time error if x references an object that isn’t a Y.
What about arrays of objects? Suppose xa is an array of X’s, and ya is an
array of Y’s.
(a) At compile-time, can we assign xa to ya, and vice versa? When is a cast
required?
(b) At run-time, if ya references an array of Y’s, can we assign it to xa?
Can we then assign it back from xa to ya?
(c) If xa references an array of X’s (that are not Y’s), can we assign it to
ya? Can we then assign it back from ya to xa? Does it make a difference
if the array of type X[] references objects that are all of class Y? Why
do you think this is the case?
Part II: Conflicting Declarations (1 point)
--------------------------------------------
Suppose a subclass inherits a method implementation from a superclass, and
implements a Java interface (that’s the "interface" keyword) that contains
a method with the same name and prototype.
(a) Will Java compile the result?
(b) What if the method declaration in the interface has a different return
type?
(c) What if the method declaration in the interface has the same return type,
but a signature with a different parameter type?
(d) What if the method declaration in the interface has the same return type,
and the same number of parameters and parameter types, but those
parameters have different names?
Part III: More Conflicting Declarations (1 point)
--------------------------------------------------
Suppose a subclass inherits a "public static final" constant from a superclass,
and implements a Java interface that contains a "public static final" constant
with the same name.
(a) Will Java compile the result? Does it make any difference whether the
constant in the superclass and the constant in the interface have the
same value?
(b) Write a main() method in the subclass that accesses the constant using the
same name used in the superclass and the Java interface. Will Java
compile the result? Does it make any difference whether the constant in
the superclass and the constant in the interface have the same value?
(c) Figure out how to modify your main() method so that it accesses and prints
one of the two conflicting values. (Look to the Lecture 9 notes for
clues.) Make sure your code compiles and runs without errors.
Part IV: Method Overriding (1 point)
-------------------------------------
Consider a subclass that has a method that overrides a method with the same
prototype in its superclass.
(a) Define a variable whose static type is the subclass and which references
an object of the subclass. If we cast the variable to the superclass type
before calling the overridden method
((Superclass) subclassvariable).method();
does Java call the superclass method or the subclass method?
(b) Define a variable whose static type is the superclass and which references
an object of the superclass (but not the subclass). If we cast the
variable to the subclass type before calling the method, does Java call
the superclass method or the subclass method?
(c) Suppose you have an object whose class is the subclass. Can you figure
out a way to call the superclass method on that object without having to
go through the subclass method of the same name?
Check-off
---------
Show your TA or Lab Assistant your code and, in some cases, its output.
1 point: Give your answers for Part I. Show the code with which you answered
Part I (c).
1 point: Give your answers for Part II.
1 point: Give your answers for Part III. Show _and_run_ the code with which
you answered Part III (c).
1 point: Give your answers for Part IV. Show the code with which you
answered Part IV (c).
Because this lab is on the long side, students who complete the first three
parts but don’t have time to complete the fourth part will get 4 points if they
promise to figure out the last part later.

Solutions

Expert Solution

Part-1

Given Y is subcclass of X.

Y is super class, X is child class.

Given xa is array of X's and is ya is array of Y's.


a) we can assign
  
ya to xa, because it is upcasting and it is called implicitly.
  
So we can assign xa = ya;

But assigning xa to ya is a downcasting, so it requires a explicit casting.

ya = (Y[])xa;


b) Yes we can do it by explicit casting, if we have a back-up copy of array of Y's to assign back the array of Y's to ya..

c) Yes we can do it, if we have a back-up copy of array of X's. Here the casting will occur implicitly. No explicit casting is required.

Screenshot of the same is attached.


Part-2

Given that there is a interface, and a subclass is implementing the interface.

Let Interface be A

method prototype is

int getResult(int x, int y);

Let B is the subclass implementing the interface A.

a) Yes it will be compiled if the subclass implements the interface method if it has same return type , same name and same parameters. Parameter namses need not to be same.
b) If the method declaration has different return type then it will not be compiled. It is not considered as implemented method. And it will be compiled only after the interface method is implemented.
c) In this case also compilation fails because of difference in parameter type.
d) There will be no problem if there is difference in parameter names, it will be compiled.

Please ask remaining questions as another task

Feel free to ask any doubts, if you face any difficulty in understanding.

Please upvote the answer if you find it helpful


Related Solutions

JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code Classwork A zoo is developing an educational safari game with various animals. You will write classes representing Elephants, Camels, and Moose. Part A Think through common characteristics of animals, and write a class ZooAnimal. ☑ ZooAnimal should have at least two instance variables of different types (protected), representing characteristics that all animals have values for. ☑ It should also have at least two methods...
The goal of this part is to give you a chance to practice working through the...
The goal of this part is to give you a chance to practice working through the steps of calculating variance. Use the table of scores (X values) below to complete the following calculations. Assume the data in the table represents a sample, not a population. Use the extra columns in the table to show your work. X 20 22 18 15 21 Question 2.1 (0.5 points) – Calculate the deviation score for each x value Question 2.2 (0.5 points) –...
a java code In this lab you will implement an inorder traversal, and a search for...
a java code In this lab you will implement an inorder traversal, and a search for a 2-3-4 tree. The inorder traversal will print the contents (integers) of the tree, and the search method will return a boolean, indicating whether a given key was found in the tree. Examine the provided template. The main method is provided for you, as well as a template of the Node and 2-3-4 classes. The main method will read either "inorder" or an integer...
Writing a Modular Program in Java In this lab, you add the input and output statements...
Writing a Modular Program in Java In this lab, you add the input and output statements to a partially completed Java program. When completed, the user should be able to enter a year, a month, and a day to determine if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared for you....
For this lab you are going to practice writing method signatures, implementing if statements, manipulating Strings,...
For this lab you are going to practice writing method signatures, implementing if statements, manipulating Strings, and improve your skills with for loops. The methods you will write will be part of a short trivia game, feel free to try it out after you finish.   import java.util.Scanner; public class Trivia { //TODO: isLeapYear //TODO: isPrime //TODO: aWord //TODO: reverse public static void main(String[] args){ Scanner answers = new Scanner(System.in); int score = 0; System.out.println("What year is a Leap Year?"); //...
write this program in java... don't forget to put comments. You are writing code for a...
write this program in java... don't forget to put comments. You are writing code for a calendar app, and need to determine the end time of a meeting. Write a method that takes a String for a time in H:MM or HH:MM format (the program must accept times that look like 9:21, 10:52, and 09:35) and prints the time 25 minutes later. For example, if the user enters 9:21 the method should output 9:46 and if the user enters 10:52...
Question Objective: The objective of this lab exercise is to give you practice in programming with...
Question Objective: The objective of this lab exercise is to give you practice in programming with one of Python’s most widely used “container” data types -- the List (commonly called an “Array” in most other programming languages). More specifically you will demonstrate how to: Declare list objects Access a list for storing (i.e., writing) into a cell (a.k.a., element or component) and retrieving (i.e., reading) a value from a list cell/element/component Iterate through a list looking for specific values using...
Objective: The goal of this lab is to practice (ragged) 2D arrays and simple recursion (in...
Objective: The goal of this lab is to practice (ragged) 2D arrays and simple recursion (in two separate parts). You are not allowed to use any of the built-in classes for this lab. If you find yourself needing to import anything at the top of your class, you are doing it wrong. Part A a) Create a class method called printArray2D that accepts a 2D integer array and prints out the values formatted such that each value occupies 4 spaces...
The goal of this paper is to practice identifying and evaluating how an author uses the...
The goal of this paper is to practice identifying and evaluating how an author uses the rhetorical appeals of ethos, pathos, and logos in their writing. After reading the essay “Designed to Addict” by Mary Aiken, describe the ways she uses these three methods of rhetorical appeal: how does she present herself and establish an authorial persona? How does she rely on the emotions of the audience to achieve her purpose? How does she establish a chain of reasoning in...
Writing a Java Code Requirements of the JAVA program: Your task is to calculate geometric area...
Writing a Java Code Requirements of the JAVA program: Your task is to calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input Invalid input should throw a message for the user. Example: Invalid input, please try again Each options should ask users for relevant...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT