C2 P1

  • Give three examples of illegal uses for the extends and implements keywords.
  • What part of a method MUST absolutely change to Overload a method?
  • What part/s of a mthod MUST be exactly the same as the origninal method when Overriding that method?
  • Write your own coded example of an overloaded and an overridden method. Your code does need to compile. Write it below this section, and name your file c2p1.java (this means that they class you have that contains a main class MUST be named c2p1.java)

C2 P2

class Player extends animObj implements Animate{
public void moveLeft(animObj obj){} // moves the animation object left.
public void moveRight(animObj obj){}// moves the animation object right.
private void moveUp(animObj obj){} // moves the animation object up.
public void moveDown(animObj obj){} // moves the animation object down.
public void Jump(animObj obj){} // makes the animation object jump.
public void Run(animObj obj){} // makes the animation object run.
public void Run(animObj obj, int x, int y){}
}//end of Player

class animObj{
//animation object code goes here...
}//end of animObj
  • Compile the code above "as is". What kind of compiler error/s do you get, and explain why you are getting this/these error/s? Then, fix the error/s and compile the program so that it compiles with no error/s.
  • Is Overriding being implemented in this source code? If so, then explain where. If not, then simply state, "No."
  • Is Overloading being implmented in this source code? If so, then explain where. If not, then simply state, "No."
  • Copy and paste this code into an empty text file. Name the new text file: Player2.java. Then write code that overloads the Jump method.
  • After the animObj class, do the following: // Create a new class called TestCode. // In TestCode put a main method. // in the main method create a new Player object with an animObj reference type. Name the object obj1. // Create a new method called c2p2 (outside of main, but inside of TestCode) that takes an animObj as its signature. Inside the CastIt method implement code (exampled in the text and in the notes) that casts the variable reference from an animObj type to a Player reference type on the object. Name your variable obj2.

C2 P3

class M{}
class P extends M{}

class c2p3{
public static void main(String[] args){
M m = new M();
P p = new P();
m = c2p3.ReturnIt(p); //Line 7
}

static M ReturnIt(M obj){
return(obj); // Remember you can return sub-types of a type!
}
}
  • The code above does compile. What are combinations of m and p in Line 7 for which the code will not compile? (meaning if you switch them, or just have m, or just have p) Explain why the code will not compile for these combination. I will be looking at how detailed and accurate your answer is for each scenario.

C2 P4

class A{
int a;
int doAStuff(){
B b = new B();
a = b.doBStuff();
return a;
}
int doMoreAStuff(){
B b = new B();
a = b.doBStuff();
return a;
}
}

class B{
int b;
int doBStuff(){
A a = new A();
b = a.doMoreAStuff();
return b;
}
}
  • How can this code be re-written so that coupling is lowered, and cohesion is increased? Re-write the code as best as you can. Make sure it will compile.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.