Experiment three classes and objects (I.)
First, the purpose of the experiment
1, master the basic concepts of class and object;
2, master the declaration of the class, the creation and use of methods;
3. The definition and use of the construction method of mastering class
4, Master the class member variable, the member method definition and the use method;
5. Understand the difference between class variable, class method and instance variable and instance method;
6. Understand How the method overloads in Java are implemented.
Second, the experimental requirements
1, according to the requirements of the experimental steps, the use of Notepad to write the corresponding Java program;
2. compile and run your own java program using the javac and Java commands provided in the JDK ;
3, according to compile and run the error information obtained in the process to modify the program to obtain the correct results;
4, record the various problems encountered in the experiment and solutions.
Third, the experimental steps
1. Declaration, creation and use of class
(1) Use Notepad to write the following Java program and Save as Circle.java;
Final static double PI;
Class circle{
pi=3.14;
Double R;
Double circumference () {
return 2*pi*r;
}
Double Area () {
return pi*r*r;
}}
(2) Modify the error, so that it can compile correctly;
(3) Use Notepad to write the following Java program and Save as Circletest.java;
Class circletest{
public static void Main (String args[]) {
Circle C=new Circle ();
System.out.printf ("area of the Circle is:%8.4f", C.area ());
}
}
(4) Run circletest to view the results.
(5) variables in Java must be assigned first, or there will be a compilation error (the variable is not initialized), then the member variable must set the initial value?
2. Construction method
(1) The Circle class does not declare a construction method, then the statement Circle C=new Circle (); Why?
(2) Add the following construction method to the Circle class:
Circle (double radius) {
R= radius;
}
(3) After recompiling Circle.java (do not recompile circletest temporarily. ) Java), What error will occur if you run Circletest? Why? How to modify?
3. Class variables and class methods
(1) Add the following code in the main method of the Circletest class:
Circle C1=null;
System.out.printf ("PI is:%8.4f", C1. PI); or circle.pi
(2) is the compile run error after modification? Why?
(3) Add the following code to the Circle class:
static void Setpi (double pi) {
Pi=pi;
this.r=3;
}
(4) What error will occur when recompiling the Circle class? Why? How to modify?
(5) the main method for modifying the Circletest class is:
Circle C1=null;
System.out.printf ("PI is:%8.4f\n", C1. PI);
Circle C=new Circle (3);
System.out.printf ("area of the Circle is:%8.4f\n", C.area ());
C1.SETPI (3.1415926);
System.out.printf ("PI is:%8.4f\n", C1. PI);
System.out.printf ("area of the Circle is:%8.4f\n", C.area ());
(6) What result does the above code output? Explain why the output is occurring by explaining its execution process.
4. Method overloading
(1) Use Notepad to write the following Java program and Save as Circle.java;
Class something{
int x;
int y;
void Move (int x,int y) {
This.x=x;
This.y=y;
}
}
(2) in the Somethingl class, try adding the following code separately to verify which forms of overloading are valid and explain why
Boolean move (int x,int y) {
Boolean flag= (this.x==x) && (this.y==y);
This.x=x;
This.y=y;
return flag;
}
void Move (int width,int height) {
X+=width;
Y+=height;
}
void Move (int distance,boolean direction) {
if (direction)
X+=distance;
Else
Y+=distance;
}
Iv. Results of the experiment
V. Summary of the Experiment
Java Third Experiment requirements