Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hey, I don\'t understand how to trace through this code. Can someone please expl

ID: 3668104 • Letter: H

Question

Hey, I don't understand how to trace through this code. Can someone please explain how my teacher arrived at the answers posted at the bottom of the document?

CSC 2310, Spring 2016, Quiz 2
Name:__________________________________________________
public class Biggie extends JayZ {
public void a() {
System.out.print("Biggie a ");
super.a();
}
public String toString() {
return "Biggie";
}
}


public class JayZ extends Tupac {
public void a() {
System.out.print("JayZ a ");
b();
super.a();
}
}


public class FiftyCent extends Biggie {
public void b() {
system.out.print("FiftyCent b ");
}
}


public class Tupac {
public void a() {
System.out.print("Tupac a ");
}
public void b() {
System.out.print("Tupac b ");
}
public String toString() {
return "Tupac";
}
}


Given the classes above, what output is produced by the following code?
Tupac[] elements = {new Biggie(), new Tupac(), new JayZ(), new FiftyCent()};
for (int i = 0; i < elements.length; i++) {
elements[i].a();
System.out.println();
elements[i].b();
System.out.println();
System.out.println(elements[i]);
System.out.println();
}

Answers:


Biggie a JayZ a Tupac b Tupac b
Tupac b
Biggie
Tupac a
Tupac b
Tupac
JayZ a Tupac b
Tupac b
Tupac
Biggie a JayZ a FiftyCent b Tupac b
FiftyCent b
Biggie

Explanation / Answer

Hi, I have modified the code to produced Answer given in question.

public class Biggie extends JayZ {
   public void a() {
   System.out.print("Biggie a ");
   super.a();
   }
   public String toString() {
   return "Biggie";
   }
}

public class JayZ extends Tupac {
       public void a() {
       System.out.print("JayZ a "); // overriding Tupac a() method
       b(); // calling Tupac b()
       //super.a(); //calling Tupac a()
       }
}

public class FiftyCent extends Biggie {
      public void a(){ // My changes
          super.a();
          super.b();
      }
       public void b() {
       System.out.print("FiftyCent b ");
       }
}

public class Tupac {
       public void a() {
       System.out.print("Tupac a ");
       }
       public void b() {
       System.out.print("Tupac b ");
       }
       public String toString() {
       return "Tupac";
       }
}