Java sixth job

A job

Problem 1: preparation of a class Computer, a class containing methods to find the factorial of n. The classes are packaged, and Java files App.java In another package is introduced in the package, in the definition of the object class Computer main class, calling methods factorial of n - (n is determined by the parameter), and outputs the result .

Topic 2:

       MyPoint a design class, it represents a point x and y coordinates having the class comprising:

  • Private members of two variables x and y coordinate values;
  • Member variables x and y and access modifier
  • Constructor with no arguments to create the point (0,0);
  • A constructor parameter, the specified point to create a coordinate parameters;
  • distance method (static modification) return parameter is the distance between the two point objects MyPoint type.

        Write main class Test, in the main category input coordinate points, two points to create objects using distance () method of calculating the distance between these two points.

Second, the code to add a comment

Topic 1:

package HomeWork2;

public class Computer {
public static void main(String[] args){
}
public static int fac(int a){
int fac1=1;
if(a>1){
for(int i=2;i<=a;i++){
fac1=fac1*i;
}
}
return fac1;
}
}

 

 

package HomeWork2;

Computer class {public
public static void main (String [] args) {// Why should the main function listed separately, the factorial column to the main function do not you?
}
Public static int FAC (int A) {// use public and static? ? ? ?
int FAC1 = 1;
IF (A> 1) {// if a <0 then factorial result is also 1, this is a BUG
for (int I = 2; I <= A; I ++) {
FAC1 = FAC1 * I;
}
}
return FAC1; // when to return? When a return? return how to use
}
}

 

Topic two:

package HomeWork2;

{class MyPoint1 public
Private int X, Y;
MyPoint1 () {// constructor name must be the class name (here, non-return type constructor)
X = 0;
Y = 0;
}
public MyPoint1 (int X , int y) {// constructor name must be the class name (this is the return value of type constructor)
in this.x = X;
this.y from Y =;
}
int getX () {
return X;
}
int getY () {
return Y;
}
void setX (int X) {
in this.x = X;
}
void setY (int Y) {
this.y from Y =;
}
public static Double distance1 (MyPoint1 XY1, MyPoint1 XY2) {
Double Distance the Math.sqrt = (Math.pow ((xy1.getX () - xy2.getX ()), 2) + Math.pow ((xy1.getY () - xy2.getY ()), 2));
return Distance ;
}
}

 

 

package HomeWork;
import HomeWork2.MyPoint1;
public class MyPoint {
public static void main(String[] args) {
MyPoint1 xy1=new MyPoint1(2,3);
MyPoint1 xy2=new MyPoint1(4,5);
double distance=MyPoint1.distance1(xy1,xy2);
System.out.println("两点间的距离是"+distance);
}

}

Third, run shot

 

Topic one:

 

 

Topic two:

 

 

 

 

Guess you like

Origin www.cnblogs.com/anemone0919/p/11544596.html