Java Experiment Class 3 polymorphism and interfaces
I. Tutorial Purpose
Understand object-oriented polymorphism. Understand the relationship between an interface and the class that implements the interface, master the Declaration and usage of a class to implement multiple interfaces.
Ii. experiment content 1. Program understanding:
Interface Application
// Interface application // define a PCI interface: interface PCI {void start (); // define the abstract method start () void stop (); // define the abstract method stop ()} // define the NetworkCard class to implement the PCI interface class NetworkCard implements PCI {// implement the start () method public void start () {System. out. println ("send... ");} // implement the stop () method public void stop () {System. out. println ("Network stop. ") ;}}// define the SoundCard class to implement the PCI interface class SoundCard implements PCI {// implement the start () method public void start () {System. out. println ("Du... ");} // implement the stop () method public void stop () {System. out. println ("Sound stop. ") ;}// defines the MainBoard class // transmits the subclass object as a PCI parameter: class MainBoard {// an interface parameter p is passed here // define a userPCICard () method to receive the PCI-type parameter public void userPCICard (PCI p) {// It is precisely because the method in the PCI interface is implemented that the object can be passed to the reference variable p. start (); // call the start () method p of the input object. stop (); // call the stop () method of the passed object} // The public class Example3 {public static void main (String [] args) subclass object implemented by the interface) {MainBoard mb = new MainBoard (); // create the Instance Object NetworkCard nc = new NetworkCard () of the MainBoard class; // create the Instance Object nc mb of the NetworkCard class. userPCICard (nc); // call the userPCICard () method of the MainBoard object and pass the nc as a parameter to SoundCard SC = new SoundCard (); // create an Instance Object of the NetworkCard class SC mb. userPCICard (SC); // call the userPCICard () method of the MainBoard object and pass SC as a parameter }}
2. programming questions:
(1) Declare the Complex class Complex. The member variables include real and double virtual im of the double type. The member methods include three overloaded constructor methods: the constructor Complex (double real, double im); the constructor Complex (). this constructor internally calls the previous constructor using this; only the constructor of the real number is Complex (double real). this constructor uses this (real, 0) to call the constructor of two parameters. In addition, the member methods of the plural class include the plural addition, plural subtraction, and string description (rewrite the toString method of the Object and output the form of a + bi), equal comparison (to determine whether two plural values are equal, you need to override the equals method of the Object) and other methods. Define the test class and perform the corresponding test in the main method. (Procedure 1)
(2) define two interfaces: Area (claim getArea Method for Area) and Volume (claim getVolume method for Volume), declare ball Globe, implement the Area and Volume interfaces, and calculate the surface Area and Volume of the ball. Design the test class and perform corresponding tests in the main method. (Procedure 2)
(3) Complete the inheritance and implementation structure of the interface shown below, define the test class, and perform the test in the main method. (The interface and class name can be changed) (Program 3)
(4) design a program that references different subclass objects using the parent class type variables to realize type polymorphism.
Iii. Experiment results and analysis
Note: Provide the running results of each program and perform necessary analysis.
(1) running result of program 1
(2) running result of Program 2
(3) running result of Program 3
(4) execution result of program 4 (if selected) <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwPjxpbWcgYWx0PQ = "here write picture description" src = "http://www.bkjia.com/uploads/allimg/160423/042T51D9-3.png" title = "\"/>
Iv. Experiment source code
Note: copy and paste the source code of each program here.
(1) program 1 source code
Import java. util. complexclass Complex1 {double real; double im; // defines the real and virtual construction methods Complex1 (double real, double im) {this. real = real; this. im = im;} // construction method without parameters Complex1 () {this (0, 0);} // construction method with real numbers Complex1 (double real) {this (real, 0) ;}// defines the method for adding a complex number public Complex1 add (Complex1 c) {return new Complex1 (this. real + c. real, this. im + c. im);} // plural subtraction public Complex1 minus (Complex1 c) {return new Complex1 (this. real-c. real, this. im-c. im);} // String description (override the toString method of the Object and output the form of a + bi) public String toString () {if (im <0) return real + "" + im + "I"; else return real + "+" + im + "I";} // equal comparison (determine whether two plural numbers are equal, the equals method of the Object to be rewritten) public boolean equals (Object obj) {Complex1 c = (Complex1) obj; if (real = c. real & im = c. im) {return true;} return false ;}// defines the test class public class Example1 {public static void main (String [] args) {nation SC = new Nation (System. in); System. out. println ("Enter the real and virtual parts of the first plural:"); double a = SC. nextDouble (); double B = SC. nextDouble (); Complex1 c1 = new Complex1 (a, B); // create a Complex object System. out. println ("Enter the real and virtual parts of the second plural:"); double c = SC. nextDouble (); double d = SC. nextDouble ();/* double result; double d1 = 1.0; double d2 = 1.0; BigDecimal bd1 = new BigDecimal (Double. toString (d1); BigDecimal bd2 = new BigDecimal (Double. toString (d2); result = bd1.add (bd2 ). doubleValue (); // Add result = bd1.subtract (bd2 ). doubleValue (); // minus result = bd1.multiply (bd2 ). doubleValue (); // multiply result = bd1.divide (bd2, 10, BigDecimal. ROUND_HALF_UP ). doubleValue (); // when division is not complete, keep the last 10 digits rounded to */Complex1 c2 = new Complex1 (c, d); // create the Complex object System. out. println ("(" + c1.toString () + ") + (" + c2.toString () + ") =" + (c1.add (c2 ))); // call the add method and print the System. out. println ("(" + c1.toString () + ")-(" + c2.toString () + ") =" + c1.minus (c2 )); // call the minus method and print if (c1.equals (c2) // call the toString method System. out. println (c1.toString () + "=" + c2.toString (); else System. out. println (c1.toString () + "=" + c2.toString ());}}
(2) Program 2 source code
Import java. util. imports; import java. text. decimalFormat; // defines the Area interface Area {double getArea (); // defines the abstract method getArea ()} // defines the Volume interface Volume {double getVolume (); // define the abstract method getVolume ()} // declare the ball Globe to implement and calculate the ball surface Area and Volume. // The Globe class implements the Area and Volume Interface class Globe implements Area, volume {double r; public Globe (double r) {this. r = r ;}// implement the getArea () method public double getArea () {return 4 * Math. PI * r;} // implement the getVolume () method public double getVolume () {return 0.75 * Math. PI * r;} // defines the test class public class Example4 {public static void main (String [] args) {Variable SC = new variable (System. in); DecimalFormat df = new DecimalFormat ("0.00"); System. out. println ("Enter the ball radius (unit: cm)"); double rad = SC. nextDouble (); Globe globe = new Globe (rad); // create a Globe class object instance System. out. println ("the surface area of the ball is" + df. format (globe. getArea () + "cm2"); // call the getArea () method System of the Dog class. out. println ("the size of the ball is" + df. format (globe. getVolume () + "cm3"); // call the getVolume () method of the Dog class }}
(3) Program 3 source code
Import java. util. imports; import java. text. decimalFormat; // defines the PlaneGraphics interface PlaneGraphics {double area (); // defines the abstract method of area () double perimeter (); // define the perimeter abstraction method perimeter ()} // defines the three-dimensional image SolidGraphics interface SolidGraphics {double volume (); // defines the volume abstraction method volume ()} // defines the rectangular Rectangle class to implement PlaneGraphics Interface class Rectangle implements PlaneGraphics {double a, B; // defines the constructor public Rectangle (double a, double B) of two parameters) {this. a = a; this. B = B;} // The abstract method for area calculation area () public double area () {return a * B;} // The abstract method for perimeter calculation perimeter () public double perimeter () {return 2 * (a + B );}} // define the Cuboid class to inherit the rectangular Rectangle class while implementing the three-dimensional image SolidGraphics Interface class Cuboid extends Rectangle implements SolidGraphics {double c; // defines the constructor public Cuboid (double a, double B, double c) {super (a, B); this. c = c;} // abstract method for surface area calculation area () public double area () {return 2 * (a * B + B * c + a * c );} // volume extraction method volume () public double volume () {return a * B * c ;}} // define the test class public class Example6 {public static void main (String [] args) {Variable SC = new variable (System. in); DecimalFormat df = new DecimalFormat ("0.00"); System. out. println ("Enter the length and width of the rectangle: (unit: cm)"); double recLength = SC. nextDouble (); double recWidth = SC. nextDouble (); // create a Rectangle Class Object Rectangle rectangle = new Rectangle (recLength, recWidth); System. out. println ("the area of this rectangle is:" + df. format (rectangle. area () + "cm2"); System. out. println ("the circumference of this rectangle is:" + df. format (rectangle. perimeter () + "cm"); System. out. println ("Enter the length, width, and height of the cube: (unit: cm)"); double cubLength = SC. nextDouble (); double cubWidth = SC. nextDouble (); double cubHeight = SC. nextDouble (); // create a Cuboid Class Object Cuboid cuboid = new Cuboid (cubLength, cubWidth, cubHeight); System. out. println ("the surface area of the cube is:" + df. format (cuboid. area () + "cm2"); System. out. println ("the size of this cube is:" + df. format (cuboid. volume () + "cm3 ");}}
(4) program 4 source code (if selected)
// Define the interface Planinterface Plan {void eat (); // define the abstract eat () method void play (); // define the abstract play () method} // defines the Saturday class to implement the Plan interface class Saturday implements Plan {// implements the abstract eat () method public void eat () {System. out. println ("eat dessert");} // defines the abstract play () method public void play () {System. out. println ("bike ride") ;}// defines the Sunday class implementation Plan Interface class Sunday implements Plan {// implements the abstract eat () method public void eat () {System. out. println ("eat sushi");} // defines the abstract play () method public void play () {System. out. println ("") ;}// defines the test class public class Example9 {public static void main (String [] args) {weekend (new Saturday ()); weekend (new Sunday ();} // defines the static weekend () method and receives the public static void weekend (Plan p) parameter of the Plan type) {// determine the type and use its unique method if (p instanceof Saturday) {System. out. println ("Saturday:");} else {System. out. println ("Sunday:");} p. eat (); p. play ();}}