Topic 1:
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.
Code:
1、App.java
1 / * 2 * main class, the method comprising a master; 3 * output volume, call rectangle modifiers soled, volume after soled; . 4 * / . 5 public class the App { . 6 public static void main (String [ ] args) { . 7 Qua Qua = new new Qua (6, new new Rec (3,4- )); . 8 / * . 9 * Create square pillar and a bottom length and width of the object and high quadrangular prism assignment (length width 3 4 6 high ) 10 * / . 11 System.out.println ( "volume rectangular prism is:" + qua.get_V ()); 12 is qua.setr ( new new Rec (5,2 )); 13 is System.out.print ( "Volume after soled quadrangular prism is:" + qua.get_V ()); 14 } 15 16 }
2、Rec.java
1 / * 2 * rectangular type, comprising two member variables, two methods; 3 * member variables a, b respectively represent the length and width of the rectangle; 4 * constructor method is used to modify the value of a member variable, get_area rectangular area calculated ; . 5 * / . 6 public class Rec { . 7 . 8 Double A; . 9 Double B; 10 Rec ( Double A, Double B) { . 11 the this II.A = A; 12 is the this .B = B; 13 is } 14 public Double get_area () { 15 // calculate area 16 return a*b; 17 } 18 }
3, Qua.java
1 / * 2 * cartridge type, comprising three member variables, three methods; 3 * a bottom member variable r, h is high, v is volume; 4 * constructor method to modify a member variable, r modifier commutation bottom, ger_V cylinder volume calculation; . 5 * / . 6 public class Qua { . 7 // rectangle, the bottom of the cylinder . 8 Rec R & lt; . 9 Double H; 10 Double V; . 11 Qua ( Double H, R & lt Rec) { 12 is / / constructor 13 is the this .h = H; 14 the this .r = R & lt; 15 } 16 public voidSETR (R & lt Rec) { . 17 // modifier 18 is the this .r = R & lt; . 19 } 20 is public Double get_V () { 21 is // calculated volume of the cylinder 22 is return r.get_area () * H; 23 is } 24 }
operation result:
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.
Code:
1、Test.java
1 / * 2 * main class, testing and validation methods MyInteger each class . 3 * / . 4 Package cn.edu.ccut.object1; . 5 . 6 public class the Test { . 7 . 8 public static void main (String [] args) { . 9 obj = MyInteger new new MyInteger (. 6); // Create Object 10 System.out.println ( "" + obj.getvalue () + "if an odd number:" + obj.isOdd ()); . 11 System.out.println ( "" + obj.getvalue () + "is an even number:" + obj.isEven ()); 12 is System.out.println ( "" + obj.getvalue () + "is prime:" +MyInteger.isPrime (obj)); 13 is obj.setvalue (. 11); // with modification value to modify the value of 14 System.out.println ( "" + obj.getvalue () + "if an odd number:" + obj. isOdd ()); 15 System.out.println ( "" + obj.getvalue () + "is an even number:" + obj.isEven ()); 16 System.out.println ( "" + obj.getvalue () + "is prime:" + MyInteger.isPrime (obj)); . 17 } 18 is }
2、MyInteger.java
. 1 / ** 2 * MyInteger class contains a member variable value, six methods; 3 data variable * value storing input member; 4 * constructor initialization, value and the access modifier, isEven determines whether an even number, isOdd Analyzing whether it is an odd number, isPrime determines whether a prime number; . 5 * / . 6 Package cn.edu.ccut.object1; . 7 . 8 public class MyInteger { . 9 static int value; 10 . 11 MyInteger ( int value) { // constructor 12 is MyInteger.value = value; 13 is } 14 15 public int getValue () { //Accessor 16 return value; . 17 } 18 is . 19 public void setValue ( int value) { // modifier 20 is = MyInteger.value value; 21 is } 22 is 23 is public Boolean isEven () { // determines whether an even number 24 IF (value% 0 == 2 ) { 25 return to true ; 26 is } 27 the else 28 return to false ; 29 } 30 31 is public Boolean isOdd () { // determines whether an odd 32 IF (value% 2 == 0 ) { 33 is return to false ; 34 is } 35 the else 36 return to true ; 37 [ } 38 is 39 public static Boolean the isPrime (MyInteger I) { // Analyzing is prime 40 int J; 41 is for (J = 2; J <value / 2; J ++ ) { 42 is iF (% value J == 0 ) { 43 is return to false ; 44 is } 45 } 46 return true; 47 } 48 }
operation result: