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

use java C.java Print.java yaway amp Lec3 InheriP.. Assignment4Fall2018- Class I

ID: 3755079 • Letter: U

Question

use java


C.java

Print.java



yaway amp Lec3 InheriP.. Assignment4Fall2018- Class Init Construct Poly Home Insert Page Layout References Mailngs Review view Protected View icrosoft Word (4) (15 points) Exercise your knowledge of Inheritance and their Constructors For this exercise, you are given two classes A and B as follow: (in C.java). For the customprint () method, you need to import a utility package to support that. You need to add this to your first line of code import static utility.Print."; class A( AO C public void aMethod1) ( private void aMethod20 ( println "AO) construct in class A") print aMethod1"); print Private aMethod2"); // this is a static method static void aMethod3() ( print" static aMethod3 ); class B extends At BO f -println("BO extends. An Ther Fightin' of 4 Worde: 997 ALIENWARE

Explanation / Answer

--------------------------------------------------------

C.java

--------------------------------------------------------

import static Lab2.Print.*;

class A {

//Constructor:

A(){

println("A() construct in class A");

}

public void aMethod1() {

print(" aMethod1");

}

private void aMethod2() {

print(" Private aMethod2");

}

// this is a static method:

static void aMethod3() {

print(" static aMethod3 ");

}

}

class B extends A {

//Constructor:

B() {

println("B() extends A");

}

public void bMethod1() {

println(" bMethod1 ");

}

private void bMethod2() {

println(" private bMethod2 ");

}

}

public class C extends A { // class C extends class A as asked in the question.

C() {

println(" C() is class C extends B "); // This line will be printed whenever the constructor is called

}

C(boolean bool){   // constructor overloading. call this one to print aMethod1 and aMethod3.

println(" C() is class C extends B ");

b.aMethod1();

B.aMethod3();

}

B b = new B();

public static void main(String args[]) {

A a1 = new A();

B b1 = new B();

C c1 = new C();

C c2 = new C(true); // Calling the second constructor, this will print aMethod1 and aMethod3.

}

}


------------------------

output

-----------------------

A() construct in class A

A() construct in class A

B() extends A

A() construct in class A

A() construct in class A

B() extends A

C() is class C extends B

A() construct in class A

A() construct in class A

B() extends A

C() is class C extends B

aMethod1

static aMethod3


--------------------------

Print.java

-------------------------

package Lab2;

import java.io.*;

public class Print {
  
   // Print with a newline:
   public static void print(Object obj) {
       System.out.println(obj);
   }
  
   // Print a newline by itself:
   public static void print() {
       System.out.println();
   }
  
  
   public static void println(Object obj) {
       System.out.println(obj);
   }
  
   public static void println() {
       System.out.println();
   }
  
   // Print with no line break:
   public static void printnb(Object obj) {
       System.out.print(obj);
   }
  
   // The new java SE5 printf() (from C):
   public static PrintStream printf(String format, Object... args) {
       return System.out.printf(format, args);
   }
  
}