Java-- seventh time job

Topic one:

       5 on the basis of the job, and then creating a column type, comprising a rectangle, and the high volume of the three member variables, a constructor method to initialize member variables, and calculating the volume change method bottom two functions, in the main category input length, width, height, calculated cylinder volume, enter a new length, width, height, creating a new rectangle, the method using soled soled, the cylinder volume is calculated again.

1. Source:

/ * This code has written two main classes and a main categories, namely Juxing class, lengzhu and Main Example7 
 * Juxing class defines three main member variables (length, width, area), and a method get_Area ( ), is mainly used to calculate the area of rectangular bottom 
       in lengzhu class, the main member defines a variable depth and a high return method Get_depth () 
       method of the main class creates two main classes (juxing, lengzhu), and respectively a1, b1 and a2, b2, 
       the input value is passed through the transmission parameters corresponding to the two classes complete calculation, and finally returns to the main class, the final volume calculation is completed, 
       the final end of the replacement input length and width, volume calculation is completed after the end of changing. 
 * / 
Import Classes in java.util *. ;
 Class Juxing {    // Juxing type 
    int length;
     int width;
     int Area;
     int get_Area ()   // find methods bottom area 
    {
         return length * width;  
    }
} 
Class lengzhu {   // lengzhu type 
    int depth;
     int Get_depth () {
         return depth; 
    } 
} 
public  class Example7 {
       public  static  void main (String [] args) {
           int Volume1, volume2; 
          Scanner Reader = new new Scanner (the System.in ); 
          Juxing A1 = new new Juxing ();   // create an object class Juxing A1 
          lengzhu b1 = new new lengzhu ();   // create objects of classes b1 lengzhu
          System.out.println ( "Please enter the length of the rectangle:" ); 
          a1.length = reader.nextInt (); 
          System.out.println ( "Please enter the rectangular width:" ); 
          a1.width = reader.nextInt ( ); 
          System.out.println ( "enter a square prism height:" ); 
          b1.depth = reader.nextInt (); 
          volume1 = (a1.get_Area ()) * (b1.Get_depth ()); 
          System.out .println ( "quadrangular prism volume:" + Volume1); 
          Juxing A2 = new new Juxing ();   // create an object class Juxing A2 
          lengzhu B2 = new new lengzhu ();  // Create lengzhu object of class B2 
          ( "Please enter the rectangular soled after long:" System.out.println ); 
          a2.length = reader.nextInt (); 
          System.out.println ( "Please enter soled later rectangular width: " ); 
          a2.width = reader.nextInt (); 
          b2.depth = b1.depth; 
          volume2 = (a2.get_Area ()) * (b2.Get_depth ());   // calculate the volume 
          System. out.println ( "after the end of changing volume of a quadrangular prism" + volume2); 
    } 

}

 2. Run Screenshot:

Topic two:

   MyInteger design class name, which includes: int type value a data field construction method, when the specified int value, object data created MyInteger field value and the access modifier isEven () and isOdd () method, if the current object is even or odd, class method returns true isPrime (MyInteger i), determines whether the specified value is a prime number, returns true MyInteger create objects in the main class, each class MyInteger verification method.

1. Source Code

/ * This java file, the definition of a class and a main class MyInteger 
               in MyInteger class defines a member variable value, a constructor (for storing a value from main class transmitted value) 
     generated to access the value of and a modifier, and defines three methods, namely isEven (for determining whether an even number), 
  isOdd (for determining whether an odd number), the isPrime (for determining whether a prime number), which are boolean return type type 
                in the main class, the main class is MyInteger object a, invoke a method to create, from the console first determines whether the input number is an even number, odd prime number 
 * / 
Import java.util.Scanner; 
class MyInteger { 
	int value; 
    public MyInteger (int value) {// Constructors 
    	this.value = value;	 
    } 
	public int the getValue () {// accessors value 
		return value; 
	} 
	public void the setValue (int value) {// value modifier 
		this.value value =; 
	} 
	Boolean isEven () {// determines whether an even number, returns a true value if a 
		if (value% 2 == 0)
			to true return; 
		System.out.println ( "Please enter an integer:");
		the else 
			return to false; 
	} 
	Boolean isOdd () {// determines whether an odd number, is then returns true 
		IF (value% 2 = 0!) 
			return to true; 
		the else 
			return to false; 
	} 
	Boolean the isPrime () {// determines whether or not a prime number 
		I int; 
		IF (value == == 0 || value. 1) 
			return to false; 
		for (I = 2; I <= value; I ++) 
		{ 
			IF (value% 2 == 0) 
				BREAK; 
		} 
		IF (I> value ) 
			return to true; 
		the else 
			return to false; 
	} 
} 
public class Example8 { 
	public static void main (String [] args) { 
		Scanner Scanner Reader new new = (the System.in); 
		int = n-reader.nextInt (); // from the console enter an integer
		MyInteger a = new MyInteger (n) ; // Create Object 
		System.out.println ( "digital" + n + "is an even number:" + a.isEven ()); // call the method determines whether an even number 
		System.out .println ( "digital" + n + "whether it is odd:" + a.isOdd ()); // call the method determines whether odd 
		System.out.println ( "digital" + n + "is prime:" + a .isPrime ()); // call the method, it is determined whether or not a prime number 
	} 
}

2. Run Screenshot

figure 1.

figure 2.

 

Guess you like

Origin www.cnblogs.com/gywx/p/11555216.html