Maurice Tutor

(5)

$15/per page/Negotiable

About Maurice Tutor

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Algebra,Applied Sciences See all
Algebra,Applied Sciences,Biology,Calculus,Chemistry,Economics,English,Essay writing,Geography,Geology,Health & Medical,Physics,Science Hide all
Teaching Since: May 2017
Last Sign in: 399 Weeks Ago
Questions Answered: 66690
Tutorials Posted: 66688

Education

  • MCS,PHD
    Argosy University/ Phoniex University/
    Nov-2005 - Oct-2011

Experience

  • Professor
    Phoniex University
    Oct-2001 - Nov-2016

Category > Computer Science Posted 14 Sep 2017 My Price 10.00

Array Covariance in Java

13.5  Array Covariance in Java

As discussed in this chapter, Java array types are covariant with respect to the types of array elements (i.e., if B <:>, then B[ ] <: a[="">). This can be useful for creating functions that operate on many types of arrays. For example, the following function takes in an array and swaps the first two elements in the array.

 

 

1:       public swapper (Object[]  swappee){ 2:                              if  (swappee.length  > 1){

3:               Object temp = swappee[0];

4:               swappee[0] = swappee[1];

5:               swappee[1] = temp;

6:             }

7:       }

This function can be used to swap the first two elements of an array of objects of any type. The function works as is and does not produce any type errors at compile time or run time.

(a)     Suppose a is declared by Shape[] a to be an array of shapes, where Shape is some class. Explain why the principle if B <> A, then B[ ] <> A[ ] allows the type checker to accept the call swapper(a) at compile time.

(b)     Suppose that Shape[] a is as in part (a). Explain why the call swapper(a) and execution of the body of swapper will not cause a type error or exception at run time.

(c)     Java may insert run-time checks to determine that all the objects are of the correct type. What run-time checks are inserted in the compiled code for the swapper function and where? List the line number(s) and the check that occur on that line.

(d)     A friend of yours is aghast at the design of Java array subtyping. In his brilliance, he suggests that Java arrays should follow contravariance instead of covariance (i.e., if B <:>, then A[ ] <: b[="">). He states that this would eliminate the need for run-time type checks. Write three lines or fewer of code that will compile fine under your friend’s new type system, but will cause a run-time type error (assuming no run-time type tests accompany his rule). You may assume you have two classes, A and B, that B is asubtype of A, and that B contains a method, foo, not found in A. Here are two declarations that you can assume before your three lines of code:

 

B b[];

A a[] = new A[10];

 

(e)     Your friend, now discouraged about his first idea, decides that covariance in Java is all right after all. However, he thinks that he can get rid of the need for run-time type tests through sophisticated compile-time analysis. Explain in a sentence or two why he will not be able to succeed. You may write a few lines of code similar to those in part (d) if it helps you make your point clearly.

 

Answers

(5)
Status NEW Posted 14 Sep 2017 03:09 PM My Price 10.00

Hel-----------lo -----------Sir-----------/Ma-----------dam-----------Tha-----------nk -----------You----------- fo-----------r u-----------sin-----------g o-----------ur -----------web-----------sit-----------e a-----------nd -----------and----------- ac-----------qui-----------sit-----------ion----------- of----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n.P-----------lea-----------se -----------pin-----------g m-----------e o-----------n c-----------hat----------- I -----------am -----------onl-----------ine----------- or----------- in-----------box----------- me----------- a -----------mes-----------sag-----------e I----------- wi-----------ll

Not Rated(0)