1, Experimental purpose
1. Exercise the definition of class construction method and member method.
2. Practice using static.
2, Experiment type and class hours
Verification / design / synthesis: verification; Class hours: 2.
3, Experimental environment
Several microcomputers equipped with JAVA language tool software (Eclipse)
4, Experimental content
1. Implement the Circle class according to the following requirements
- Member variables of Circle class:
The private final static member P represents the PI (initialized to Math.PI)
The private member radius represents the radius of the circle and is of type double
- Member methods of Circle class:
Circle() construction method, set the radius to 0
The Circle(double r) construction method initializes the radius to r when creating a Circle object
double getRadius() gets the radius value of the circle
void setRadius() sets the radius value of the circle
Method of reading PI P (what modifier is needed?)
double getPerimeter() gets the circumference of the circle
double getArea() gets the area of the circle
void disp() outputs the circumference, radius, perimeter and area of a circle to the screen
- Create the test class CircleTest of Circle class,
a) When there is no object of Circle class, the value of the member variable PI P is output.
b) Use the construction method Circle() to create the object c1, set the radius to 3.0, and output the radius, perimeter and area of the circle c1 to the screen
c) Use the construction method Circle(double r) to create the object c2, initialize the radius to 5.2, and output the radius, perimeter and area of circle c2 to the screen
package work03; public class CircleTest { public static void main(String[] args) { System.out.println("PI is:" + Circle.getP()); Circle c1 = new Circle(); c1.setRadius(3.0); c1.disp(); Circle c2 = new Circle(5.2); c2.disp(); } } class Circle{ // Member variable private static final double P = Math.PI; private double radius; double perimeter; double area; // Initialize to 0 public Circle() { this.radius = 0; } // Initialize to r public Circle(double r) { this.radius = r; } // Get radius public double getRadius() { return radius; } // Set radius public void setRadius(double r) { this.radius = r; } // Get P public double getP() { this.P = P; return P; } // Find the circumference of a circle double getPerimeter(){ perimeter = 2*P*radius; return perimeter; } // Find the area of a circle double getArea(){ area = P*radius*radius; return area; } // display information void disp(){ System.out.println("The radius of the circle is:" + radius); System.out.println("The circumference of the circle is:" + 2*P*radius); System.out.println("The area of the circle is:" + P*radius*radius); } }
Operation effect:
2. Write a complete Java Application program, define the Complex class Complex, generate two Complex objects in the test program, add 1+2i and 3+4i, generate a new Complex 4+6i and output it.
Complex must meet the following requirements:
- (1) The properties of Complex are:
Realpart: type int, representing the real part of the complex number
Imageinpart: type int, representing the imaginary part of the complex number
- (2) The methods of Complex include:
Complex(): constructor that sets both the real and imaginary parts of a complex number to 0
Complex (int r, int i): constructor. The formal parameter r is the initial value of the real part and I is the initial value of the imaginary part.
Complex complexAdd(Complex a): add the current complex object and the formal parameter complex object, and the result is still a complex value, which is returned to the caller of this method.
String toString(): combines the real part and imaginary part of the current complex object into a+bi string form, where a and b are the data of real part and imaginary part respectively.
Source code:
package work03; public class ComplexTest { public static void main(String[] args) { Complex aComplex = new Complex(1, 2); Complex bComplex = new Complex(3, 4); System.out.println("a=" + aComplex.toString()); System.out.println("b=" + bComplex.toString()); Complex c = aComplex.complexAdd(bComplex); System.out.println("a+b=" + c.toString()); } } class Complex{ // Member variable int realPart; int imaginPart; // Construction method public Complex() { this.realPart = 0; this.imaginPart = 0; } public Complex(int r,int i) { this.realPart = r; this.imaginPart = i; } // Member method Complex complexAdd(Complex a) { this.realPart += a.realPart; this.imaginPart += a.imaginPart; return this; } public String toString() { String str = realPart + "+" + imaginPart + "i"; return str; } }
Operation effect:
5, Test report requirements
1. Sort out the experimental results.
2. Summarize the experimental experience.