Access to objects using

 first question:

Requirements Analysis : 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, for the bottom two functions method, 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

package abd; build a abd

import java.util.Scanner; incorporated Scanner

Build a class member variable called zhuti, a rectangular area high

public class zhuTi {
private tri juxing;
private double high;
private double vol;

Zhuti build a constructor
public zhuTi (Tri Juxing, Double High) {

this.juxing = Juxing;
this.high = High;

}

Seeking cylinder area
public Double volCalc () {
Double Voli = juxing.getArea () * High;
return Voli;
}

The method of changing the configuration of the bottom
public void changDi (Double longth, Double width) {
juxing.longth = longth;
juxing.width = width;
}
primary method for inputting LWH
public static void main (String [] args) {
Scanner SC = Scanner new new (the System.in);
System.out.println ( "enter long:");
a float longth sc.nextFloat = ();

System.out.println ( "Enter Width:");
a float width = sc.nextFloat ();
Tri Tri Juxing new new = (longth, width);
System.out.println ( "Please enter high:");
Double High = sc.nextFloat ();

The method of building a column output area
zhuTi new new zhuTi ZT = (Juxing, High);
System.out.println ( "area of the cylinder is:" + zt.volCalc ());

Enter the new length and width

System.out.println ( "Please enter a new length:");
a float longth1 sc.nextInt = ();
System.out.println ( "Please enter a new width:");
a float width1 sc.nextInt = ();
NZT = new new zhuTi zhuTi (Juxing, High);
nzt.changDi (longth1, width1);

After the bottom area of the transducer
System.out.println ( "changed after bottom area of the cylinder is:" + zt.volCalc ());
}

}

 Test Run

The second question

demand analysis

MyInteger design class name, which includes: int type value a data field construction method, when the specified int value,

  Create MyInteger access object data field value and modifier isEven () and isOdd () method, if the current object is even or odd, returns true

  Class method 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

package abd; abd build a package

Myinteger build a value of a member variable class
public class MyInteger {
Private int [] value;
public MyInteger (n-int) {
value = new new int [] {n-};
}

GetValue methods to build a
public int [] getValue () {
return value;
}

Build a constructor
public void the setValue (int [] value) {
this.value = value;
}

The method of building a Boolean isEven
public Boolean isEven () {
IF (value [0]% 2 == 0)
return to true;
the else
return to false;
}

The method of building a Boolean isOdd
public Boolean isOdd () {
IF (value [0] == 2%. 1)
return to true;
the else
return to false;
}

public boolean isPrime(MyInteger My){
int n=My.value[0];
if(n<2){
return false;
}
for(int i=2;i*i<=n;i++){
if(n%i==0){
return false;
}
}
return true;
}

The method of the main
public static void main (String [] args) {
// constructor call has parameters
MyInteger new new MyInteger Y = (. 3);
System.out.println ( "is not an odd:" + y.isOdd ());
the System .out.println ( "is not even number:" + y.isEven ());
System.out.println ( "is not a prime number:" y.isPrime + (Y));
// test SET / GET
int [] A new new int = [] {}. 4;
y.setValue (A);
A = y.getValue ();
for (int I = 0; I <a.length; I ++) {
System.out.println ( "new domain has "A + [I]);
}
}
}

Test Run

 

Guess you like

Origin www.cnblogs.com/wusi1/p/11568604.html