Hu Hao - 6th job -static keyword, subject

Title # 1:
the 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 .
Computer.java

QQQ Package; 

public class Computer { 
	
	/ ** 
	 * This class implements the factorial operation of the main process parameters passed, and the method of return to the main 
	 * / 
	public int getQQQ (n-int) { 
		// the TODO Auto-Generated Method Stub 
		int. 1 = X; // multiplicative variables used, the initial value is assigned. 1 
		for (I = int. 1; I <= n-; I ++) { 
			X = I *; 
		} 
		return X; 
	} 

}

  

App.Java

ZZZ Package; 
Import QQQ.Computer; 
Import Classes in java.util *;. 
public class the App { 

	/ ** 
	 * main class class, which includes a main method, a number of inputs n, by calling class Computer, 
	 * achieve factorial of n - operation 
	 * / 
	public static void main (String [] args) { 
		// the TODO Auto-Generated Method Stub 
		Computer Computer new new C = (); // instantiate an object of class Computer 
		Scanner r = new Scanner (System.in) ; 
		System.out.println ( "enter a number:"); 
		int = n-r.nextInt (); 
		c.getQQQ (n-); // call the method getQQQ Computer class 
		System.out.println (n + "factorial is: "+ c.getQQQ (n-)); 

	} 

}

 

Program implements screenshots

 

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.

MyPoint.java

cn.edu.ccut.point Package; 

public class MyPoint { 

	/ ** 
	 * This class implements the functionality required distance between two points 
	 * / 
	Double X; 
	Double Y; 
	public Double getX () { 
		return X; 
	} 
	public void setX (Double X ) { 
		in this.x = X; 
	} 
	public Double getY () { 
		return y; 
	} 
	public void setY (Double y) { 
		this.y from = y; 
	} 
	
	MyPoint () {// constructor, so x, y of the initial value is 0 
		X = 0; 
		Y = 0; 
	} 
	
	public MyPoint (Double X, Y Double) {// constructor arguments, parameters can be passed to achieve the assignment 
		in this.x = X; 
		this.y from Y =; 
	}
	 
	public static double distance (MyPoint p1, MyPoint p2) {// find the distance method
		double x1 = p1.getX (); // getX by () and getY (), horizontal and vertical coordinates of the points acquired 
		Double p2.getX X2 = (); 
		Double p1.getY Y1 = (); 
		Double Y2 = P2. getY (); 
		return the Math.sqrt (Math.pow ((X1-X2), 2) Math.pow + ((Y1-Y2), 2)); 
	} 
}

  

Test.java

* cn.edu.ccut.point Import;. 
Import Classes in java.util *;. 
public class the Test { 

	/ ** 
	 * main class category, calling MyPoint class in this class, seeking to realize the function of distance between two points 
	 * / 
	public void main static (String [] args) { 
		// the TODO Auto-Generated Method Stub 
		Scanner Scanner R & lt new new = (the System.in); 
		
		System.out.println ( "Please enter the value of one point X"); 
		Double Ax of R & lt = .nextDouble (); 
		System.out.println ( "Please enter the Y value of the points 1"); 
		Double r.nextDouble Ay = (); 
		System.out.println ( "Please enter the point X 2 value"); 
		Double Bx r.nextDouble = (); 
		System.out.println ( "Please enter the point Y 2 value");		 
		Double r.nextDouble By = (); 
		
		MyPoint new new MyPoint P1 = (Ax of, Ay); // instantiate class MyPoint object, and the value for the horizontal and vertical coordinates transfer point 
		MyPoint p2 = new MyPoint (Bx, By);
		System.out.print ( "two-point distance is:" + MyPoint.distance (P1, P2)); 
		
	} 

}

 

Screenshot operating results

Guess you like

Origin www.cnblogs.com/whohow/p/11564399.html