Package homework03; <br/> Import Java. util. compile; <br/>/* <br/> * compile a class, which has a method <br/> * Public int F (int A, int B) {<br/> *} <br/> * then write a subclass of this class. Rewrite method F () is required (), in addition, the Rewriting Method returns the minimum public multiple of two integers. Requirements: <br/> * in the method body of the override method, the hidden method is called to return the maximum public approx. m of A and B, and then a * B/M is returned; in the <br/> * main class of the application, create an object using the parent class and subclass respectively, and call method F () to calculate the maximum and minimum public multiples of the two positive integers. <Br/> */<br/> class father {<br/> Public int F (int A, int B) {// returns the maximum common approx. <br/> int R; <br/> do {<br/> if (a <B) {<br/> int T = A; <br/> A = B; <br/> B = T; <br/>}< br/> r = A % B; <br/> A = B; <br/> B = R; <br/>} while (R! = 0); <br/> return; <br/>}< br/> class son extends father {<br/> Public int F (int A, int B) {// override the parent class function public int F (int A, int B); returns the smallest public multiple. <Br/> return a * B/super. f (a, B); <br/>}< br/> public class ch05_1 {<br/> Public static void main (string [] ARGs) {<br/> S = new partition (system. in); <br/> system. out. println ("Enter the first INTEGER:"); <br/> int A = S. nextint (); <br/> system. out. println ("enter the second INTEGER:"); <br/> int B = S. nextint (); <br/> system. out. println ("the maximum common divisor calculated by the parent class method:" + new father (). f (a, B); <br/> system. out. println ("minimum public multiple calculated by the method overridden by the subclass:" + new son (). f (a, B); <br/>}< br/>
Running example:
Enter the first INTEGER:
12
Enter the second INTEGER:
9
Maximum common divisor calculated by the parent class method: 3
The minimum public multiple calculated by the method overridden by the subclass: 36
Package homework03; <br/>/* <br/> * First, write an abstract class that requires three Abstract METHODS: <br/> * public abstract void F (int x); <br/> * public abstract void g (int x, int y ); <br/> * public abstract double H (Double X); <br/> * then, three subclasses of the class are given. Requirement: use these subclasses in the main class of the application to create objects, and then let them <br/> * transform the object calling method: F (), g () and H (). <Br/> */<br/> abstract class f {<br/> public abstract void F (int x); <br/> public abstract void g (int x, int y); <br/> public abstract double H (Double X ); <br/>}< br/> class S1 extends f {<br/> Public void F (int x) {<br/> system. out. println ("f function printing parameter of subclass 1:" + x); <br/>}< br/> Public void g (int x, int y) {<br/> system. out. println ("G Function printing parameter of subclass 1 (INT) x =:" + x + "Y =" + y ); <br/>}< br/> Public double H (Double X) {<br/> system. out. println ("H function printing parameter of subclass 1 (double):" + x); <br/> return X; <br/>}< br/> class S2 extends f {<br/> Public void F (int x) {<br/> system. out. println ("f function printing parameter of subclass 2:" + x); <br/>}< br/> Public void g (int x, int y) {<br/> system. out. println ("G Function printing parameter of subclass 2 (INT) x =:" + x + "Y =" + y ); <br/>}< br/> Public double H (Double X) {<br/> system. out. println ("subclass 2 H function print parameter (double):" + x); <br/> return X; <br/>}< br/> class S3 extends f {<br/> Public void F (int x) {<br/> system. out. println ("f function printing parameter of subclass 3:" + x); <br/>}< br/> Public void g (int x, int y) {<br/> system. out. println ("G Function printing parameter of subclass 3 (INT) x =:" + x + "Y =" + y ); <br/>}< br/> Public double H (Double X) {<br/> system. out. println ("H function printing parameter of subclass 3 (double):" + x); <br/> return X; <br/>}< br/> public class ch05_2 {<br/> Public static void main (string [] ARGs) {<br/> F testf = new S1 (); // upload object <br/> testf. F (2); <br/> testf. G (2, 2); <br/> testf. H (2); </P> <p> testf = new S2 (); // upload object <br/> testf. F (2); <br/> testf. G (2, 2); <br/> testf. H (2); </P> <p> testf = new S3 (); // upload object <br/> testf. F (2); <br/> testf. G (2, 2); <br/> testf. H (2); </P> <p >}< br/>
Running result:
F function printing parameters of subclass 1: 2
G Function printing parameter of subclass 1 (INT) x =: 2 y = 2
Input parameter of the H function of subclass 1 (double): 2.0
F function printing parameters of subclass 2: 2
G Function printing parameter (INT) x =: 2 y = 2 of subclass 2
Subclass 2 H function print parameter (double): 2.0
F function printing parameters of subclass 3: 2
G Function printing parameter (INT) x =: 2 y = 2 of subclass 3
Input parameter of the H function of subclass 3 (double): 2.0
Package homework03; <br/>/* <br/> * compile a class that requires the class to implement an interface which has the following three Abstract METHODS: <br/> * public abstract void F (int x); <br/> * public abstract void g (int x, int y ); <br/> * public abstract double H (Double X); <br/> * requirement: use this class to create an object in the main class of the application, use the interface callback to call these methods: F (), g (), and H (); <br/> */<br/> interface interfacetest {<br/> public abstract void F (int x); <br/> public abstract void g (int x, int y); <br/> public abstract double H (Double X ); <br/>}< br/> Class A1 implements interfacetest {<br/> Public void F (int x) {<br/> system. out. println ("f function printing parameter of subclass 1:" + x); <br/>}< br/> Public void g (int x, int y) {<br/> system. out. println ("G Function printing parameter of subclass 1 (INT) x =:" + x + "Y =" + y ); <br/>}< br/> Public double H (Double X) {<br/> system. out. println ("H function printing parameter of subclass 1 (double):" + x); <br/> return X; <br/>}< br/> class A2 implements interfacetest {<br/> Public void F (int x) {<br/> system. out. println ("f function printing parameter of subclass 2:" + x); <br/>}< br/> Public void g (int x, int y) {<br/> system. out. println ("G Function printing parameter of subclass 2 (INT) x =:" + x + "Y =" + y ); <br/>}< br/> Public double H (Double X) {<br/> system. out. println ("subclass 2 H function print parameter (double):" + x); <br/> return X; <br/>}< br/> class A3 implements interfacetest {<br/> Public void F (int x) {<br/> system. out. println ("f function printing parameter of subclass 3:" + x); <br/>}< br/> Public void g (int x, int y) {<br/> system. out. println ("G Function printing parameter of subclass 3 (INT) x =:" + x + "Y =" + y ); <br/>}< br/> Public double H (Double X) {<br/> system. out. println ("H function printing parameter of subclass 3 (double):" + x); <br/> return X; <br/>}< br/> public class ch05_3 {<br/> Public static void main (string [] ARGs) {<br/> interfacetest testf = new A1 (); // interface callback <br/> testf. F (2); <br/> testf. G (2, 2); <br/> testf. H (2); </P> <p> testf = new A2 (); // interface callback <br/> testf. F (2); <br/> testf. G (2, 2); <br/> testf. H (2); </P> <p> testf = new A3 (); // interface callback <br/> testf. F (2); <br/> testf. G (2, 2); <br/> testf. H (2); </P> <p >}< br/>
The result is shown in question 2.