I. Tutorial Purpose
1. Understand the definition of abstract classes and abstract methods in Java;
2. Understand the interface definition in Java, master the interface definition form and interface implementation method.
Ii. experiment content
1. Use Java for encoding as required.
1) write an abstract class shape with the abstract methods getarea () and getperimeter ()
2) based on the shape class, the rectangle and circle classes are derived. Both of them implement the calculation area method getarea () and the calculation perimeter method getperimeter ();
3) construct the main function, generate the rectangle and circle objects, and call the getarea () and getperim () Methods of the rectangle and circle objects with the shape variables.
2. Take the phone as the parent class and the mobile phone and the landline phone as two sub-classes, and enable the mobile phone to implement the interface: movable. The landline phone has a subclass: cordless phone. Define interfaces and various types to clarify their inheritance relationships.
3. Based on the teacher Class implemented in Experiment 2, modify the code of the teacher class. The requirement is that arrays can be used for arrays formed by multiple teacher objects. sort (numbers are sorted from low to high ).
Iii. Lab requirements
Complete the program design and submit the lab report.
<1>
package shape;abstract class Shape {public abstract void getArea();public abstract void getPerimeter();}
Package Shape; public class circle extends shape {private int R; public circle (int r) {This. R = r;} public void getarea () {system. out. println ("Circle Area:" + math. pI * r);} public void getperimeter () {system. out. println ("circumference length:" + 2 * Math. pI * r);} public static void main (string [] ARGs) {shape c = new circle (4); C. getarea (); C. getperimeter ();}}Package Shape; public class rectangle extends shape {private int A, B; Public rectangle (int A, int B) {This. A = A; this. B = B;} public void getarea () {system. out. println ("rectangular area:" + a * B);} public void getperimeter () {system. out. println ("rectangle perimeter:" + 2 * (a + B);} public static void main (string [] ARGs) {shape r = new rectangle (3, 4 ); r. getarea (); R. getperimeter ();}}
Package teacher; import Java. util. arrays; public class teachers implements comparable {private string no; private int age; private string name; private string seminary; teachers (string no, string name, int age, string seminary) {This. no = no; this. name = Name; this. age = age; this. seminary = seminary;} public void getname () {system. out. println ("name:" + name);} public void getno () {system. out. println ("No.:" + NO);} public void getage () {system. out. println ("Age:" + age);} public void getseminary () {system. out. println (":" + seminary);} public void setseminary (string seminary) {This. seminary = seminary;} public void setno (string no) {This. no = no;} public void setname (string name) {This. name = Name;} public void setage (INT age) {This. age = age;} public Boolean equals (string no) {If (this. no = No) {return true;} else {return false;} Public String tostring () {return "name:" + name + "" + "No: "+ NO +" "+" Age: "+ age +" "+": "+ seminary;} public int compareto (Object O) {teachers t = (teachers) o; If (this. no. compareto (T. no)> 0) return 1; if (this. no. compareto (T. no) <0) Return-1; return 0;} public static void main (string ARGs []) {teachers [] T = new teachers [5]; T [0] = new teachers ("0901051717", "Wang Tao", 26, ""); t [1] = new teachers ("0901051714 ", "Si jifeng", 21, ""); t [2] = new teachers ("0901051719", "Wang Lei", 22, ""); T [3] = new teachers ("0901051713", "Ruoxi", 19, ""); t [4] = new teachers ("0901051715", "Jackie Chan ", 24, "Information Institute"); arrays. sort (t); For (INT I = 0; I <5; I ++) {T [I]. getname ();}}}