CS作业代写 CSE 14 – cscodehelp代写

Implementing the Standard Methods
27 February 2019 OSU CSE 1

Loose Ends

Copyright By cscodehelp代写 加微信 cscodehelp

• In implementing several kernel interfaces so far, you have been given code in the
skeletons for the Standard methods
• The code for these methods is very stylized and easy to adapt to a new situation, even if the code itself is hardly transparent!
– Several new Java issues arise …
27 February 2019 OSU CSE 2

newInstance TF newInstance()
• Returns a new object with the same dynamic type as this, having an initial value. If the type TF has a no-argument constructor, then the value of the new returned object satisfies its contract. Otherwise, the value of the new returned object satisfies the contract of the constructor call that was used to initialize this.
• Ensures:
is_initial(newInstance) 27 February 2019 OSU CSE

newInstance TF newInstance()
• Returns a new object with the same dynamic type as this, having an initial value. If the type TF has a no-argument constructor, then the value of the new returned object satisfies its contract. Otherwise, the value of the new
Throughout this set of returned object satisfies the contract of the
slides, TF stands for the constructor call that was used to initialize this.
• Ensures:
kernel you are
implementing; it is not a
is_initial(newInstance)
generic type parameter!
27 February 2019 OSU CSE
type family interface whose

newInstance
TF newInstance()
• Returns a new object with the same dynamic type as this, having an initial value. If the type TF has a no-argument constructor, then the value of the new returned object satisfies its contract. Otherwise, the value of the new returned object satisfies the contract of the constructor call that was used to initialize this.
• Ensures:
is_initial(newInstance) 27 February 2019 OSU CSE
So, is_initial means what it says above…

Implementing newInstance
• Java already offers a method to make a new instance that is “like” an existing object, but it is messy to call it
• The reason for the Standard method newInstance is simply to make it easy to
call Java’s method for doing this, by wrapping the mess inside a method body
• The same code works for any type, so you can just copy the code that follows
27 February 2019 OSU CSE 6

Code for newInstance
@SuppressWarnings(“unchecked”) @Override
public final Queue newInstance() {
return this.getClass()
.getConstructor().newInstance(); } catch (ReflectiveOperationException e) {
throw new AssertionError(
“Cannot construct object of type ” + this.getClass());
27 February 2019

Code for newInstance
@SuppressWarnings(“unchecked”) @Override
public final Queue newInstance() {
return this.getClass() .getConstructor().newInstance();
throw new AssertionError(
The return type is whatever
} catch (ReflectiveOperationException e) { type family interface TF you
“Cannot construct object of type ” Queue is shown here
+ this.getClass()); }
for definiteness.
27 February 2019
are implementing;

Code for newInstance
@SuppressWarnings(“unchecked”)
public final Queue newInstance() {
return this.getClass()
.getConstructor().newInstance(); } catch (ReflectiveOperationException e) {
throw new AssertionError(
This tells the compiler not to
+ this.getClass()); }
27 February 2019
issue a warning about
“Cannot construct object of type ”
“unchecked conversion” …

Code for newInstance
@SuppressWarnings(“unchecked”) @Override
public final Queue newInstance() {
return this.getClass()
.getConstructor().newInstance();
} catch (ReflectiveOperationException e) { throw new AssertionError(
27 February 2019
+ this.getCla
warn you about on this
… which it would otherwise
“Cannot construct object of type ”
ent (even though it
cannot cause any trouble).

Code for newInstance
@SuppressWarnings(“unchecked”) @Override
public final Queue newInstance() {
return this.getClass()
.getConstructor().newInstance();
} catch (ReflectiveOperationException e) { The try/catch block and
throw new AssertionError(
exceptions are Java
“Cannot construct object of type ”
+ this.getClass());
later in the course.
27 February 2019
constructs to be explained

clear void clear()
• Resets this to an initial value. If the type TF has a no-argument constructor, then thissatisfiesitscontract. Otherwise, this satisfies the contract of the
constructor call that was used to initialize #this.
• Clears:this
27 February 2019 OSU CSE

void clear()
clears means what it says
above, i.e., is_initial is
true for that parameter.
• Resets this to an initial value. If the type
TF has a no-argument constructor, then thissatisfiesitscontract. Otherwise, this satisfies the contract of the
constructor call that was used to initialize #this.
• Clears:this
27 February 2019 OSU CSE
So, the parameter mode

Implementing clear
• Because this code has to do the same thing as the no-argument (or maybe some other) constructor, it is a good idea to factor out the code that initializes the instance variables into a separate private method
• For the no-argument constructor (usual case):
private void createNewRep() {
// initialize instance variables
27 February 2019 OSU CSE 14

Code for createNewRep • Again, code from Queue2 is shown:
private void createNewRep() { this.preFront = new Node(); this.preFront.next = null; this.rear = this.preFront; this.length = 0;
27 February 2019 OSU CSE

Code for clear
public final void clear() {
this.createNewRep(); }
27 February 2019 OSU CSE

Code for clear
public final void clear() {
this.createNewRep();
If there isn’t a no-argument constructor, then createNewRep
needs some parameters (like a constructor with parameters); see a SortingMachine implementation for an example.
27 February 2019

transferFrom void transferFrom(TF source)
• Sets this to the incoming value of source, and resets source to an initial value; source must have thesamedynamictypeasthis. IfthetypeTFhasa no-argument constructor, then source satisfies its contract. Otherwise,sourcesatisfiesthecontractof the constructor call that was used to initialize #source.
• Replaces:this
• Clears:source
• Ensures:
this = #source 27 February 2019

Implementing transferFrom • This code simply copies the values of all
the instance variables from source to this, and then re-initializes source
– For an instance variable of a reference type, the reference value is copied—but aliases are eliminated before transferFrom returns
27 February 2019 OSU CSE 19

Code (Pt 1) for transferFrom @Override
public final void transferFrom(Queue source)
assert source != null : “Violation of:”
+ ” source is not null”;
assert source != this : “Violation of:”
+ ” source is not this”;
assert source instanceof Queue2 : “”
+ “Violation of: source is of dynamic”
+ ” type Queue2“;
27 February 2019 OSU CSE

Code (Pt 1) for transferFrom @Override
public final void transferFrom(Queue source)
assert source != null : “Violation of:”
+ ” source is not null”;
assert source != this : “Violation of:”
+ ” source is not this”;
assert source instanceof Queue2 : “” whatever type family
interface TF you are + “Violation of: source is of dynamic”
+ ” type Queue2“;
27 February 2019 OSU CSE
The parameter type is
implementing; Queue shown here for definiteness.

Code (Pt 1) for transferFrom @Override
public final void transferFrom(Queue source)
+ ” type Queue2“;
assert source != null : “Violation of:” + ” source is not null”;
assert source != this : “Violation of:” + ” source is not this”;
assert source instanceof Queue2 : “”
First two asserts check for
repeated arguments.
+ “Violation of: source is of dynamic”
27 February 2019 OSU CSE
normal problems: null and

Code (Pt 1) for transferFrom
public final void transferFrom(Queue source)
assert source != null : “Violation of:”
This assert checks for a mismatch between the
dynamic types of source and this, using the
instanceof operator. + ” source is not null”;
assert source != this : “Violation of:” + ” source is not this”;
assert source instanceof Queue2 : “” + “Violation of: source is of dynamic” + ” type Queue2“;
27 February 2019 OSU CSE

Code (Pt 1) for transferFrom
Because of a problem in Java called type erasure, you can check
public final void transferFrom(Queue source)
only that the dynamic type is Queue2, meaning “Queue2 of
assert source != null : “Violation of:”
something”; more on this later.
+ ” source is not null”;
assert source != this : “Violation of:”
+ ” source is not this”;
assert source instanceof Queue2 : “”
+ “Violation of: source is of dynamic”
+ ” type Queue2“;
27 February 2019 OSU CSE

Code (Pt 2) for transferFrom @Override
public final void transferFrom(Queue source)
Queue2 localSource = (Queue2) source; this.preFront = localSource.preFront; this.rear = localSource.rear;
this.length = localSource.length; localSource.createNewRep();
27 February 2019 OSU CSE

Code (Pt 2) for transferFrom @Override
public final void transferFrom(Queue source)
Queue2 localSource = (Queue2) source;
this.preFront = localSource.preFront;
this.rear = localSource.rear;
this.length = localSource.length;
This cast cannot fail since the assert
localSource.createNewRep();
27 February 2019
above would have stopped execution in that case; so, source must be of dynamic type Queue2, and the ? must be T or the call would not have compiled.

The Cast and Why It Works Here
this length 27 February 2019

The Cast and Why It Works Here
The compiler thinks things look like this
picture because of the declared types of this and source, shown below.
this length Queue2
27 February 2019

The initialization of localSource claims
The Cast and Why It Works Here
that the dynamic type of source is Queue2, as shown here—and it is!
this length Queue2
length source
27 February 2019
OSU CSE 29
localSource
Queue2 Queue

How transferFrom Works
this length
Start here …
length source
localSource
27 February 2019
OSU CSE 30

How transferFrom Works
this length
… update this …
length source
localSource
27 February 2019
OSU CSE 31

How transferFrom Works
this length
… clear source …
length source
localSource
27 February 2019
OSU CSE 32

How transferFrom Works
this length
… and return.
length source
27 February 2019

Code (Pt 2) for transferFrom
To check your understanding: could this code be used in place of the last line shown
public final void transferFrom(Queue source)
in the body?
source.clear();
Queue2 localSource = (Queue2) source; this.preFront = localSource.preFront; this.rear = localSource.rear;
this.length = localSource.length; localSource.createNewRep();
27 February 2019 OSU CSE

程序代写 CS代考 加微信: cscodehelp QQ: 2235208643 Email: kyit630461@163.com

Leave a Reply

Your email address will not be published. Required fields are marked *